#! /bin/bash
#
# Copyright (c) 1999-2009, Port25 Solutions, Inc. All Rights Reserved.
#
# $Id$
#
# chkconfig: 2345 80 30
# description: Starts and stops the PowerMTA ESMTP mailer
#
### BEGIN INIT INFO
# Provides: pmta
# 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: Port25's Message Transfer Agent (MTA)
# Description: PowerMTA is an extremely fast Message Transfer Agent (MTA).
### END INIT INFO
#
PID_FILE=/var/run/pmta.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


set_limits() {
    OLIMIT=$(ulimit -Hn)
    if [ "$OLIMIT" != "unlimited" ] && [ "$OLIMIT" -lt 32768 ] ; then
        # raise RLIMIT_NOFILE (max file descriptors)
        ulimit -Hn 32768
    fi

    HARD_PROC_LIMIT=-Hu
    OLIMIT=$(ulimit $HARD_PROC_LIMIT)
    if [ "$OLIMIT" != "unlimited" ] && [ "$OLIMIT" -lt 16384 ] ; then
        # raise RLIMIT_NPROC (max user processes)
        ulimit $HARD_PROC_LIMIT 16384
    fi
}


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

        set_limits

        # Tell PowerMTA to use 'ioctl' rather than 'getifaddrs' for detecting
        # local IP addresses.  This is normally only needed for older kernels
        #export PMTA_USE_GETIFADDRS=0

        # Use pmtawatch rather than 'daemon' or 'startproc', as the latter may
        # not catch some startup errors
        /usr/sbin/pmtawatch --start pmtad >/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: " /usr/sbin/pmtawatch --stop pmtad
                RETVAL=$?
                [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/pmta
                ;;
            suse)
                echo -n "Shutting down PowerMTA "
                /usr/sbin/pmtawatch --stop pmtad
                rc_status -v
                ;;
            debian|generic)
                echo -n "Stopping PowerMTA: "
                /usr/sbin/pmtawatch --stop pmtad
                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
        ;;
    force-reload|reload)
        case "$VERSION" in
            redhat)
                echo -n "Reloading configuration:"
                /usr/sbin/pmta reload &> /dev/null
                RETVAL=$?
                echo
                ;;
            suse)
                echo -n "Reload service PowerMTA "
                /usr/sbin/pmta reload &> /dev/null
                rc_status -v
                ;;
            debian|generic)
                /usr/sbin/pmta reload &> /dev/null
                RETVAL=$?
                ;;
        esac
        ;;
    status)
        case "$VERSION" in
            redhat)
                status pmtad
                RETVAL=$?
                ;;
            suse)
                echo -n "Checking for service PowerMTA "
                /sbin/checkproc /usr/sbin/pmtad
                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|force-reload|reload}"
        exit 2
        ;;
esac

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

