#! /bin/sh
#
# postgrey      start/stop the postgrey greylisting deamon for postfix
#		(priority should be smaller than that of postfix)
#
# Author:	(c)2004-2006 Adrian von Bidder <avbidder@fortytwo.ch>
#		Based on Debian sarge's 'skeleton' example
#               Distribute and/or modify at will.
#
# Version:	$Id: postgrey.init 1436 2006-12-07 07:15:03Z avbidder $
#
### BEGIN INIT INFO
# Provides:          postgrey
# Required-Start:    $syslog, $local_fs
# Required-Stop:     $syslog, $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start/stop the postgrey daemon
### END INIT INFO

set -e

PATH=/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/postgrey
NAME=postgrey
DESC="postfix greylisting daemon"

PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME

# Gracefully exit if the package has been removed.
test -x $DAEMON || exit 0

# Read config file if it is present.
if [ -r /etc/default/$NAME ]
then
    . /etc/default/$NAME
fi

POSTGREY_OPTS="--pidfile=$PIDFILE --daemonize $POSTGREY_OPTS"
if [ -z "$POSTGREY_TEXT" ]; then
    POSTGREY_TEXT_OPT=""
else
    POSTGREY_TEXT_OPT="--greylist-text=$POSTGREY_TEXT"
fi

case "$1" in
  start)
	echo -n "Starting $DESC: $NAME"
	start-stop-daemon --start --quiet --pidfile $PIDFILE \
		--exec $DAEMON -- $POSTGREY_OPTS "$POSTGREY_TEXT_OPT"
	echo "."
	;;
  stop)
	echo -n "Stopping $DESC: $NAME"
	start-stop-daemon --stop --quiet --pidfile $PIDFILE --oknodo
        rm -f $PIDFILE
	echo "."
	;;
  reload|force-reload)
	echo -n "Reloading $DESC configuration..."
	start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE
	echo "done."
        ;;
  restart)
	echo -n "Restarting $DESC: $NAME"
	start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE
        rm -f $PIDFILE
	sleep 1
	start-stop-daemon --start --quiet --pidfile $PIDFILE \
                --exec $DAEMON -- $POSTGREY_OPTS  "$POSTGREY_TEXT_OPT"
	echo "."
	;;
  *)
	echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
	exit 1
	;;
esac

exit 0
