#!/bin/sh
version="v0.3 - 2003-12-06 / TR"
#
# cdate: 2003-07-07
# mdate: 2003-12-06
#
# 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'
INIT="/service"
SVC="/service/qmail.lst"
QQ="/var/qmail/queue"

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

qmail service commands:
start   - starting qmail
stop    - for stopping qmail
restart - restarting qmail
status  - show status of the services
flush   - flush the mailqueue

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 then!)
q-stat  - show output of qmail-qstat

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

qmail process commands:
p-stat  - does a ps, greping for qmail

qmail antispam commands:
a-xxx   - xxx

qmail statistic commands:
s-xxx   - xxx

($version)
EOF
 exit
}

function qmailtail() {
 test "$1" = "l-tailf" && T="-f /var/log/qmail/*/current"
 test "$1" = "l-tails" && T="-f /var/log/qmail/smtpd/current"
 test "$1" = "l-tailS" && T="-f /var/log/qmail/send/current"
 test "$1" = "l-tailp" && T="-f /var/log/qmail/*pop*/current"
 # leave the millsecondstuff away!
 tail $T | tai64nlocal | cut -b1-19,30-
}

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
}

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 -k Q/$i
  test -d "$INIT/$i/log" && msvc -k 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
 ;;
flush)
 echo "flushing qmail-queue ..."
 msvc -a $INIT/send
 ;;
esac
}

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

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


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