#!/bin/bash
#
# Startup script for imap
#
# chkconfig: 345 95 5
# description: imap is a mail protocol.

if [[ -f ~iworx/etc/env/mda && "$(cat ~iworx/etc/env/mda)" == "dovecot" ]]; then
    service dovecot $1
    exit $?
fi

SYSTEMCTL_SKIP_REDIRECT=1

# 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

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

prog=imap4-ssl
progdirs="/service/imap4-ssl"

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

    if [ $failcount -gt 0 ]; then
        ret=1
    fi
    
    return $ret
}

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

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

    return $localret
}

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

    if [ $failcount -gt 0 ]; then
        ret=1
    fi
    
    return $ret
}

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

    if [ -d $dir ]; then
        $svc -d $dir
        if [ $? -eq 0 ]; then
            action "Stopping $dir: " /bin/true
        else
            action "Stopping $dir: " /bin/false
            localret=1
        fi
    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
            # unlike /etc/init.d/smtp its not critical that the
            # service is actualy dead dead
            localret=3 
        fi
        # echo the svstat output
        echo $s
    fi

    return $localret
}

restart() {
    stop
    sleep 1
    start
}


case "$1" in 
	start)
		start
		;;
	stop)
		stop
		;;
    restart)
        restart
		;;
    try-restart)
        if stat >/dev/null; then
            restart
        fi
        ;;
    reload)
        $svc -t $progdirs
        action "Reloading imap4-ssl: " /bin/true
        ret=0
        ;;
    force-reload)
        restart
        ;;
    status)  
	    stat
		;;
	*)
		echo $"Usage: $0 {start|stop|restart|try-restart|reload|force-reload|status}"
		exit 1
esac

exit $ret
