#!/bin/bash
#
# SNFServer	This shell script takes care of starting and stopping
#		the MicroNeil SNFServer daemon for SUSE systems.
#
# 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 4 5
# Default-Stop:      0 1 2 6
# Short-Description: SNFServer providing email filtering.
# Description:       Start SNFServer to filter email for spam,
#	blacklist IP addresses, etc.
### END INIT INFO

# Location of programs.
installedDir="PREFIX"

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

# Name of config file.
configFile="CONFFILE"

# Name of daemon.
prog="SNFServer"

# Name of client.
clientProg="SNFClient"

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

# Name of lockfile.
lockFile="/var/lock/subsys/$prog"

# Start command.
snfStartCmd="$dir/$prog $configFile > /dev/null 2>&1 &"

# Check for missing binaries (stale symlinks should not happen)
# Note: Special treatment of stop for LSB conformance
SNFServer_BIN=$dir/$prog
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

# 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(){
    SNFPID=$(pidof -s $dir/$prog)
    echo -n $"Starting $prog "
    if [ -n "$SNFPID" ] ; then
	return 0
    else
	su $userName -c "$snfStartCmd" -s /bin/sh
        RETVAL=$?
        if [ $RETVAL -eq 0 ]; then
	    $dir/$clientProg -status.second > /dev/null 2>&1
	    RETVAL=$?
	fi
    fi
    if [ $RETVAL -eq 0 ]; then
	touch $lockFile
    else
	rc_failed 1
    fi
    return $RETVAL
}

stop(){
    echo -n $"Stopping $prog "
    SNFPID=$(pidof -s $dir/$prog)
    if [ -n "$SNFPID" ]; then
	$dir/$clientProg -shutdown > /dev/null 2>&1
	sleep 10
	SNFPID=$(pidof -s $dir/$prog)
	if [ -n "$SNFPID" ]; then
	    kill $SNFPID
	    RETVAL=$?
	else
	    RETVAL=0
	fi
    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
	;;
    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}"
	exit 1
	;;
esac
rc_exit
