#!/bin/sh
##############################################################################
# cdate: TR-2003-07-07
# mdate: TR-2004-06-16
#
# blurb:
# - this is a small qmail control script
# - every admin should add his own personal stuff!
#
# warning:
# - you need the qmail big-todo patch for this script!!
##############################################################################

version="v0.5 - 2004-06-16 / TR"

MINIT="/etc/minit/qmail"
INIT="/service"
LIST="/var/qmail/qmail.lst"
QQ="/var/qmail/queue"
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 deamons

qmail queue commands:
q-list  - show all messages from queue
q-head  - head msg files from queue
q-tail  - tail msg files from queue
q-stat  - show output of qmail-qstat
q-flush - flush qmail queue
q-del   - delete all msg's from queue + restart qmail
q-rm ID - remove message ID 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="/var/log/qmail/*/current"
  test "$1" = "l-tailf" && T="-f /var/log/qmail/*/current"
  test "$1" = "l-tails" && T="-f /var/log/qmail/smtp*/current"
  test "$1" = "l-tailS" && T="-f /var/log/qmail/send/current"
  test "$1" = "l-tailp" && T="-f /var/log/qmail/*pop*/current"
  tail $T | tai64nlocal
}

function qmailkill() {
  $0 stop
  echo "Hard RESET of services ..."
  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
  $0 start
}

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

# SVC should be added also
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/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/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-del|q-tail|q-head|q-rm)
  qmaildo $*
  ;;
q-stat)
  qmail-qstat
  ;;
l-tail|l-tailf|l-tails|l-tailS|l-tailp)
  clear
  qmailtail $*
  ;;
l-list)
  ls -1 /var/log/qmail/*/* /var/log/mail* | grep -vE '(state|lock)'
  ;;
p-stat)
  ps uaxw|grep -E '(qmail-)' | grep -v grep
  ;;
*)
  qmailhelp
esac
