#!/bin/sh
#
# dbmail init script for debian
# 
# written by Paul J Stevens, 2004-2006
# 
#
### BEGIN INIT INFO
# Provides:          dbmail
# Required-Start:    $local_fs $remote_fs $syslog $network
# Required-Stop:     $local_fs $remote_fs $syslog $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start dbmail services
# Description:       Run network services provided by dbmail such as
#                    imap-server, pop3-server, lmtp-server, timsieve-server
### END INIT INFO


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

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

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

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

. /lib/lsb/init-functions

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

if [ ! -d ${PID_DIR} ]; then
	mkdir -p -m 0755 ${PID_DIR}
	chown dbmail:dbmail ${PID_DIR}
fi

service_start() {
	eval '_daemon=$'$1
	eval '_name=$'$1'_NAME'
	[ -e $_daemon ] || return 0
	
	log_progress_msg "$_name"
	start-stop-daemon --start --exec $_daemon
	
	r=$?
	if [ $r -gt 0 ]; then
		log_failure_msg "failed"
		return $r
	fi
	
	return 0
	
}


service_stop() {
	eval '_daemon=$'$1
	eval '_name=$'$1'_NAME'
	
	log_progress_msg "$_name"
	start-stop-daemon --stop --retry 10 \
		--pidfile ${PID_DIR}/${_name}.pid >/dev/null 2>&1 || true

	pidof ${_name} >/dev/null 2>&1 || return 0
	# try harder
	echo -n "."
	killall ${_name} >/dev/null 2>&1 || true
	sleep 4
	pidof ${_name} >/dev/null 2>&1 || return 0
	echo -n "."
	killall -9 ${_name} >/dev/null 2>&1 || true
	sleep 2
	pidof ${_name} >/dev/null 2>&1 || return 0
	echo -n "."
	log_failure_msg "failed"
	return 1

}
	
service_reload() {
	eval '_daemon=$'$1
	eval '_name=$'$1'_NAME'
	
	log_progress_msg "$_name"
	start-stop-daemon --stop --signal HUP --quiet --pidfile ${PID_DIR}/${_name}.pid
	
	r=$?
	if [ $r -gt 0 ]; then
		log_failure_msg "failed"
		return $r
	fi
	
	return 0
	
}

case "$1" in
  start)
	log_daemon_msg "Starting $DESC"
	r=0
	if [ "$START_IMAPD" ]; then service_start "IMAPD" || r=1; fi
	if [ "$START_POP3D" ]; then service_start "POP3D" || r=1; fi
	if [ "$START_LMTPD" ]; then service_start "LMTPD" || r=1; fi
	if [ "$START_SIEVE" ]; then service_start "SIEVE" || r=1; fi
 	log_end_msg $r
	;;
	
  stop)
	log_daemon_msg "Stopping $DESC"
	r=0
	if [ "$START_IMAPD" ]; then service_stop "IMAPD" || r=1; fi
	if [ "$START_POP3D" ]; then service_stop "POP3D" || r=1; fi
	if [ "$START_LMTPD" ]; then service_stop "LMTPD" || r=1; fi
	if [ "$START_SIEVE" ]; then service_stop "SIEVE" || r=1; fi
	log_end_msg $r
	;;
	
  reload)
  	log_daemon_msg "Reloading $DESC"
	r=0
	if [ "$START_IMAPD" ]; then service_reload "IMAPD" || r=1; fi
	if [ "$START_POP3D" ]; then service_reload "POP3D" || r=1; fi
	if [ "$START_LMTPD" ]; then service_reload "LMTPD" || r=1; fi
	if [ "$START_SIEVE" ]; then service_reload "SIEVE" || r=1; fi
	log_end_msg $r
	;;
	
  restart|force-reload)
	$0 stop
	$0 start
	;;
  *)
	N="$NAME"
	echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
	exit 1
	;;
esac

exit 0

