#!/bin/sh
#
# exim          This shell script takes care of starting and stopping
#               exim.
#
# chkconfig: 2345 80 30
# description: Exim is a Mail Transport Agent, which is the program \
#              that moves mail from one machine to another.
# processname: exim
# config: /etc/exim.conf
# pidfile: /var/run/exim.pid

# RedHat or Suse?
if [ -e "/etc/SuSE-release" ]; then
    SUSE=1
    DAEMONIZE=''
    RESULT='done'
else
    REDHAT=1
    DAEMONIZE='daemon'
    RESULT=''
fi

if [ -x "/usr/local/cpanel/3rdparty/bin/clamd" ]; then
    CLAMD=/usr/local/cpanel/3rdparty/bin/clamd
else
    CLAMD=/usr/sbin/clamd
fi

if [ "$REDHAT" = "1" ]; then
    # Source function library.
    if [ -e "/etc/init.d/functions" ]; then
        . /etc/init.d/functions
    else
        if [ -e "/etc/rc.d/init.d/functions" ]; then
        . /etc/rc.d/init.d/functions
        fi
    fi

    # Source networking configuration
    if [ -f /etc/sysconfig/network ]; then
        . /etc/sysconfig/network
    fi

    # Networking check is different for SuSE so moved check here
    # Check that networking is up for Red Hat
    [ ${NETWORKING} = "no" ] && exit 0
fi

if [ "$SUSE" = "1" ]; then
    # Source rc status functions
    . /etc/rc.status

    # Source networking functions
    #. /etc/sysconfig/network/scripts/functions

fi




DAEMON=yes
QUEUE=1h

# Source exim configureation.
if [ -f /etc/sysconfig/exim ] ; then
    . /etc/sysconfig/exim
fi

LANG=C
export LANG


[ -f /usr/sbin/exim ] || exit 0

RETVAL=0
# See how we were called.
case "$1" in
  start)
    # Start daemons.

    # EXIM
    if [ ! -e "/etc/eximdisable" ]; then
        if [ -x "$CLAMD" -a ! -e "/etc/clamddisable" ]; then
            echo -n "Starting clamd: "
                rm -f /var/clamd
                $DAEMONIZE $CLAMD
            echo
        fi
        PERL_AT_STARTUP=""
        if [ ! -e "/var/cpanel/conserve_memory" ]; then
              PERL_AT_STARTUP="-ps"
        fi
        if [ -e "/etc/exim_outgoing.conf" ]; then
            echo -n "Starting exim: "
            TMPDIR=/tmp $DAEMONIZE /usr/sbin/exim $([ "$DAEMON" = yes ] && echo $PERL_AT_STARTUP -bd) \
                                                    -oP /var/spool/exim/exim-daemon.pid
            echo $RESULT
            echo -n "Starting exim-outgoing: "
                    TMPDIR=/tmp $DAEMONIZE /usr/sbin/exim $([ "$DAEMON" = yes ] && echo -C) \
                                      $(echo /etc/exim_outgoing.conf) \
                                      $([ -n "$QUEUE" ] && echo -q$QUEUE) \
                                      -oP /var/spool/exim/exim-outgoing.pid
            echo $RESULT
        else
            echo -n "Starting exim: "
            TMPDIR=/tmp $DAEMONIZE /usr/sbin/exim $([ "$DAEMON" = yes ] && echo $PERL_AT_STARTUP -bd) \
                                          $([ -n "$QUEUE" ] && echo -q$QUEUE) \
                                          -oP /var/spool/exim/exim-daemon.pid
            echo $RESULT
        fi
    fi

    # SPAMD
    if [ -x "/usr/local/cpanel/scripts/restartsrv_spamd" ]; then
        echo -n "Starting spamd: "
        $DAEMONIZE /usr/local/cpanel/scripts/restartsrv_spamd --no-verbose
        echo $RESULT
    else
        if [ ! -e "/etc/spamdisable" ]; then
            if [ -e "/etc/cpspamd.conf" ]; then
                . /etc/cpspamd.conf
            fi
            if [ -x "/usr/bin/spamd" ]; then
                echo -n "Starting spamd: "
                $DAEMONIZE /usr/bin/spamd -d ${local+"--local"} --allowed-ips=${allowedips-127.0.0.1} \
                  ${timeouttcp+"--timeout-tcp="}${timeouttcp} ${timeoutchild+"--timeout-child="}${timeoutchild} \
                  --pidfile=${pidfile-"/var/run/spamd.pid"} --max-children=${maxchildren=5} \
                  ${maxconnperchild+"--max-conn-per-child="}${maxconnperchild} \
                  ${maxspare+"--max-spare="}${maxspare}
                echo $RESULT
            fi
        fi
    fi
    touch /var/lock/subsys/exim
    ;;
  stop)
    # Stop daemons.
    if [ -x "$CLAMD" ]; then
        [ ! -e "/etc/clamddisable" ] && echo -n "Shutting down clamd: "
        killproc clamd
        [ ! -e "/etc/clamddisable" ] && echo $RESULT
    fi
    echo -n "Shutting down exim: "
    killproc exim
    echo $RESULT
    echo -n "Shutting down spamd: "
    killproc spamd
    echo $RESULT
    rm -f /var/lock/subsys/exim
    ;;
  reload)
	[ $UID -eq 0 ] || exit 4
	echo -n "Reloading exim: "
	killproc exim -HUP
	RETVAL=$?
	echo
    ;;
  restart)
    $0 stop
    $0 start
    ;;
  status)
    if [ "$SUSE" = "1" ]; then
        echo -n exim
        rc_status -v
    else
        status exim
    fi
    ;;
  *)
    echo "Usage: exim {start|stop|restart|reload|status}"
    exit 1
esac

exit $RETVAL

