#! /bin/sh --posix
#
# skeleton	example file to build /etc/init.d/ scripts.
#		This file should be used to construct scripts for /etc/init.d.
#
#		Written by Miquel van Smoorenburg <miquels@cistron.nl>.
#		Modified for Debian GNU/Linux
#		by Ian Murdock <imurdock@gnu.ai.mit.edu>.
#
# Version:	@(#)skeleton  1.9.1  08-Apr-2002  miquels@cistron.nl
#

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

IMAPD=/usr/sbin/dbmail-imapd
POP3D=/usr/sbin/dbmail-pop3d
LMTPD=/usr/sbin/dbmail-lmtpd

IMAPD_NAME=dbmail-imapd
POP3D_NAME=dbmail-pop3d
LMTPD_NAME=dbmail-lmtpd

NAME="dbmail"

PID_DIR="/var/run/$NAME/"

DESC="dbmail servers"

test -x $IMAPD || exit 0
test -x $POP3D || exit 0
test -x $LMTPD || exit 0

set -e

[ -e /etc/default/dbmail ] && . /etc/default/dbmail

config_valid() {
	/usr/sbin/dbmail-users -l 2>&1 >/dev/null && return 0
	cat << EOM
Dbmail cannot connect to the database server. Please make sure
/etc/dbmail/dbmail.conf is correct, the database-server is running,
the database for dbmail exists, and the dbmail user exists with all
necessary permissions. To re-configure /etc/dbmail/dbmail.conf run
'dpkg-reconfigure dbmail'.
EOM
	return 1
}	

ssl_wrapper() {
	# this needs more work. Use stunnel at your own risk,
	# or wait for native support for ssl in dbmail-2  :-(
	# note: stunnel4 in inetd mode will scale better.
	cd /etc/ssl/certs || return 0
	[ -e "$PEMFILE" ] || return 0
	[ -x /usr/sbin/stunnel ] || return 0
	# assuming stunnel runs on the same host as dbmail, even though
	# it aint necessarily so.
	STUNNLOPT="-D 7 -p $PEMFILE -S 0 -a /etc/ssl/certs -t 300 -s dbmail -g dbmail -P $PID_DIR"
	echo -n "SSL wrapper "
	case "$1" in 
		start)
		[ "$START_IMAPD" ] && \
			/usr/sbin/stunnel $STUNNLOPT -r localhost:imap -d imaps 
		[ "$START_POP3D" ] && \
			/usr/sbin/stunnel $STUNNLOPT -r localhost:pop3 -d pop3s
		;;
		
		stop)
		if [ "$START_IMAPD" ]; then
			pidfile="$PID_DIR/stunnel.localhost.imap.pid"
			if [ -f "$pidfile" ]; then
				kill `cat $pidfile` 2>&1 >/dev/null || true
			fi
		fi
		if [ "$START_POP3D" ]; then
			pidfile="$PID_DIR/stunnel.localhost.pop3.pid"
			if [ -f "$pidfile" ]; then
				kill `cat $pidfile` 2>&1 >/dev/null || true
			fi
		fi
		;;
	esac	
	return 0
}	

persistent_start() {
	## _debug="true"
	
	eval '_daemon=$'$1
	eval '_name=$'$1'_NAME'
	echo -n "$_name "
	count=0
	while [ $count -lt 30 ]; do
		echo -n "."
		start-stop-daemon --start --exec $_daemon
		sleep 2
		pids=`pidof $_daemon` 1>/dev/null
		if [ -n "$pids" ]; then
			[ $_debug ] && echo "...got pids: $pids"
			return 0
		fi	
		count=$(($count+1))
	done
	echo "Starting $_name failed."
	return 1
}


persistent_stop() {
	 #_debug="true"
	
	eval '_daemon=$'$1
	eval '_name=$'$1'_NAME'
	echo -n "$_name "
	count=0
	
	start-stop-daemon --stop --retry 10 \
		--pidfile ${PID_DIR}/${_name}.pid \
		--name $_name &>/dev/null || true
		
	while [ $count -lt 5 ]; do
		echo -n "."
		pids=`pidof $_daemon` 1>/dev/null
		if [ -n "$pids" ]; then
			[ $_debug ] && echo "...got pids: $pids"
			start-stop-daemon --stop --name $_name || true
		else
			return 0
		fi	
		count=$(($count+1))
		sleep 2
	done
	[ $_debug ] && echo "Stopping $_name failed. Killing hard."
	killall -9 $_name
	
	return 0
	
}


case "$1" in
  start)
	config_valid || exit 0
	echo -n "Starting $DESC: "
	[ "$START_IMAPD" ] && persistent_start "IMAPD" || true
	[ "$START_POP3D" ] && persistent_start "POP3D" || true
	[ "$START_LMTPD" ] && persistent_start "LMTPD" || true
	[ "$START_SSL" ] && ssl_wrapper start || true
 	echo "done."
	;;
  stop)
	echo -n "Stopping $DESC: "
	[ "$START_SSL" ] && ssl_wrapper stop 2>&1 /dev/null || true
	[ "$START_IMAPD" ] && persistent_stop "IMAPD" || true
	[ "$START_POP3D" ] && persistent_stop "POP3D" || true
	[ "$START_LMTPD" ] && persistent_stop "LMTPD" || true
	echo "done."
	;;
  reload)
  	echo -n "Reloading $DESC: "
	[ "$START_IMAPD" ] && start-stop-daemon --stop --signal HUP --pidfile ${PID_DIR}/${IMAPD_NAME}.pid --name $IMAPD_NAME || true
	[ "$START_POP3D" ] && start-stop-daemon --stop --signal HUP --pidfile ${PID_DIR}/${POP3D_NAME}.pid --name $POP3D_NAME || true
	[ "$START_LMTPD" ] && start-stop-daemon --stop --signal HUP --pidfile ${PID_DIR}/${LMTPD_NAME}.pid --name $LMTPD_NAME || true
	echo "done."
	;;
  restart|force-reload)
	$0 stop || true
	sleep 3
	$0 start
	;;
  *)
	N="$NAME"
	echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
	exit 1
	;;
esac

exit 0

