#!/bin/sh

# $Id: qmailq,v 1.4 1996/12/13 16:06:50 hanche Exp $
# Start, stop, hup, restart, check the qmail queue daemons.
# NOTE: qmail-smtpd is to be run from inetd; see /etc/inetd.conf.
# $Log: qmailq,v $
# Revision 1.4  1996/12/13 16:06:50  hanche
# Added alrm option and cleaned things up a little.
#
# Revision 1.3  1996/12/13 15:57:42  hanche
# Added an option to send the daemon a HUP signal.
#
# Revision 1.2  1996/12/13 15:43:11  hanche
# Use new feature of watch: Don't close stdout.
# This simplifies the running of accustamp.
#
# Revision 1.1  1996/12/13 15:34:51  hanche
# Initial revision
#

PATH=/var/qmail/bin:/usr/sbin:/usr/bin:/usr/ucb
export PATH
pidfile=/var/qmail/qmail-send.pid
times="5 5 10 10 30 30 60"

slumber ()
{
    Slumber $times
}

Slumber ()
{
    echo "qmail-send needs more time to finish.  Sleeping $1 seconds..."
    sleep $1
    test $# != 1 && shift
    times="$*"
}

getpid ()
{
    if test -r $pidfile; then
	pid=`cat $pidfile`
	if test -n $pid && kill -0 $pid; then
	    return 0
	else
	    unset pid; rm -f $pidfile; return 1
	fi
    else
	return 1
    fi
}

start ()
{
    if getpid; then
	echo Not starting qmail queue daemons: Already running.
	echo "(See $pidfile)"
	return 1
    else
	echo Starting qmail queue daemons.
	touch /var/log/qmail.log
	chown qmaill /var/log/qmail.log
	env - PATH=$PATH \
		# Use (a variation on) one of the following two:
		watch -1 $pidfile qmail-start accustamp >> /var/log/qmail.log
		# watch $pidfile qmail-start splogger
    fi
}

signal () 
{
    if getpid; then
	echo Sending qmail-send a $1 signal.
	kill -$1 $pid
    else
	echo No qmail queue daemons to signal.
    fi
}

restart ()
{
    signal TERM; sleep 1
    while getpid; do slumber; done
    start
}

check ()
{
    if getpid; then ps -fp $pid; else echo qmail-send not running; fi
}

case "$1" in
    start)   start ;;
    stop)    signal TERM
	     echo '(It may take a while for qmail-send to finish)'
	     ;;
    hup)     signal HUP ;;
    alrm)    signal ALRM ;;
    restart) restart ;;
    check)   check ;;
    *)
	echo "Usage: sh $0 start|stop|alrm|hup|restart|check"
	;;
esac
