#!/bin/sh
########################################################################
### FILE:	/etc/init.d/greylist
### PURPOSE:	Start/stop the greylistd(8) daemon.
########################################################################

FLAGS="defaults 19"
RUNDIR=/var/run/greylistd
DATADIR=/var/lib/greylistd
UGID=mail:mail

### Read settings from /etc/default/greylistd
[ -r /etc/default/greylistd ] && . /etc/default/greylistd

daemon=/usr/sbin/greylistd
pidfile=$RUNDIR/pid
socket=$RUNDIR/socket


# See if the daemon is present
test -x "$daemon" || exit 0

case "$1" in
    start)
	if [ -e "$socket" ]
	then
	    echo "$0:"
	    echo "  Another instance of \`${daemon##*/}' seems to be running."
	    echo "  If this is not the case, please remove \"$socket\"."
	    exit 1
	fi

	if [ "$UGID" ]
	then
	    mkdir -p "$RUNDIR"  && chown -R "$UGID" "$RUNDIR"
	    mkdir -p "$DATADIR" && chown -R "$UGID" "$DATADIR"
	fi


	echo -n "Starting greylisting daemon: "
	start-stop-daemon --start --background \
	    --chuid "$UGID" \
	    --pidfile "$pidfile" --make-pidfile \
	    --exec "$daemon" &&
	    echo "${daemon##*/}."
	;;


    stop)
	echo -n "Stopping greylisting daemon: "
	start-stop-daemon --stop --pidfile "$pidfile" &&
	    rm -f "$pidfile" &&
	    echo "${daemon##*/}."
	;;


    restart|reload|force-reload)
	$0 stop
	$0 start
	;;


    *)
	echo "Usage: $0 {start|stop|restart|reload|force-reload}" >&2
	exit 1
	;;
esac

exit 0

