#!/bin/sh
##############################################################################
# cdate: TR-2003-07-07
# mdate: TR-2004-10-24
#
# blurb:
# - this is a small qmail control script
# - every admin should add his own personal stuff!
#
# notes:
# - automatically detects qmail-big-todo patched installations
# - can be used with minit, see http://www.fefe.de/minit/ for more info
# - can be used with svc, see http://cr.yp.to/daemontools/svc.html
# - needs a qmail.lst file, with the names of the services
# - if you have some improvements or corrections, just mail to:
#   -> milky-qmail at mcmilk dot de
#
# requires:
# - cat, sh, exit, grep, test, echo, killall, tail, ls, ps, rm, basename
# - msvc or svc + qmail + daemontools
##############################################################################

# this is my version :)
version="v0.7 - 2004-10-24 / TR"

# minit is the first try
MINIT="/etc/minit/qmail"

# if minit isn't available, we try svc from daemontools
INIT="/service"

# list of all services, which belong to qmail
# -> qmail-pop3d, qmail-send, qmail-smtpd, qmail-qmtpd ...
LIST="/var/qmail/qmail.lst"

Q_QUEUE="/var/qmail/queue"
Q_SEND="send"
Q_LOG="/var/qmail/log"
Q_LINES="20"

alias echo='echo -e'

function qmailhelp() {
 cat << EOF
usage: $0 (start|stop|restart|status|q-show|...)

qmail service commands:
start   - start qmail
stop    - stop qmail
restart - stop + start qmail
status  - show status of qmail (svc / msvc)
kill    - stop + kill -9 all qmail related processes

qmail queue commands:
q-stat  - show output of qmail-qstat (status of queue)
q-read  - show output of qmail-qread (remote todo)
q-list  - show all messages from queue
q-head  - head msg files from queue ($Q_LINES lines)
q-tail  - tail msg files from queue ($Q_LINES lines)
q-flush - flush qmail queue
q-rm ID - remove message ID from qmail queue
q-RM    - remove all messages from qmail queue

qmail logfile commands:
l-list  - show a list all found logfiles
l-tail  - show last lines of all logs
l-tailf - tail -f all the logs
l-tails - tail -f all the current qmail-smtpd logs
l-tailS - tail -f all the current qmail-send logs
l-tailp - tail -f all the current qmail-pop3 logs

qmail process commands:
p-stat  - show all processes of qmail releated progs

($version)
EOF
 exit
}

function qmailtail() {
  test "$1" = "l-tail"  && T="$Q_LOG/*/current"
  test "$1" = "l-tailf" && T="-f $Q_LOG/*/current"
  test "$1" = "l-tails" && T="-f $Q_LOG/smtp*/current"
  test "$1" = "l-tailS" && T="-f $Q_LOG/send/current"
  test "$1" = "l-tailp" && T="-f $Q_LOG/*pop*/current"
  tail $T | tai64nlocal
}

function qmailkill() {
  $0 stop
  echo "Hard RESET of services ..."
  sleep 1
  killall -9 qmail-send
  killall -9 qmail-popup
  killall -9 qmail-pop3d
  killall -9 qmail-remote
  killall -9 qmail-smtpd
  killall -9 qmail-lspwan
  killall -9 qmail-rspwan
}

function qmaildo() {
 test -z "$1" && exit 1
 test -d $Q_QUEUE/todo/0 && BIG='/*'
 for m in \
  $Q_QUEUE/info/*/* \
  $Q_QUEUE/local/*/* \
  $Q_QUEUE/remote/*/* \
  $Q_QUEUE/mess/*/* \
  $Q_QUEUE/bounce/* \
  $Q_QUEUE/intd/*$BIG \
  $Q_QUEUE/todo/*$BIG; do
  test -e "$m" || continue
  test "$1"  = "q-list"  && echo $m
  test "$1"  = "q-head"  && echo "msg:$m:head:"  && (head -$Q_LINES $m;echo "\n")
  test "$1"  = "q-tail"  && echo "msg:$m:tail:"  && (tail -$Q_LINES $m;echo "\n")
  test "$1"  = "q-RM"    && echo "msg:$m:deleted!" && rm -f $m
  test "$1"  = "q-rm"    && test "`basename $m`" = "$2" && echo "msg:$m:deleted!" && rm -f $m
 done
}

function qmailrunMSVC() {
case $1 in
start)
 qmail-newu
 for i in `cat $LIST`; do
  echo "Starte $i ..."
  msvc -u "$MINIT/$i"
  msvc -u "$MINIT/$i/log"
 done
 ;;
stop)
 for i in `cat $LIST`; do
  echo "Stoppe $i ..."
  msvc -d "$MINIT/$i"
  msvc -d "$MINIT/$i/log"
  msvc -C "$MINIT/$i"
  msvc -C "$MINIT/$i/log"
 done
 ;;
status)
 for i in `cat $LIST`; do
  msvc "$MINIT/$i"
 done
 for i in `cat $LIST`; do
  msvc "$MINIT/$i/log"
 done
 ;;
restart)
 $0 stop
 $0 start
 ;;
q-flush)
 echo "Flushe qmail-queue ..."
 msvc -a $MINIT/$Q_SEND
 ;;
esac
}

function qmailrunSVC() {
case "$1" in
 start)
 for i in `cat $LIST`; do
  echo "Starte $i ..."
  svc -u $INIT/$i
  svc -u $INIT/$i/log
 done
 ;;
 stop)
 for i in `cat $LIST`; do
  echo "Stoppe $i ..."
  svc -d $INIT/$i
  svc -d $INIT/$i/log
 done
 ;;
restart)
 $0 stop
 $0 start
 ;;
status)
 for i in `cat $LIST`; do
  svstat $INIT/$i
  svstat $INIT/$i/log
 done
 ;;
q-flush)
 echo "Flushe qmail-queue ..."
 svc -a $INIT/$Q_SEND
 ;;
esac
}

case $1 in
start|stop|restart|status|q-flush)
  if [ -d "$MINIT" ]; then
    qmailrunMSVC $1
    exit 0
  fi
  if [ -d "$INIT" ]; then
    qmailrunSVC $1
    exit 0
  fi
  ;;
kill)
  qmailkill
  ;;
q-list|q-tail|q-head|q-rm|q-RM)
  qmaildo $*
  ;;
q-stat)
  qmail-qstat
  ;;
q-read)
  qmail-qread
  ;;
l-tail|l-tailf|l-tails|l-tailS|l-tailp)
  clear
  qmailtail $*
  ;;
l-list)
  ls -1 $Q_LOG/*/* | grep -vE '(state|lock)'
  ;;
p-stat)
  ps uaxw|grep -E '(qmail-)' | grep -v grep
  ;;
*)
  qmailhelp
esac
