#!/bin/sh
#
# /etc/init.d/expurgate -- start the expurgate daemon
#
### BEGIN INIT INFO
# Provides:          expurgate
# Required-Start:    $remote_fs $syslog $named $network $time
# Required-Stop:     $remote_fs $syslog $named $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: expurgate
# Description:       eXpurgate e-mail scanning and classification service
### END INIT INFO

VERBOSE=yes

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

[ -x /usr/bin/expurgate ] || exit 0

ENABLE=no
CHROOT=""
CONFIG=""
PIDFILE=""
OPTIONS=""

[ -f /etc/default/expurgate ] && . /etc/default/expurgate

name=expurgate
binpath=/usr/bin/expurgate
pidfile=/var/run/expurgate.pid
config=/etc/expurgate/expurgate.conf
chroot=
options=

if [ "$ENABLE" = no ] ; then
    echo "$name: disabled, see /etc/default/expurgate"
    exit 0
fi

[ -z "$CHROOT"  ] || chroot="$CHROOT"
[ -z "$CONFIG"  ] || config="$CONFIG"
[ -z "$PIDFILE" ] || pidfile="$PIDFILE"
[ -z "$OPTIONS" ] || options="$OPTIONS"

options="$options --daemonize --pidfile $pidfile --config $chroot$config"
start_stop_options="--quiet --exec $binpath --pidfile $chroot$pidfile"

if [ -n "$chroot" ] ; then
    options="$options --chroot $chroot"
fi

case $1 in
  start)
    echo -n "Starting $name "

    start-stop-daemon --start $start_stop_options -- $options

    case $? in
        0) [ "$VERBOSE" != no ] && echo -n "[OK]" ;;
        *) [ "$VERBOSE" != no ] && echo -n "[FAILED]" ;;
    esac
    echo
    ;;

  stop)
    echo -n "Stopping $name "

    start-stop-daemon --stop $start_stop_options

    case $? in
        0) [ "$VERBOSE" != no ] && echo -n "[OK]" ;;
        *) [ "$VERBOSE" != no ] && echo -n "[FAILED]" ;;
    esac
    echo
    ;;

  reload|force-reload)
    if ! $binpath $options --test-config > /dev/null 2> /dev/null ; then
        echo "Config file contains errors."
        exit 6
    fi

    echo -n "Reloading $name "

    start-stop-daemon --stop --signal 1 $start_stop_options

    case $? in
        0) [ "$VERBOSE" != no ] && echo -n "[OK]" ;;
        *) [ "$VERBOSE" != no ] && echo -n "[FAILED]" ;;
    esac
    echo
    ;;

  restart)
    echo -n "Restarting $name "

    start-stop-daemon --stop $start_stop_options
    sleep 1
    start-stop-daemon --start $start_stop_options -- $options

    case $? in
        0) [ "$VERBOSE" != no ] && echo -n "[OK]" ;;
        *) [ "$VERBOSE" != no ] && echo -n "[FAILED]" ;;
    esac
    echo
    ;;

  *)
    echo "Usage: /etc/init.d/expurgate {start|stop|reload|force-reload|restart}" >&2
    exit 1
    ;;
esac

exit 0
