#! /bin/sh
#
# snfServer.  This shell script takes care of starting and stopping
# the ARM Research SNFServer daemon for Ubuntu systems.
#
# Author: Alban Deniz
#
# Copyright (C) 2008 ARM Research Labs, LLC.
# See www.armresearch.com for the copyright terms.
#
### 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 installation.
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"

# Name of client.
clientProg="SNFClient"

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

# Do NOT "set -e"

# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/usr/sbin:/usr/bin:/sbin:/bin
DESC="SNFServer providing email filtering"
NAME="snfServer"
DAEMON=$dir/$prog
DAEMON_ARGS="$configFile"
SCRIPTNAME=/etc/init.d/$NAME

# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0

# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME

# Load the VERBOSE setting and other rcS variables
[ -f /etc/default/rcS ] && . /etc/default/rcS

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
. /lib/lsb/init-functions

#
# Function that starts the daemon/service
#
do_start()
{
	# Return
	#   0 if daemon has been started
	#   1 if daemon was already running
	#   2 if daemon could not be started
	start-stop-daemon --chuid $userName --start --quiet \
	    --exec $DAEMON --test > /dev/null 2>&1 \
		|| return 1
	start-stop-daemon --chuid $userName --start --quiet \
	    --background --exec $DAEMON -- $DAEMON_ARGS > /dev/null 2>&1 \
	    || return 2
	# Check that process started.
	$dir/$clientProg -status.second > /dev/null 2>&1
	RETVAL=$?
	if [ $RETVAL -ne 0 ]; then
	    RETVAL=2
	fi
	return $RETVAL
}

#
# Function that stops the daemon/service
#
do_stop()
{
	# Return
	#   0 if daemon has been stopped
	#   1 if daemon was already stopped
	#   2 if daemon could not be stopped
	#   other if a failure occurred
        # Check whether SNFServer is running.
	start-stop-daemon --chuid $userName --start --quiet \
	    --exec $DAEMON --test > /dev/null
	RETVAL=$?

	if [ $RETVAL -ne 1 ]; then
	    return 1
	fi

	# Issue shutdown command
	$dir/$clientProg -shutdown > /dev/null 2>&1
	sleep 10

        # Check again whether SNFServer is running.
	start-stop-daemon --chuid $userName --start --quiet \
	    --exec $DAEMON --test > /dev/null
	RETVAL=$?

	if [ $RETVAL -eq 1 ] ; then
	    # Send TERM signal to stop SNFServer.
	    start-stop-daemon --stop --quiet --retry=TERM/5 --exec $DAEMON
	    RETVAL="$?"
	    [ "$RETVAL" = 2 ] && return 2
	    return "$RETVAL"
	fi

	# SNFServer isn't running.
	return 0

}

case "$1" in
  start)
	[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
	do_start
	case "$?" in
		0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
		2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
	esac
	;;
  stop)
	[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
	do_stop
	case "$?" in
		0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
		2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
	esac
	;;
  status)
	start-stop-daemon --chuid $userName --start --quiet \
	    --exec $DAEMON --test > /dev/null
	RETVAL=$?
	if [ $RETVAL -eq 0 ]; then
	    # Stopped
	    echo "$prog is stopped"
	else
	    # Running
	    echo "$prog (pid $(pidof $dir/$prog)) is running"
	fi
	;;
  restart|force-reload)
	#
	# If the "reload" option is implemented then remove the
	# 'force-reload' alias
	#
	log_daemon_msg "Restarting $DESC" "$NAME"
	do_stop
	case "$?" in
	  0|1)
		do_start
		case "$?" in
			0) log_end_msg 0 ;;
			1) log_end_msg 1 ;; # Old process is still running
			*) log_end_msg 1 ;; # Failed to start
		esac
		;;
	  *)
	  	# Failed to stop
		log_end_msg 1
		;;
	esac
	;;
  *)
	echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
	exit 3
	;;
esac
