#!/bin/sh

# For Red Hat chkconfig
# chkconfig: - 30 80
# description: the qmail MTA

PATH=/var/qmail/bin:/bin:/usr/bin:/usr/local/bin:/usr/local/sbin
export PATH

QMAILDUID=`id -u qmaild`
NOFILESGID=`id -g qmaild`

#
# change these variables to decide whether pop, pops, imap and imaps 
# are controlled by qmailctl
#

POP3="yes"
POP3S="yes"
IMAP="yes"
IMAPS="yes"
MSA="yes"

###

case "$1" in
  start)
    echo "Starting qmail"
    if svok /service/qmail-send ; then
      svc -u /service/qmail-send /service/qmail-send/log
    else
      echo "qmail-send supervise not running"
    fi
    if svok /service/qmail-smtpd ; then
      svc -u /service/qmail-smtpd /service/qmail-smtpd/log
    else
      echo "qmail-smtpd supervise not running"
    fi
    if [ -d /var/lock/subsys ]; then
      touch /var/lock/subsys/qmail
    fi

    if [ $MSA = "yes" ] ; then
       if svok /service/qmail-msa ; then
        echo "Starting qmail-msa"
        svc -u /service/qmail-msa /service/qmail-msa/log
       else
        echo qmail-msa supervise not running
       fi
    fi

    if [ $POP3 = "yes" ] ; then
       if svok /service/qmail-pop3d ; then
        echo "Starting qmail-pop3d"
        svc -u /service/qmail-pop3d /service/qmail-pop3d/log
       else
        echo qmail-pop3d supervise not running
       fi
    fi

    if [ $POP3S = "yes" ] ; then
       if svok /service/qmail-pop3ds ; then
        echo "Starting qmail-pop3ds"
        svc -u /service/qmail-pop3ds /service/qmail-pop3ds/log
       else
        echo qmail-pop3ds supervise not running
       fi
    fi

    if [ $IMAP = "yes" ] ; then
       if svok /service/imap ; then
        echo "Starting imap"
        svc -u /service/imap /service/imap/log
       else
        echo bincimap supervise not running
       fi
    fi

    if [ $IMAPS = "yes" ] ; then
       if svok /service/imaps ; then
        echo "Starting imaps"
        svc -u /service/imaps /service/imaps/log
       else
        echo bincimaps supervise not running
       fi
    fi

    ;;
qstart)
    echo "Starting qmail"
    if svok /service/qmail-send ; then
      svc -u /service/qmail-send /service/qmail-send/log
    else
      echo "qmail-send supervise not running"
    fi

    if svok /service/qmail-smtpd ; then
      svc -u /service/qmail-smtpd /service/qmail-smtpd/log
    else
      echo "qmail-smtpd supervise not running"
    fi

    if svok /service/qmail-msa ; then
      svc -u /service/qmail-msa /service/qmail-msa/log
    else
      echo "qmail-msa supervise not running"
    fi
    
    if [ -d /var/lock/subsys ]; then
      touch /var/lock/subsys/qmail
    fi
    ;;
popstart)
    if [ $POP3 = "yes" ] ; then
       if svok /service/qmail-pop3d ; then
        echo "Starting qmail-pop3d"
        svc -u /service/qmail-pop3d /service/qmail-pop3d/log
       else
        echo qmail-pop3d supervise not running
       fi
    fi

    if [ $POP3S = "yes" ] ; then
       if svok /service/qmail-pop3ds ; then
        echo "Starting qmail-pop3ds"
        svc -u /service/qmail-pop3ds /service/qmail-pop3ds/log
       else
        echo qmail-pop3ds supervise not running
       fi
    fi
    ;;
imapstart)
    if [ $IMAP = "yes" ] ; then
       if svok /service/imap ; then
        echo "Starting imap"
        svc -u /service/imap /service/imap/log
       else
        echo bincimap supervise not running
       fi
    fi

    if [ $IMAPS = "yes" ] ; then
       if svok /service/imaps ; then
        echo "Starting imaps"
        svc -u /service/imaps /service/imaps/log
       else
        echo bincimaps supervise not running
       fi
    fi
    ;;
  stop)
    echo "Stopping qmail..."
    echo "  qmail-smtpd"
    svc -d /service/qmail-smtpd /service/qmail-smtpd/log
    echo "  qmail-send"
    svc -d /service/qmail-send /service/qmail-send/log
    if [ -f /var/lock/subsys/qmail ]; then
      rm /var/lock/subsys/qmail
    fi

    if [ $MSA = "yes" ] ; then
      echo "  qmail-msa"
      svc -d /service/qmail-msa /service/qmail-msa/log
    fi

    if [ $POP3 = "yes" ] ; then
      echo "  qmail-pop3d"
      svc -d /service/qmail-pop3d /service/qmail-pop3d/log
    fi

    if [ $POP3S = "yes" ] ; then
      echo "  qmail-pop3ds"
      svc -d /service/qmail-pop3ds /service/qmail-pop3ds/log
    fi

    if [ $IMAP = "yes" ] ; then
      echo "  bincimap"
      svc -d /service/imap /service/imap/log
    fi

    if [ $IMAPS = "yes" ] ; then
      echo "  bincimaps"
      svc -d /service/imaps /service/imaps/log
    fi
    ;;
 qstop)
    echo "Stopping qmail..."
    echo "  qmail-smtpd"
    svc -d /service/qmail-smtpd /service/qmail-smtpd/log
    
    echo "  qmail-send"
    svc -d /service/qmail-send /service/qmail-send/log
    
    echo "  qmail-msa"
    svc -d /service/qmail-msa /service/qmail-msa/log
    
    if [ -f /var/lock/subsys/qmail ]; then
      rm /var/lock/subsys/qmail
    fi
    ;;
popstop)
    if [ $POP3 = "yes" ] ; then
      echo "Stopping qmail-pop3d"
      svc -d /service/qmail-pop3d /service/qmail-pop3d/log
    fi

    if [ $POP3S = "yes" ] ; then
      echo "Stopping qmail-pop3ds"
      svc -d /service/qmail-pop3ds /service/qmail-pop3ds/log
    fi
    ;;
imapstop)
    if [ $IMAP = "yes" ] ; then
      echo "Stopping bincimap"
      svc -d /service/imap /service/imap/log
    fi

    if [ $IMAPS = "yes" ] ; then
      echo "Stopping bincimaps"
      svc -d /service/imaps /service/imaps/log
    fi
    ;;
  stat)
    svstat /service/qmail-send
    svstat /service/qmail-send/log
    svstat /service/qmail-smtpd
    svstat /service/qmail-smtpd/log

    if [ $MSA = "yes" ] ; then
      svstat /service/qmail-msa
      svstat /service/qmail-msa/log
    fi
    
    if [ $POP3 = "yes" ] ; then
      svstat /service/qmail-pop3d
      svstat /service/qmail-pop3d/log
    fi
    
    if [ $POP3S = "yes" ] ; then
      svstat /service/qmail-pop3ds
      svstat /service/qmail-pop3ds/log
    fi
    
    if [ $IMAP = "yes" ] ; then
      svstat /service/imap
      svstat /service/imap/log
    fi
    
    if [ $IMAPS = "yes" ] ; then
      svstat /service/imaps
      svstat /service/imaps/log
    fi

    qmail-qstat
    ;;
  qstat)
    svstat /service/qmail-send
    svstat /service/qmail-send/log
    svstat /service/qmail-smtpd
    svstat /service/qmail-smtpd/log
    svstat /service/qmail-msa
    svstat /service/qmail-msa/log
    ;;
  popstat)
    if [ $POP3 = "yes" ] ; then
      svstat /service/qmail-pop3d
      svstat /service/qmail-pop3d/log
    fi
    if [ $POP3S = "yes" ] ; then
      svstat /service/qmail-pop3d
      svstat /service/qmail-pop3d/log
    fi
    ;;
  imapstat)
    if [ $IMAP = "yes" ] ; then
      svstat /service/imap
      svstat /service/imap/log
    fi
    if [ $IMAPS = "yes" ] ; then
      svstat /service/imaps
      svstat /service/imaps/log
    fi
    ;;
  doqueue|alrm|flush)
    echo "Flushing timeout table and sending ALRM signal to qmail-send."
    /var/qmail/bin/qmail-tcpok
    svc -a /service/qmail-send
    ;;
  queue)
    qmail-qstat
    qmail-qread
    ;;
  reload|hup)
    echo "Sending HUP signal to qmail-send."
    svc -h /service/qmail-send
    ;;
  pause)
    echo "Pausing qmail-send"
    svc -p /service/qmail-send
    echo "Pausing qmail-smtpd"
    svc -p /service/qmail-smtpd

    if [ $POP3 = "yes" ] ; then
      echo "Pausing qmail-pop3d"
      svc -p /service/qmail-pop3d
    fi
    if [ $IMAP = "yes" ] ; then
      echo "Pausing bincimap"
      svc -p /service/imap
    fi
    if [ $IMAPS = "yes" ] ; then
      echo "Pausing bincimaps"
      svc -p /service/imaps
    fi
    ;;
  cont)
    echo "Continuing qmail-send"
    svc -c /service/qmail-send
    echo "Continuing qmail-smtpd"
    svc -c /service/qmail-smtpd

    if [ $POP3 = "yes" ] ; then
      echo "Continuing qmail-pop3d"
      svc -c /service/qmail-pop3d
    fi
    if [ $IMAP = "yes" ] ; then
      echo "Continuing bincimap"
      svc -c /service/imap
    fi
    if [ $IMAPS = "yes" ] ; then
      echo "Continuing bincimaps"
      svc -c /service/imaps
    fi
    ;;
  restart)
    echo "Restarting qmail:"
    echo "* Stopping qmail-smtpd."
    svc -d /service/qmail-smtpd /service/qmail-smtpd/log
    echo "* Sending qmail-send SIGTERM and restarting."
    svc -t /service/qmail-send /service/qmail-send/log
    echo "* Restarting qmail-smtpd."
    svc -u /service/qmail-smtpd /service/qmail-smtpd/log

    if [ $POP3 = "yes" ] ; then
      echo "* Restarting qmail-pop3d."
      svc -t /service/qmail-pop3d /service/qmail-pop3d/log
    fi
    if [ $IMAP = "yes" ] ; then
      echo "* Restarting bincimap."
      svc -t /service/imap /service/imap/log
    fi
    if [ $IMAPS = "yes" ] ; then
      echo "* Restarting bincimaps."
      svc -t /service/imaps /service/imaps/log
    fi
    ;;
  cdb)
    tcprules ~vpopmail/etc/tcp.smtp.cdb ~vpopmail/etc/tcp.smtp.tmp < ~vpopmail/etc/tcp.smtp
    chmod 644 ~vpopmail/etc/tcp.smtp.cdb
    echo "Reloaded ~vpopmail/etc/tcp.smtp."
    ;;
  help)
    cat <<HELP
      stop -- stops all mail services (smtp connections refused, nothing goes out)
     qstop -- stops mail service (smtp connections refused, nothing goes out)
   popstop -- stops pop3 and pop3s services
  imapstop -- stops imap and imaps services
     start -- starts all mail services (smtp connection accepted, mail can go out)
    qstart -- start all mail service (smtp connection accepted, mail can go out)
  popstart -- starts pop3 and pop3s services
 imapstart -- starts imap and imaps services
     pause -- temporarily stops mail service (connections accepted, nothing leaves)
      cont -- continues paused mail service
      stat -- displays status of all mail services
     qstat -- displays status of mail service
   popstat -- displays status of mail service
  imapstat -- displays status of mail service
       cdb -- rebuild the tcpserver cdb file for smtp
   restart -- stops and restarts smtp, sends qmail-send a TERM & restarts it
   doqueue -- schedules queued messages for immediate delivery
    reload -- sends qmail-send HUP, rereading locals and virtualdomains
     queue -- shows status of queue
      alrm -- same as doqueue
     flush -- same as doqueue
       hup -- same as reload
HELP
    ;;
  *)
    echo "Usage: $0 {start|stop|restart|doqueue|flush|reload|stat|pause|cont|cdb|queue|help}"
    exit 1
    ;;
esac

exit 0
