#! /bin/sh
#
# Written by Bennett Todd <bet@rahul.net> as part of the pop-before-smtp daemon
# Customized by Jonas Smedegaard <dr@jones.dk> for use with Debian GNU systems

PATH=/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/pop-before-smtp
NAME=pop-before-smtp
DESC=pop-before-smtp
PID=/var/run/$NAME.pid
MAILLOG=/var/log/mail.log
DB=/var/lib/pop-before-smtp/hosts

test -f $DAEMON || exit 0

set -e

die(){ echo "$progname: $*">&2; exit 1; }
initdb(){ rm -f $DB; touch $DB; /usr/sbin/postmap $DB; }

case "$1" in
  start)
	echo -n "Starting $DESC: "
	initdb
	$DAEMON --logfile=$MAILLOG & echo $! >$PID
	echo "$NAME."
	if [ `/usr/sbin/postconf | grep -c /var/lib/pop-before-smtp/hosts` -lt 1 ]; then
		echo "Warning: Postfix may not take advantage of pop-before-smtp currently"
		echo "(read /usr/share/doc/pop-before-smtp/README.Debian for more info)!"
	fi
	;;
  stop)
	echo -n "Stopping $DESC: "
	p=`cat $PID 2>/dev/null`; test -n "$p" && (
		kill $p || exit 0; sleep 1
		kill -9 $p 2>/dev/null || exit 0; sleep 1
		kill -0 $p && die "$PID won't die"
	)
	if test $? -eq 0; then
		rm -f $pid
		echo "$NAME."
	else
		echo "failed..."
		exit 1
	fi
	echo ''
	;;
  reload|force-reload)
	echo "Reloading $DESC configuration files."
	# cruel: simply erase everything and hope for the best...
	initdb
	;;
  restart)
	#
	#	If the "reload" option is implemented, move the "force-reload"
	#	option to the "reload" entry above. If not, "force-reload" is
	#	just the same as "restart".
	#
	$0 stop || true
	$0 start
	;;
  status)
	p=`cat $pid 2>/dev/null`
	test -n "$p" || die "no pidfile for $DESC"
	kill -0 $p || die "$NAME[$p] is no longer running"
	ps -up $p
	;;
  *)
	N=/etc/init.d/$NAME
	echo "Usage: $N {start|stop|restart|reload|force-reload|status}" >&2
	exit 1
	;;
esac

exit 0
