#!/bin/sh
# chkconfig: 2345 50 50
### BEGIN INIT INFO
# Provides:          exim
# Required-Start:    $network
# Required-Stop:     $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start exim at boot time
# Description:       Enable service provided by exim.
# X-Start-Before:    
# X-Stop-After:      
# X-Timesys-Start-Number:  50
# X-Timesys-Stop-Number:  50
### END INIT INFO

PATH=/usr/bin:/bin:/usr/sbin:/sbin
DAEMON=exim
# Run as SMTP listener daemon, do queue runs every 15 mins.
DAEMON_OPTIONS="-bd -q15m"

case "$1" in
    start)
        if ! grep ^exim /etc/passwd > /dev/null; then
            echo "$0: Creating system user 'exim' with generic settings.  Review and modify as required."
            yes '' | adduser exim >/dev/null 2>&1 || yes '' | useradd exim >/dev/null 2>&1
        fi
	PID=$(pidof exim)
        if [ "$PID" ]; then
        	echo "exim already started; not starting."
		exit
        fi
        mkdir -p /var/spool/exim
        chown -R exim:exim /var/spool/exim
        echo -n "Starting exim: "
        $DAEMON $DAEMON_OPTIONS && \
          echo '[ OK ]' || echo '[FAIL]'
    ;;
    stop)
	PID=$(pidof exim)
        if [ "$PID" ]; then
            echo -n "Stopping exim: "
            kill $PID && \
              echo '[ OK ]' || echo '[FAIL]'
        else
            echo "pid file not found... is exim running?"
        fi
    ;;
    restart)
        $0 stop
        $0 makedb
        $0 start
    ;;
    *)
        echo "usage: $0 {start|stop|makedb|restart}"
    ;;
esac
