#!/bin/bash
#
# SNFServer	This shell script takes care of starting and stopping
#		the MicroNeil SNFServer daemon for RedHat 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 provides email filtering (anti-spam) services \
#              See www.armresearch.com for details.
# processname: SNFServer

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

# Source networking configuration.
. /etc/sysconfig/network

# 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 &"

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

restart(){
    stop
    start
}


# See how we were called.
case "$1" in
    start)
	start
	;;
    stop)
	stop
	;;
    status)
	status $prog
	;;
    restart)
	restart
	;;
    *)
	echo $"Usage: $0 {start|stop|status|restart}"
	exit 1
esac

exit $?
