#!/bin/bash
### BEGIN INIT INFO
# Provides:          smtp
# Required-Start:    $network $local_fs
# Required-Stop:     $network $local_fs
# Default-Start:     3 4 5
# Default-Stop:      0 1 6
# Short-Description: qmail-smtp wrapper
### END INIT INFO
#
# Startup script for smtp
#
# chkconfig: 345 95 5
# description: smtp is a mail protocol.

# Source function library.
if [ -f /etc/init.d/functions ]; then
  . /etc/init.d/functions
elif [ -f /etc/rc.d/init.d/functions ]; then
  . /etc/rc.d/init.d/functions
fi

PATH=$PATH:/var/qmail/bin
export PATH

svc=/usr/bin/svc
svstat=/usr/bin/svstat
ret=0

prog=smtp
smtp="/service/smtp"
smtp2="/service/smtp2"
send="/service/send"

if [[ -f ~iworx/etc/env/msa && "$(cat ~iworx/etc/env/msa)" == "dovecot" ]]; then
    smtp2=""
fi

progdirs="$smtp $smtp2 $send"
smtpprocs="qmail-smtpd simscan"
sendprocs="qmail-remote qmail-remote.orig qmail-local deliverquota"
processes="$sendprocs $smtpprocs"

start() {
    local failcount=0
    ret=0
    for dir in $progdirs
    do
        if ! __svcstart $dir; then
            let failcount++
        fi
    done

    if [ $failcount -eq 0 ]; then
        touch /var/lock/subsys/smtp
    else
        ret=1
    fi
    
    return $ret
}

__svcstart() {
    local localret=0
    local dir=$1

    if [ -d $dir ]; then
        $svc -u $dir
        if [ $? -eq 0 ]; then
            sleep 1
            if __statprog $dir >/dev/null; then
                action "Starting $dir: " /bin/true
            else
                action "Starting $dir: " /bin/false
                localret=1
            fi
        else
            action "Starting $dir: " /bin/false
            localret=1
        fi
    fi

    return $localret
}

stop() {
    ret=0
    if __svcstop "$smtp $smtp2"; then
        action "Stopping $smtp: " /bin/true
        [ -d $smtp2 ] && action "Stopping $smtp2: " /bin/true
    else
        action "Stopping $smtp: " /bin/false
        [ -d $smtp2 ] && action "Stopping $smtp2: " /bin/false
        ret=1
    fi
        
    if __svcstop "$send"; then
        action "Stopping $send: " /bin/true
    else
        action "Stopping $send: " /bin/false
        ret=1
    fi

    rm -f /var/lock/subsys/smtp
    return $ret
}


__svcstop() {
    local localret=0
    local dirs=$1

    $svc -d $dirs &>/dev/null
    if [ $? -eq 0 ]; then
        sleep 1
        if __statprog $dirs >/dev/null; then
            if [ "$dirs" == "$smtp $smtp2" ]; then
                killall $smtpprocs &>/dev/null
                sleep 1
                killall -9 $smtprocs &>/dev/null
            else
                killall $sendprocs &>/dev/null
                sleep 1
                killall -9 $sendprocs &>/dev/null
            fi
        fi
    else
        localret=1
    fi

    return $localret
}

stat() {
    ret=0
    local deadprogs=0

    # iterate through dirs
    for dir in $progdirs
    do
            if ! __statprog $dir; then
                let deadprogs++
            fi
    done

    if [ $deadprogs -gt 0 ]; then
        ret=3
    fi


    return $ret
}

__statprog() {
    # 0 = running
    # 1 = dead, /var/run exists
    # 2 = /var/lock exists
    # 3 = dead
    # 4 = UNKNOWN
    local localret=0
    local runningcount=0
    local s=""
    dir=$1

    # if dir exists ( program is installed )
    if [ -d $dir ]; then

        # svstat & check for "up" in output text"
        s=`$svstat $dir`
        echo $s | grep -E "^$dir: up.*$" &>/dev/null

        # if svstat doesn't say it's up
        if [ $? -ne 0 ]; then
            localret=3 
            #depending on what service we are checking, we want to check to see if
            #its processes are running
            case $dir in
                # smtp and smtp2 services
                "$smtp" | "$smtp2")
                    for proc in $smtpprocs; do
                        # if pidof returns that process is running
                        if pidof $proc >/dev/null; then 
                            let runningcount++
                        fi
                    done
                ;;
                # send service
                "$send")
                    for proc in $sendprocs; do
                        # if pidof returns that process is running
                        if pidof $proc >/dev/null; then
                            let runningcount++
                        fi
                    done
                ;;
            esac
        fi
        # echo the svstat output
        echo $s
    fi

    # svstat may have reported "not up" but there are still processes running so we consider it up.
    if [ $runningcount -gt 0 ]; then
        localret=0
    fi

    return $localret
}

pause() {
    for dir in $progdirs
    do
        if [ -d $dir ]; then
            $svc -p $dir
        fi
    done
    ret=$?

    if [ $ret -eq 0 ]; then
        action "Pausing $prog: " /bin/true
    else
        action "Pausing $prog: " /bin/false
    fi
    
    return $ret
}

cont() {
    for dir in $progdirs
    do
        if [ -d $dir ]; then
            $svc -c $dir
        fi
    done
    ret=$?

    if [ $ret -eq 0 ]; then
        action "Continuing $prog: " /bin/true
    else
        action "Continuing $prog: " /bin/false
    fi
    
    return $ret
}

doqueue() {
    svc -a /service/send
    ret=$?

    if [ $ret -eq 0 ]; then
        action "Sending ALRM signal to qmail-send: " /bin/true
    else
        action "Sending ALRM signal to qmail-send: " /bin/false
    fi
    
    return $ret
}

reload() {
    svc -h /service/send
    ret=$?

    if [ $ret -eq 0 ]; then
        action "Sending HUP signal to qmail-send: " /bin/true
    else
        action "Sending HUP signal to qmail-send: " /bin/false
    fi
    
    return $ret
}

case "$1" in 
        start)
                start
                ;;
        stop)
                stop
                ;;
        restart)
                stop
                start
                ;;
        try-restart)
                if stat >/dev/null; then
                    stop
                    start
                fi
                ;;
        status)  
                stat
                ;;
        doqueue|alrm)
                doqueue
                ;;
        queue)
                qmail-qstat
                qmail-qread
                ;;
        reload|hup)
                reload
                ;;
        force-reload)
                stop
                start
                ;;
        pause)
                pause
                ;;
        cont)
                cont
                ;;
        *)
                echo $"Usage: $0 {start|stop|restart|try-restart|status|doqueue|queue|reload|force-reload|pause|cont}"
                exit 1
esac

exit $ret
