#! /bin/sh
#
# Copyright (c) 1999-2009, Port25 Solutions, Inc. All Rights Reserved.
#
# $Id$
#
# chkconfig: 2345 80 30
# description: Starts and stops the PowerMTA web monitor
#
### BEGIN INIT INFO
# Provides: pmtahttp
# Required-Start: $network $named $syslog $time $remote_fs
# Should-Start:
# Required-Stop:
# Should-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: pmtahttp is the web monitor for PowerMTA.
# Description: pmtahttp is the web monitor for PowerMTA.
### END INIT INFO
#
PID_FILE=/var/run/pmtahttp.pid

RETVAL=0


get_status() {
    if [ -f $PID_FILE ] ; then
        PID=`cat $PID_FILE`
        if [ -n "`ps --pid $PID --noheader`" ]; then
            # running
            RET=0
        else
            # not running but pid file exists
            RET=1
        fi
    else
        # stopped
        RET=3
    fi
    return $RET
}



# detect the version of linux
VERSION=generic
if [ -f /etc/redhat-release ] ; then
    VERSION=redhat
    . /etc/rc.d/init.d/functions
elif [ -f /etc/SuSE-release ] ; then
    VERSION=suse
    . /etc/rc.status
    rc_reset
elif [ -f /etc/debian_version ] ; then
    VERSION=debian
    if [ ! -f /usr/sbin/pmtawatch ]; then 
        exit 0
    fi
fi


case "$1" in
    start)
        if [ "$VERSION" = "suse" ] ; then
            echo -n "Starting PowerMTA web monitor "
        else
            echo -n "Starting PowerMTA web monitor: "
        fi

        # Use pmtawatch rather than 'daemon' or 'startproc', as the latter may
        # not catch some startup errors
        /usr/sbin/pmtawatch --start pmtahttpd >/dev/null 2>&1 </dev/null

        case "$VERSION" in
            redhat)
                RETVAL=$?
                [ $RETVAL -eq 0 ] && success "$text" || failure "$text"
                [ $RETVAL -eq 0 ] && touch /var/lock/subsys/pmta
                echo
                ;;
            suse)
                rc_status -v
                ;;
            debian|generic)
                RETVAL=$?
                [ $RETVAL -eq 0 ] && echo "done." || echo "failed."
                ;;
        esac
        ;;
    stop)
        case "$VERSION" in
            redhat)
                action "Stopping PowerMTA web monitor: " /usr/sbin/pmtawatch --stop pmtahttpd
                RETVAL=$?
                [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/pmta
                ;;
            suse)
                echo -n "Shutting down PowerMTA web monitor "
                /usr/sbin/pmtawatch --stop pmtahttpd
                rc_status -v
                ;;
            debian|generic)
                echo -n "Stopping PowerMTA web monitor: "
                /usr/sbin/pmtawatch --stop pmtahttpd
                RETVAL=$?
                [ $RETVAL -eq 0 ] && echo "done." || echo "failed."
                ;;
        esac
        ;;
    try-restart|condrestart)
        $0 status
        if [ $? -eq 0 ] ; then
            $0 restart
        else
            if [ "$VERSION" = "suse" ] ; then
                rc_reset
            else
                RETVAL=0
            fi
        fi
        ;;
    restart)
        $0 stop
        $0 start

        RETVAL=$?
        if [ "$VERSION" = "suse" ] ; then
            [ $RETVAL -ne 0 ] && rc_failed
        fi
        ;;
    status)
        case "$VERSION" in
            redhat)
                status pmtahttpd
                RETVAL=$?
                ;;
            suse)
                echo -n "Checking for service PowerMTA web monitor "
                /sbin/checkproc /usr/sbin/pmtahttpd
                rc_status -v
                ;;
            debian|generic)
                get_status
                RETVAL=$?
                if [ $RETVAL = 0 ]; then
                    echo running                              
                else
                    echo stopped                              
                fi
                ;;
        esac
        ;;
    *)
        echo "Usage: $0 {start|stop|status|try-restart|restart}"
        exit 2
        ;;
esac

if [ "$VERSION" = "suse" ] ; then
    rc_exit
else
    exit $RETVAL
fi

