#!/bin/bash
#
# snf-server	Starts and stops the SNFServer daemon (RedHat).
#
# Author--  Alban Deniz
#
# Copyright (C) 2008 ARM Research Labs, LLC.
# See www.armresearch.com for the copyright terms.
#
# chkconfig: 345 80 30
# description:	SNFServer provides email filtering (anti-spam) services \
#              See www.armresearch.com for details.
# processname: SNFServer

# 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

# Source function library.
. /etc/rc.d/functions

# 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 daemon.
debugProg="SNFDebugServer"
productionProg="SNFServer"

# Name of client.
clientProg="SNFClient"

# Name of user to run as.
userName="snfuser"

# Name of lockfile.
lockFile="/var/PACKAGE_NAME/PACKAGE_NAME"
#
# 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

}

start(){
    echo -n $"Starting $productionProg: "
    for prog in $productionProg $debugProg
    do
        SNFPID=$(pidof -s $dir/$prog)
        if [ -n "$SNFPID" ] ; then
            echo -n $"$productionProg is already running"
            failure
            echo
            return 1
        fi
    done

    # Start.
    if [ $debugMode ]
    then

	# Enable core dumps and start with strace and output redirected.
        (ulimit -c unlimited; \
            su $userName -s /bin/sh -c \
            "echo Starting $dir/$debugProg on $(date) >> $debugOutputFile"; \
            cd PREFIX/share/PACKAGE_NAME; \
	    su $userName -c \
            "strace -r -tt -v $dir/$debugProg $configFile >> $debugOutputFile 2>&1 &" \
	    -s /bin/sh)

    else
        
	(cd PREFIX/share/PACKAGE_NAME; \
            su $userName -c "$dir/$productionProg $configFile > /dev/null 2>&1 &" \
	    -s /bin/sh > /dev/null 2>&1)
    fi

    RETVAL=$?
    if [ $RETVAL -eq 0 ]; then 
	$dir/$clientProg -status.second > /dev/null 2>&1
	RETVAL=$?
    fi

    if [ $RETVAL -eq 0 ]; then
        touch $lockFile
        echo success
        echo
    else
        failure
        echo
    fi
    return $RETVAL
}

stop(){
    echo -n $"Stopping $productionProg: "
    DEBUG_SNFPID=$(pidof -s $dir/$debugProg)
    PRODUCTION_SNFPID=$(pidof -s $dir/$productionProg)
    if [ -n "$DEBUG_SNFPID" ] || [ -n "$PRODUCTION_SNFPID" ]; then
	$dir/$clientProg -shutdown > /dev/null 2>&1
	sleep 10

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

            SNFPID=$(pidof -s $dir/$prog)
	    if [ -n "$SNFPID" ]; then
	        kill $SNFPID
	        RETVAL=$(($RETVAL+$?))
	    fi
        done
    else
	echo -n $"$productionProg is not running"
	RETVAL=1
	failure
	echo
    fi
    if [ $RETVAL -eq 0 ]; then 
        echo
        rm -f $lockFile
    fi
    return $RETVAL
}

restart(){
    stop
    start
}


# See how we were called.
case "$1" in
    start)
	start
	;;
    stop)
	stop
	;;
    status)
	status $productionProg
	status $debugProg
	;;
    restart)
	restart
	;;
    debug_mode)
  	#
  	# Remove any mode flags, and create the debug_mode file.
  	#
  	echo $"Switching to debug mode"
  	createModeFile $debugModeFile
  	;;
    production_mode)
  	#
  	# Remove any mode flags, and create the debug_mode file.
  	#
  	echo $"Switching to production mode"
  	createModeFile $productionModeFile
  	;;
    *)
	echo $"Usage: $0 {start|stop|status|restart|production_mode|debug_mode}"
	exit 1
esac

exit $?
