#!/bin/sh
#
# Author: Simon Horman <horms@verge.net.au>
#
### BEGIN INIT INFO
# Provides:          perdition
# Required-Start:    $remote_fs
# Should-Start:      $syslog $named
# Required-Stop:     $remote_fs
# Should-Stop:       $syslog $named
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: POP and IMAP proxy
# Description:       Starts perdition to allow proxied POP and IMAP access
### END INIT INFO

PATH=/usr/sbin:/bin:/usr/bin:/sbin:/usr/sbin
NAME=perdition
DAEMON=/usr/sbin/perdition

test -f $DAEMON || exit 0

if [ -e /etc/default/perdition ]; then
	. /etc/default/perdition
fi

# Please do not edit the values below.
# Rather, please edit /etc/default/perdition
RUN_PERDITION="${RUN_PERDITION:=no}"
FLAGS="${FLAGS:=}"
POP3="${POP3:=yes}"
POP3_FLAGS="${POP3_FLAGS:=}"
POP3S="${POP3S:=yes}"
POP3S_FLAGS="${POP3S_FLAGS:=}"
IMAP4="${IMAP4:=yes}"
IMAP4_FLAGS="${IMAP4_FLAGS:=}"
IMAP4S="${IMAP4S:=yes}"
IMAP4S_FLAGS="${IMAP4S_FLAGS:=}"

if [ "$RUN_PERDITION" != "yes" ]; then
        exit 0
fi


case "$1" in
  start)
    if [ "$POP3" = "yes" ]; then 	
      start-stop-daemon --start --verbose \
        --pidfile /var/run/perdition.pop3/perdition.pop3.pid \
        --exec /usr/sbin/perdition.pop3 -- $FLAGS $POP3_FLAGS
        if [ ! -e /var/run/perdition.pop3/perdition.pop3.pid ]; then
                echo "Unable to start POP3 Daemon (maybe another process is listening to the same port?)"
        fi

    fi
    if [ "$POP3S" = "yes" ]; then 	
      start-stop-daemon --start --verbose \
        --pidfile /var/run/perdition.pop3s/perdition.pop3s.pid \
        --exec /usr/sbin/perdition.pop3s -- $FLAGS $POP3S_FLAGS
        if [ ! -e /var/run/perdition.pop3s/perdition.pop3s.pid ]; then
                echo "Unable to start POP3S Daemon (maybe another process is listening to the same port?)"
        fi
    fi
    if [ "$IMAP4" = "yes" ]; then 	
      start-stop-daemon --start --verbose \
        --pidfile /var/run/perdition.imap4/perdition.imap4.pid \
        --exec /usr/sbin/perdition.imap4 -- $FLAGS $IMAP4_FLAGS
        if [ ! -e /var/run/perdition.imap4/perdition.imap4.pid ]; then
                echo "Unable to start IMAP4 Daemon (maybe another process is listening to the same port?)"
        fi

    fi
    if [ "$IMAP4S" = "yes" ]; then 	
      start-stop-daemon --start --verbose \
        --pidfile /var/run/perdition.imaps/perdition.imaps.pid \
        --exec /usr/sbin/perdition.imaps -- $FLAGS $IMAP4S_FLAGS
         if [ ! -e /var/run/perdition.imaps/perdition.imaps.pid ]; then
                 echo "Unable to start IMAP4S Daemon (maybe another process is listening to the same port?)"
         fi
    fi
    ;;
  stop)
    if [ "$POP3" = "yes" ]; then 	
      start-stop-daemon --stop --verbose \
        --pidfile /var/run/perdition.pop3/perdition.pop3.pid \
        --exec /usr/sbin/perdition.pop3
    fi
    if [ "$POP3S" = "yes" ]; then 	
      start-stop-daemon --stop --verbose \
        --pidfile /var/run/perdition.pop3s/perdition.pop3s.pid \
        --exec /usr/sbin/perdition.pop3s
    fi
    if [ "$IMAP4" = "yes" ]; then 	
      start-stop-daemon --stop --verbose \
        --pidfile /var/run/perdition.imap4/perdition.imap4.pid \
        --exec /usr/sbin/perdition.imap4
    fi
    if [ "$IMAP4S" = "yes" ]; then 	
      start-stop-daemon --stop --verbose \
        --pidfile /var/run/perdition.imaps/perdition.imaps.pid \
        --exec /usr/sbin/perdition.imaps
    fi
    ;;
  restart)
    $0 stop 
    $0 start 
    ;;
  force-reload|reload)
    echo "Reloading $NAME configuration files"
    if [ "$POP3" = "yes" ]; then 	
      start-stop-daemon --stop --signal 1 --quiet \
        --pidfile /var/run/perdition.pop3/perdition.pop3.pid \
        --exec /usr/sbin/perdition.pop3
    fi
    if [ "$POP3S" = "yes" ]; then 	
      start-stop-daemon --stop --signal 1 --quiet \
        --pidfile /var/run/perdition.pop3s/perdition.pop3s.pid \
        --exec /usr/sbin/perdition.pop3s
    fi
    if [ "$IMAP4" = "yes" ]; then 	
      start-stop-daemon --stop --signal 1 --quiet \
        --pidfile /var/run/perdition.imap4/perdition.imap4.pid \
        --exec /usr/sbin/perdition.imap4
    fi
    if [ "$IMAP4S" = "yes" ]; then 	
      start-stop-daemon --stop --signal 1 --quiet \
        --pidfile /var/run/perdition.imaps/perdition.imaps.pid \
        --exec /usr/sbin/perdition.imaps
    fi
    ;;
  *)
    echo "Usage: /etc/init.d/$NAME {start|stop|restart|reload|force-reload}"
    exit 1
    ;;
esac

exit 0
