#!/bin/sh
version="v0.4 - 2004-01-30 / TR / aspire release"
# cdate: 2003-07-07
# mdate: 2004-06-30
#
# blurb:
# - this is a small qmail-control script
# - it does serveral nice things by now
# - every admin should add his own personal stuff
#
# test -z $NOTTY && B="\e[1m"
# test -z $NOTTY && N="\e[0m"

alias echo='echo -e'
SVC="/etc/minit/Q/qmail.lst"
QQ="/var/qmail/queue"
INIT="/etc/minit/Q"

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-del   - delete all msg's from queue (restart qmail)
q-stat  - show output of qmail-qstat
q-flush - flush 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() {
  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
  qmail restart
}

function qmaildo() {
 test -z "$1" && exit 1
 for m in \
  $QQ/info/*/* \
  $QQ/local/*/* \
  $QQ/remote/*/* \
  $QQ/mess/*/* \
  $QQ/bounce/* \
  $QQ/intd/*/* \
  $QQ/todo/*/*; do
  test -e "$m" || continue
  echo -n "${B}msg:$m"
  test "$1" = "q-del"   && echo ":deleted!${N}" && rm -f $m
  test "$1" = "q-head"  && echo ":head:${N}"  && (head -20 $m;echo "\n")
  test "$1" = "q-tail"  && echo ":tail:${N}"  && (tail -20 $m;echo "\n")
  test "$1" = "q-list"  && echo "${N}"
 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 $SVC`; do
  echo "Starte $i ..."
  test -d "$INIT/$i"     && msvc -u Q/$i
  test -d "$INIT/$i/log" && msvc -u Q/$i/log
 done
 ;;
stop)
 for i in `cat $SVC`; do
  echo "Stoppe $i ..."
  test -d "$INIT/$i"     && msvc -d Q/$i
  test -d "$INIT/$i/log" && msvc -d Q/$i/log
  test -d "$INIT/$i"     && msvc -C Q/$i
  test -d "$INIT/$i/log" && msvc -C Q/$i/log
 done
 ;;
status)
 for i in `cat $SVC`; do
  test -d "$INIT/$i"     && msvc Q/$i
 done
 for i in `cat $SVC`; do
  test -d "$INIT/$i/log" && msvc Q/$i/log
 done
 ;;
restart)
 $0 stop
 $0 start
 ;;
esac
}

case $1 in
start|stop|restart|status)
  test -d /etc/minit && qmailrunMSVC $1
  ;;
kill)
  qmailkill
  ;;
q-list|q-del|q-tail|q-head)
  qmaildo $1
  ;;
q-stat)
  qmail-qstat
  ;;
q-flush)
  killall -1 qmail-send
  ;;
l-tail|l-tailf|l-tails|l-tailS|l-tailp)
  clear
  qmailtail $1
  ;;
l-list)
  ls -1 /var/log/qmail/*/* /var/log/mail* | grep -vE '(state|lock)'
  ;;
p-stat)
  ps uaxw|grep qmail
  ;;
*)
  qmailhelp
esac
