#!/bin/sh
#
# snf-server	Starts and stops the SNFServer daemon (FreeBSD).
#
# Author--  Alban Deniz
#
# Copyright (C) 2008 ARM Research Labs, LLC.
# See www.armresearch.com for the copyright terms.
#

# PROVIDE: SNFServer
# REQUIRE: FILESYSTEMS
# KEYWORD: shutdown

. /etc/rc.subr

# Directory to run in.
runDir=PREFIX/share/PACKAGE_NAME

# Define mode files.
debugModeFile=$runDir/debug_mode
productionModeFile=$runDir/production_mode

# Set debug mode flag.
if [ -f $debugModeFile ]
then
    debugMode=true
fi

# Debug output file.
debugOutputFile=/var/log/PACKAGE_NAME/debug.log

# Location of installation.
installedDir="PREFIX"

# Location of programs.
dir="$installedDir/sbin"

# Name of config file.
configFile="CONFFILE"

# Name of client.
clientProg="SNFClient"

# Names of daemons.
debugProg="SNFDebugServer"
productionProg="SNFServer"

name="snfserver"
rcvar=snfserver_enable
required_dirs=$dir
required_files="$dir/$productionProg $dir/$clientProg $configFile"
snfserver_user=snfuser
snfserver_group=snfuser

# Start in a directory that is writable.
snfserver_chdir=PREFIX/share/PACKAGE_NAME

if [ $debugMode ]
then

    # Enable core dumps.
    ulimit -c unlimited

    # Get system call info.
    command="/usr/bin/truss"

    # Start with output redirected to a file.
    command_args="-d $dir/$debugProg $configFile >> $debugOutputFile 2>&1 &"

else

    # Run non-debug version.
    command=$dir/$productionProg

    # Start with output discarded.
    command_args="$configFile > /dev/null 2>&1 &"

fi

start_postcmd="${name}_poststart"

snfserver_poststart()
{
    $dir/$clientProg -status.second > /dev/null 2>&1
    return $?
}

status_cmd="${name}_status"

snfserver_status()
{
    # Check whether the programs are running.
    RETVAL=0
    for progName in $debugProg $productionProg
    do

        SNFPID=$(check_process $dir/$progName)
        if [ -n "$SNFPID" ]; then
	    echo "$progName is running as pid $SNFPID."
            RETVAL=$(($RETVAL+1))
	else
	    echo "$progName is not running."
        fi
    done
    if [ 0 -eq $RETVAL ]
    then
        # Not running.
	return 1
    else
        # At least one program is running.
	return 0
    fi
}

stop_cmd="${name}_stop"
snfserver_stop()
{
    echo "Stopping $name."
    $dir/$clientProg -shutdown > /dev/null 2>&1
    sleep 10
}

stop_postcmd="${name}_poststop"

snfserver_poststop()
{
    DEBUG_SNFPID=$(check_process $dir/$debugProg)
    PRODUCTION_SNFPID=$(check_process $dir/$productionProg)

    # Check that the programs are no longer running.
    RETVAL=0
    for progName in $debugProg $productionProg
    do

        SNFPID=$(check_process $dir/$progName)
        if [ -n "$SNFPID" ]; then
            kill $SNFPID
            RETVAL=$(($RETVAL+$?))
        fi
    done
    return $RETVAL
}

# Additional commands.
extra_commands="production_mode debug_mode"

#
# Function to create the mode file.
#
createModeFile()
{
    fileName=$1

    # Remove any existing mode files.
    rm -f $productionModeFile $debugModeFile $fileName
    (
	echo "PACKAGE_NAME mode file"
	echo
	echo "This file specifies whether PACKAGE_NAME is configured to run in"
	echo "production mode or debug mode.  If the name of this file is"
        echo "'production_mode', then PACKAGE_NAME is configured to run in"
        echo "production mode.  If the name is 'debug_mode', then PACKAGE_NAME is"
        echo "configured to run in debug mode."
	echo
	echo "To run in debug mode:"
	echo
	echo "    1) Run 'PACKAGE_NAME debug_mode'"
	echo
	echo "    2) Run 'PACKAGE_NAME restart' (if PACKAGE_NAME is already running),"
	echo "       or 'PACKAGE_NAME start' (to start PACKAGE_NAME)"
	echo
	echo "To run in production mode:"
	echo
	echo "    1) Run 'PACKAGE_NAME production_mode'"
	echo
	echo "    2) Run 'PACKAGE_NAME restart' (if PACKAGE_NAME is already running),"
	echo "       or 'PACKAGE_NAME start' (to start PACKAGE_NAME)"
	echo
	echo "By default, PACKAGE_NAME is configured to run in production mode."
	) > $fileName

}

production_mode_cmd="${name}_production_mode"

# Switch to production mode.
snfserver_production_mode()
{
    echo "Switching to production mode"
    createModeFile $productionModeFile
}

debug_mode_cmd="${name}_debug_mode"

# Switch to debug mode.
snfserver_debug_mode()
{
    echo "Switching to debug mode"
    createModeFile $debugModeFile
}

load_rc_config $name
run_rc_command "$1"
