#!/bin/sh
#
# chkconfig: 345 55 45
# description: The qmail service provides a Mail Transport Agent (MTA)
#	replacement for sendmail or other mta programs.
#

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

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0

[ -f /var/qmail/bin/sendmail ] || exit 0

qmail_start(){
    # Start daemons.
    echo -n "Starting qmail: "
    if ! status qmail-send > /dev/null; then
	PATH="/usr/bin/qmail:$PATH"; export PATH
	(qmail-start "`cat /etc/qmail/dot-qmail`" splogger qmail &) \
	    && success || failure
	echo
	touch /var/lock/subsys/qmail
    else
	echo -n "Already running."
	failure
	echo
    fi
}

qmail_stop(){
    # Stop daemons.
    echo -n "Shutting down qmail: "
    if [ -f /var/lock/subsys/qmail ]; then
	killproc qmail-send
	echo
	rm -f /var/lock/subsys/qmail
    else
	echo -n "Not running."
	failure
	echo
    fi
}

qmail_restart(){
    if [ -f /var/lock/subsys/qmail ]; then
	qmail_stop
	sleep 2
    fi
    qmail_start
}

qmail_flush(){
    # Flush queue
    echo -n "Flushing mail queue: "
    if [ -f /var/lock/subsys/qmail ]; then
	killproc qmail-send -ALRM
	echo
    else
	echo -n "Not running."
	failure
	echo
    fi
}


# See how we were called.
case "$1" in
  start)
	qmail_start
	;;
  stop)
	qmail_stop
	;;
  restart)
	qmail_restart
        ;;
  flush)
	qmail_flush
        ;;
  status)
	status qmail-send
	;;
  *)
	echo "Usage: qmail {start|stop|restart|flush|status}"
	exit 1
esac

exit 0
