#! /bin/sh
### BEGIN INIT INFO
# Provides:          assp
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: ASSP init script
# Description:       This script is used to start and stop the 
#                    ASSP AntSpam SMTP Proxy
### END INIT INFO

# Author: Thomas Eckardt <Thomas.Eckardt@thockar.com>

name=assp

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

# specify your pathes here
#
# the path to the assp user home
user_home=$PWD

# the path to the perl binary
perlbin="/usr/bin/perl"

# the path to the assp base folder
base="/opt/${name}"

# the path to the assp.pl script
assp="${base}/${name}.pl"

# the path to the assp PID file
pidfile="${base}/pid"

# tells assp that it is started as a OS controlled daemon
# this overrides and changes the configured value for --AsADaemon in assp.cfg
assp_parms="--AsADaemon:=2"

# the friendly name
desc="AntiSpam SMTP Proxy"


# Exit if assp is not installed
[ -x "$assp" ] || exit 0

# Load the VERBOSE setting and other rcS variables
#. /lib/init/vars.sh

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.2-14) to ensure that this file is present
# and status_of_proc is working.
#. /lib/lsb/init-functions



# code goes here
get_status() {
    local retcode=0 # assume assp is running
    if [ -f ${pidfile} ]; then # is the PID found
        if ! ps -p $(cat $pidfile) > /dev/null; then # is the process not running
	    rm ${pidfile}
	    echo "removed orphaned PID file ${pidfile}"
	    retcode=1 # assp is not running
	else
	    retcode=0 # assp is running
	fi
    else
	retcode=1 # assp is not running
    fi
    return $retcode
}

assp_start() {
    ! get_status && echo "Starting ${desc} - ${name}." && \
    ${perlbin} ${assp} ${base} ${assp_parms} > /dev/null && \
    echo "${name} started" || \
    echo "${name} not started"
}

assp_stop() {
    if [ ! -f ${pidfile} ];then # PID file not available
	echo "${name} seems not to be running - missing PID file ${pidfile}"
	exit 1
    fi
    if ! get_status; then # assp is not running
        exit 1
    fi
    echo "Stopping ${desc} - ${name} - sent INT."
    pkill -INT -F ${pidfile} # send INT to assp
    echo "wait for ${name} termination - max 60 seconds."
    local timeout=60
    # wait until assp has stopped but max 60 seconds
    while [ -f ${pidfile} -a ${timeout} -gt 0 ]; do
        timeout=$(( timeout -1 ))
        sleep 1
    done
    if [ -f ${pidfile} ];then # assp is still running - kill it
        echo "${name} was not stopped within 60 seconds - will KILL it"
        pkill -KILL -F ${pidfile}
        sleep 2
        rm ${pidfile}
    fi
    echo "${desc} - ${name} stopped"
}

assp_status() {
    if get_status; then
	echo "{$desc} - ${name} is running as pid" $(cat $pidfile).
        return 0
    else
	echo "{$desc} - $name is not running"
        return 1
    fi
}


case "$1" in
  start)
    assp_start 
	;;
  stop)
	assp_stop
	;;
  status)
       assp_status && exit 0 || exit $?
       ;;
  restart|force-reload)
	echo "Restarting ${desc} - ${name}"
	if get_status; then
	    assp_stop
	fi
	assp_start
	;;
  *)
	echo "Usage: /etc/init.d/${name} {start|stop|status|restart|force-reload}" >&2
	exit 3
	;;
esac

:
