#! /bin/sh

# Rmilter init script
### BEGIN INIT INFO
# Provides:                 rmilter
# Required-Start:    $local_fs $named $remote_fs $syslog
# Required-Stop:     $local_fs $remote_fs
# Should-Start:             sendmail
# Should-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Script to start/stop the rmilter
# Description: another spam-defense service
### END INIT INFO

# Based on skeleton by Miquel van Smoorenburg and Ian Murdock

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin
DAEMON=/usr/sbin/rmilter
NAME=rmilter
DESC="Rmilter Mail Filter Daemon"
USER=_rmilter
RUNDIR=/run/$NAME
SOCKET=$RUNDIR/$NAME.sock
SCRIPTNAME=/etc/init.d/$NAME

[ -x $DAEMON ] || exit 0

. /lib/init/vars.sh
. /lib/lsb/init-functions

export TMPDIR=/tmp
# Apparently people have trouble if this isn't explicitly set...

set -e

SSD="start-stop-daemon --pidfile /run/$NAME.pid --name rmilter"

do_start()
{
	mkdir -m750 -p $RUNDIR
	chown $USER:adm $RUNDIR
	chmod g+s $RUNDIR
	rm -f $SOCKET
	$SSD --start --background --make-pidfile --chuid $USER --oknodo \
		--startas $DAEMON -- -c /etc/rmilter.conf
}

do_stop()
{
	$SSD --stop --oknodo "$@"
}

case "$1" in
  start)
    [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
    do_start;;
  stop)
    [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
    do_stop;;
  restart|force-reload)
    [ "$VERBOSE" != no ] && log_daemon_msg "Restarting $DESC" "$NAME"
    do_stop --retry 5 && do_start;;
  status)
    PID=`pidof $DAEMON`
    if [ x$PID = x ]; then
        echo "$DAEMON is not running"
    else
      echo "$DESC is running with pid[$PID]"
    fi
    exit 0;;
  *)
    echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload|status}" >&2
    exit 1;;
esac

RET=$?
[ "$VERBOSE" != no ] && log_end_msg $RET
exit $RET
