#!/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
}

RETVAL=0

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

	svc=$(svcget /service/qmail-{smtpd,send,qmqpd,qmtpd}/log)
	echo "Starting qmail mta logging ($svc) ..."
	svc -u $svc
;;

stop)
    svc=$(svcget /service/qmail-{smtpd,send,qmqpd,qmtpd})
    echo "Stopping qmail mta ($svc)..."
    svc -d $svc

    svc=$(svcget /service/qmail-{smtpd,send,qmqpd,qmtpd}/log)
    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
