#!/bin/sh
#
### BEGIN INIT INFO
# Provides:       sendmail qmail
# Required-Start: $network $named $syslog $remote_fs $local_fs $time
# X-UnitedLinux-Should-Start: 
# Required-Stop:
# Default-Start:  3 5
# Default-Stop: 0 1 2 6
# Short-Description: Plesk Qmail MTA
# Description:    Start the Qmail MTA
### END INIT INFO

test -s /etc/rc.status && . /etc/rc.status && rc_reset

# Source LSB init functions
. /lib/lsb/init-functions

# Source rc.status
. /etc/rc.status

# Reset status
rc_reset

# Qmail specific parameters
qmail_dir="/var/qmail"
qmail_bin="$qmail_dir/bin/qmail-send"
service="Qmail"

# Set path
export PATH="$qmail_dir/bin:$PATH"

# Default delivery 
if [ -s "${qmail_dir}/control/defaultdelivery" ]; then
	defauldelivery=`cat ${qmail_dir}/control/defaultdelivery`
else
	defauldelivery="| /usr/bin/deliverquota ./Maildir"
fi
# end of qmail specific parameters

# Return values acc. to LSB for all commands but status:
# 0   - success 
# 1       - generic or unspecified error
# 2       - invalid or excess argument(s)
# 3       - unimplemented feature (e.g. "reload")
# 4       - user had insufficient privileges
# 5       - program is not installed
# 6       - program is not configured
# 7       - program is not running
# 8--199  - reserved (8--99 LSB, 100--149 distrib, 150--199 appl)
# 
# Note that starting an already running service, stopping
# or restarting a not-running service as well as the restart
# with force-reload (in case signaling is not supported) are
# considered a success.

# Binary check
if ! [ -x "$qmail_bin" ]; then
	echo >&2 "${warn}$qmail_bin is not a valid binary"
	rc_failed 5
	rc_status -v1
	rc_exit
fi

case "$1" in
    start)
# Exit gracefully, if we are already running
		checkproc $qmail_bin && echo -n "$service is already running " &&rc_status -v && rc_exit
	    echo -n "Starting $service "
	    # startproc is as good as start_daemon on SuSE-10.2 and the only method to start QMail w/o a hassle
	    # on SuSE-11. SuSE-11 start_daemon hangs upon "start" http://bugs.plesk.ru/show_bug.cgi?id=144200
	    startproc "$qmail_dir/bin/qmail-start" "$defauldelivery" splogger qmail
		sleep 1
		checkproc $qmail_dir/bin/qmail-send
		rc_status -v
	;;
	stop)
		echo -n "Shutting down $service "
		/sbin/killproc -TERM "$qmail_bin"
		rc_status -v
	;;
	try-restart|condrestart)
		if test "$1" = "condrestart"; then
			echo "${attn} Use try-restart ${done}(LSB)${attn} rather than condrestart ${warn}(RH)${norm}"
		fi
		$0 status
		if test $? = 0; then
			$0 restart
		else
			rc_reset    # Not running is not a failure.
		fi
# Remember status and be quiet
	rc_status
	;;
	reload|force-reload)
# WARNING: qmail-send reads its control files only when it starts. If
# you change the control files, you must stop and restart qmail-send.
# Exception: If qmail-send receives a HUP signal, it will reread locals
# and virtualdomains. For additional information see man 8 qmail-send
		echo -n "Reload service $service "
		killproc -HUP "$qmail_bin"
		rc_status -v
	;;
	restart)
# Stop the service and regardless of whether it was
# running or not, start it again.
		$0 stop
		$0 start
	;;
	status)
		echo -n "Checking for service $service "
		/sbin/checkproc "$qmail_bin"
		rc_status -v
	;;
	probe)
# Optional: Probe for the necessity of a reload, print out the
# argument to this init script which is required for a reload.
# Note: probe is not (yet) part of LSB (as of 1.9)
		echo "reload"
	;;
	*)
		echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload|probe}"
		exit 1
	;;
esac
rc_exit 
