#! /bin/sh -e

sysconfdir="/etc/courier"
sbindir="/usr/sbin"
TCPD=${sbindir}/couriertcpd
DAEMONPROG=${sbindir}/esmtpd-ssl

test -f $DAEMONPROG || exit 0

# check for couriertcpd
if ! [ -x $TCPD ]; then
  exit 0
fi

# check for couriertls
if ! [ -x $COURIERTLS ]; then
  exit 0
fi

# check for esmtpd configuration file
if ! [ -f ${sysconfdir}/esmtpd ]; then
  exit 0
fi

# check for esmtpd-ssl configuration file
if ! [ -f ${sysconfdir}/esmtpd-ssl ]; then
  exit 0
fi

. ${sysconfdir}/esmtpd
. ${sysconfdir}/esmtpd-ssl

. /lib/lsb/init-functions

case "$1" in
start)
	log_begin_msg "Starting Courier SMTP/SSL server..."
	${sbindir}/esmtpd-ssl start || log_end_msg 1
	log_end_msg 0
	;;
stop)
	log_begin_msg "Stopping Courier SMTP/SSL server..."
   	${sbindir}/esmtpd-ssl stop || log_end_msg 1
   	log_end_msg 0
	;;
restart | reload | force-reload)
    $0 stop
    $0 start
    ;;
*)
  log_success_msg "Usage: $0 {start|stop|restart|reload|force-reload}"
  exit 1
  ;;
esac
exit 0
