#!/bin/sh
# qmail This shell script helps you stop and start qmail temporarily.
#
# chkconfig:	2345 80 30
#
# description:	Qmail is a Mail Transport Agent, which is the program that \
# moves mail from one machine to another.

# Source function library
. /etc/rc.d/init.d/functions

# Get network config
. /etc/sysconfig/network

# Check that networking is up.
if is_no "${NETWORKING}"; then
	msg_network_down qmail
	exit 1
fi

svcget() {
	local svc
	for s in `ls -d "$@" 2>/dev/null`; do
		svc="$svc $s"
	done
	echo $svc
}

# hold STATE QUEUE
# state can be 'hold' or 'unhold'
# queue can be 'local' or 'remote', if empty both are changed
hold() {
	local state="$1"
	local queue="$2"

	umask 022

	case "$state" in
	hold)
		val=1
		;;
	unhold)
		val=0
		;;
	*)
		msg_usage "$0 hold [queue] | unhold [queue]"
		;;
	esac


	case "$queue" in
	local|remote)
		echo "$state $queue"
		echo "$val" > /etc/qmail/control/hold$queue
		;;
	'')
		echo "$state local"
		echo "$val" > /etc/qmail/control/holdlocal
		echo "$state remote"
		echo "$val" > /etc/qmail/control/holdremote
		;;
	*)
		msg_usage "$0 hold [queue] | unhold [queue]"
		;;
	esac

	svc -h /service/qmail-send
}

wait_for_proc() {
	local uid="$1"
	local proc="$2"
	local pids=$(pgrep -u $uid $proc | xargs)
	if [ "$pids" ]; then
		echo -n "Waiting $proc ($pids) to exit"
		timeout=0
		while [ "$(pgrep -u $uid $proc)" ]; do
			if [ $timeout -ge 30 ]; then
				break
			fi

			timeout=$((timeout + 1))
			sleep 1
			echo -n "."
		done
		echo ""

		pids=$(pgrep -u $uid $proc)
		if [ "$pids" ]; then
			echo -n "Sending SIGTERM"
			kill $pids
		fi

		timeout=0
		while [ "$(pgrep -u $uid $proc)" ]; do
			if [ $timeout -ge 5 ]; then
				break
			fi

			timeout=$((timeout + 1))
			sleep 1
			echo -n "."
		done
		echo ""

		pids=$(pgrep -u $uid $proc)
		if [ "$pids" ]; then
			echo -n "Sending SIGKILL"
			kill -KILL $pids && ok || false
		fi
	fi
}

RETVAL=0

# See how we were called.
case "$1" in
start)
	if [ "$2" ]; then
		svc=$(svcget /service/qmail-$2)
	else
		svc=$(svcget /service/qmail-{smtpd,send,qmqpd,qmtpd})
	fi
	echo "Starting qmail mta ($svc)..."
	svc -u $svc

	if [ "$2" ]; then
		svc=$(svcget /service/qmail-$2/log)
	else
		svc=$(svcget /service/qmail-{smtpd,send,qmqpd,qmtpd}/log)
	fi
	echo "Starting qmail mta logging ($svc) ..."
	svc -u $svc
;;

stop)
	if [ "$2" ]; then
		svc=$(svcget /service/qmail-$2)
	else
		svc=$(svcget /service/qmail-{smtpd,send,qmqpd,qmtpd})
	fi
	echo "Stopping qmail mta ($svc)..."
	svc -d $svc

	if [[ $svc = *send* ]]; then
		wait_for_proc qmailr qmail-remote
		wait_for_proc qmails qmail-send
	fi

	if [ "$2" ]; then
		svc=$(svcget /service/qmail-$2/log)
	else
		svc=$(svcget /service/qmail-{smtpd,send,qmqpd,qmtpd}/log)
	fi
	echo "Stopping qmail mta logging ($svc)..."
	svc -d $svc
;;

hold|unhold)
	hold "$1" "$2"
;;

restart)
	$0 stop
	$0 start
;;

reload)
	echo "Reloading 'locals', 'virtualdomains' and concurrency control files."
	svc -h /service/qmail-send
;;

queue)
	qmail-qstat
	qmail-qread
;;

*)
	msg_usage "$0 {start|stop|restart|reload|status|queue|hold|unhold}"
	exit 3
esac

exit $RETVAL
