#!/bin/sh
# Courier IMAP SSL Daemon
#
# chkconfig:	345 80 20
# description:	Courier IMAP SSL Daemon

sysconfdir=/etc/courier-imap
libexecdir=/usr/libexec/courier-imap
sbindir=/usr/sbin

# Source function library
. /etc/rc.d/init.d/functions

# Get network config
. /etc/sysconfig/network

# Get service config
[ -f /etc/sysconfig/courier-imap-ssl ] && . /etc/sysconfig/courier-imap-ssl

# Check that networking is up.
if is_yes "${NETWORKING}"; then
	if [ ! -f /var/lock/subsys/network -a "$1" != stop -a "$1" != status ]; then
		msg_network_down "Courier IMAP SSL"
		exit 1
	fi
else
	exit 0
fi

start() {
	# Check if the service is already running?
	if [ -f /var/lock/subsys/courier-imap-ssl ]; then
		msg_already_running "Courier IMAP SSL"
		return
	fi

	if [ -x "$COURIERTLS" -a ! -f "$TLS_CERTFILE" ]; then
		msg_starting "Courier IMAP SSL cert generation"
		daemon $sbindir/mkimapdcert
	fi
	msg_starting "Courier IMAP SSL"
	daemon $libexecdir/imapd-ssl.rc start
	RETVAL=$?
	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/courier-imap-ssl
}

stop() {
	if [ ! -f /var/lock/subsys/courier-imap-ssl ]; then
		msg_not_running "Courier IMAP SSL"
		return
	fi

	msg_stopping "Courier IMAP SSL"
	daemon $libexecdir/imapd-ssl.rc stop
	RETVAL=$?
	rm -f /var/lock/subsys/courier-imap-ssl >/dev/null 2>&1
}

condrestart() {
	if [ ! -f /var/lock/subsys/courier-imap-ssl ]; then
		msg_not_running "Courier IMAP SSL"
		RETVAL=$1
		return
	fi

	stop
	start
}

RETVAL=0
# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  restart)
	stop
	start
	;;
  force-reload)
	condrestart 7
	;;
  status)
	# FIXME: matches other services, like courier-imap
	status couriertcpd
	exit $?
	;;
*)
	msg_usage "$0 {start|stop|restart|force-reload|status}"
	exit 3
esac

exit $RETVAL
