#! /bin/sh
#
# amavisd-new-milter	/etc/init.d/ initscript for amavisd-new milter
# 		$Id: amavisd-new-milter.init,v 1.6 2003/08/25 13:54:37 hmh Exp $
#
#		Copyright (c) 2003 by Brian May <bam@debian.org>
#			and Henrique M. Holschuh <hmh@debian.org>
#		Distributed under the GPL version 2
#

PATH=/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/amavis-milter
PARENTDAEMON=/usr/sbin/amavisd-new
NAME=amavis-milter
DESC="AMaViS Daemons (milter)"
PARAMS="-D -p /var/lib/amavis/amavisd-new-milter.sock"

test -f ${PARENTDAEMON} || exit 0
test -f ${DAEMON} || exit 0

set -e

START="--start --quiet --pidfile /var/run/amavis/amavisd-new-milter.pid --chuid amavis --exec ${DAEMON} -- ${PARAMS}"

case "$1" in
  start)
	echo -n "Starting $DESC: "
	if start-stop-daemon ${START} ; then
		echo "$NAME."
	else
		if start-stop-daemon --test ${START}  >/dev/null 2>&1; then
			echo "(failed)."
			exit 1
		else
			echo "${DAEMON} already running."
			exit 0
		fi
	fi
	;;
  stop)
	echo -n "Stopping $DESC: "
	if start-stop-daemon --stop --retry 10 --quiet \
		--pidfile /var/run/amavis/amavisd-new-milter.pid \
		--exec $DAEMON -- $PARAMS ; then
		echo "$NAME."
	else
		echo "(failed)."
	fi
	;;
#  reload)
#	echo "Reloading $DESC configuration files."
#	start-stop-daemon --stop --signal 1 --quiet --exec $DAEMON
#  	;;
  restart|force-reload)
	$0 stop && {
		echo -n "Waiting for complete shutdown..."
		i=5
		while [ $i -gt 0 ] ; do
			if start-stop-daemon --start \
			   --pidfile /var/run/amavis/amavisd-new-milter.pid \
			   --test \
			   --exec $DAEMON >/dev/null 2>&1 ; then
			     break
			fi
			sleep 2s
			i=$(($i - 1))
			echo -n "."
		done
		[ $i -eq 0 ] && {
			echo
			echo "fatal: incomplete shutdown detected, aborting."
			exit 1
		}
		echo
	}
	exec $0 start
	;;
  *)
	N=/etc/init.d/amavisd-new-milter
	# echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
	echo "Usage: $N {start|stop|restart|force-reload}" >&2
	exit 1
	;;
esac

exit 0
