#!/bin/bash
#
# snf-server	Starts and stops the SNFServer daemon (SUSE).
#
# Author--  Alban Deniz
#
# Copyright (C) 2008 ARM Research Labs, LLC.
# See www.armresearch.com for the copyright terms.
#
# chkconfig: 345 80 30
# description: SNFServer providing email filtering.
# 
### BEGIN INIT INFO
# Provides:          SNFServer
# Required-Start:    $syslog $remote_fs $network $named
# Should-Start: $time ypbind smtp
# Required-Stop:     $syslog $remote_fs
# Should-Stop: $time ypbind smtp
# Default-Start:     3 5
# Default-Stop:      0 1 2 6
# Short-Description: SNFServer providing email filtering.
# Description:       Start SNFServer to filter email for spam and
#	check IP addresses.
### END INIT INFO

# Directory to run in.
runDir=/usr/share/snf-server

# 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/snf-server/debug.log

# Location of installation.
installedDir="/usr"

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

# Name of config file.
configFile="/etc/snf-server/SNFServer.xml"

# 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/lock/$productionProg"

# Name of client.
clientProg="SNFClient"

# Check for missing binaries (stale symlinks should not happen)
# Note: Special treatment of stop for LSB conformance
SNFServer_BIN=$dir/$productionProg
test -x $SNFServer_BIN || { echo $"$SNFServer_BIN not installed"; 
	if [ "$1" = "stop" ]; then exit 0;
	else exit 5; fi; }

# Use the SUSE rc_ init script functions;
# emulate them on LSB, RH and other systems

# Default: Assume sysvinit binaries exist
start_daemon() { /sbin/start_daemon ${1+"$@"}; }
killproc()     { /sbin/killproc     ${1+"$@"}; }
pidofproc()    { /sbin/pidofproc    ${1+"$@"}; }
checkproc()    { /sbin/checkproc    ${1+"$@"}; }
if test -e /etc/rc.status; then
    # SUSE rc script library
    . /etc/rc.status
else
    export LC_ALL=POSIX
    _cmd=$1
    declare -a _SMSG
    if test "${_cmd}" = "status"; then
	_SMSG=(running dead dead unused unknown reserved)
	_RC_UNUSED=3
    else
	_SMSG=(done failed failed missed failed skipped unused failed failed reserved)
	_RC_UNUSED=6
    fi
    if test -e /lib/lsb/init-functions; then
	# LSB    
    	. /lib/lsb/init-functions
	echo_rc()
	{
	    if test ${_RC_RV} = 0; then
		log_success_msg "  [${_SMSG[${_RC_RV}]}] "
	    else
		log_failure_msg "  [${_SMSG[${_RC_RV}]}] "
	    fi
	}
	# TODO: Add checking for lockfiles
	checkproc() { return pidofproc ${1+"$@"} >/dev/null 2>&1; }
    elif test -e /etc/init.d/functions; then
	# RHAT
	. /etc/init.d/functions
	echo_rc()
	{
	    #echo -n "  [${_SMSG[${_RC_RV}]}] "
	    if test ${_RC_RV} = 0; then
		success "  [${_SMSG[${_RC_RV}]}] "
	    else
		failure "  [${_SMSG[${_RC_RV}]}] "
	    fi
	}
	checkproc() { return status ${1+"$@"}; }
	start_daemon() { return daemon ${1+"$@"}; }
    else
	# emulate it
	echo_rc() { echo $"  [${_SMSG[${_RC_RV}]}] "; }
    fi
    rc_reset() { _RC_RV=0; }
    rc_failed()
    {
	if test -z "$1"; then 
	    _RC_RV=1;
	elif test "$1" != "0"; then 
	    _RC_RV=$1; 
    	fi
	return ${_RC_RV}
    }
    rc_check()
    {
	return rc_failed $?
    }	
    rc_status()
    {
	rc_failed $?
	if test "$1" = "-r"; then _RC_RV=0; shift; fi
	if test "$1" = "-s"; then rc_failed 5; echo_rc; rc_failed 3; shift; fi
	if test "$1" = "-u"; then rc_failed ${_RC_UNUSED}; echo_rc; rc_failed 3; shift; fi
	if test "$1" = "-v"; then echo_rc; shift; fi
	if test "$1" = "-r"; then _RC_RV=0; shift; fi
	return ${_RC_RV}
    }
    rc_exit() { exit ${_RC_RV}; }
    rc_active() 
    {
	if test -z "$RUNLEVEL"; then read RUNLEVEL REST < <(/sbin/runlevel); fi
	if test -e /etc/init.d/S[0-9][0-9]${1}; then return 0; fi
	return 1
    }
fi

# Reset status of this service
rc_reset

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

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

}

# Return values acc. to LSB for all commands but status:
# 0	  - success
# 1       - generic or unspecified error
# 2       - invalid or excess argument(s)
# 3       - unimplemented feature (e.g. "reload")
# 4       - user had insufficient privileges
# 5       - program is not installed
# 6       - program is not configured
# 7       - program is not running
# 8--199  - reserved (8--99 LSB, 100--149 distrib, 150--199 appl)
# 
# Note that starting an already running service, stopping
# or restarting a not-running service as well as the restart
# with force-reload (in case signaling is not supported) are
# considered a success.

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

    # Start.
    if [ $debugMode ]
    then

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

    else
        
	(cd /usr/share/snf-server; \
            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
    else
	rc_failed 1
    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
	# Process is not running.
	RETVAL=0
    fi

    if [ $RETVAL -eq 0 ]; then 
	rm -f $lockFile
    else
	rc_failed 1
    fi
    return $RETVAL
}

restart(){
    stop
    start
}

case "$1" in
    start)
	start

	# Remember status and be verbose
	rc_status -v
	;;
    stop)
	stop

	# Remember status and be verbose
	rc_status -v
	;;
    restart)
	## Stop the service and regardless of whether it was
	## running or not, start it again.
	$0 stop
	$0 start

	# Remember status and be quiet
	rc_status
	;;
    status)
	echo -n "Checking for service SNFServer "
	checkproc $SNFServer_BIN
	rc_status -v
	echo -n "Checking for service SNFDebugServer "
	checkproc $dir/$debugProg
	rc_status -v
	;;
    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 production_mode file.
  	#
  	echo $"Switching to production mode"
  	createModeFile $productionModeFile
  	;;
    try-restart|condrestart|force-reload|reload|probe)
	# Not supported.
	echo -n "$0 $1 "
	rc_failed 3
	rc_status -v
	;;
    *)
	echo $"Usage: $0 {start|stop|status|restart|production_mode|debug_mode}"
	exit 1
	;;
esac
rc_exit
