#! /bin/sh
# Copyright (c) 2003 SuSE Linux AG, Nuernberg, Germany.
# All rights reserved.
#
# Author: Thorsten Kukuk <feedback@suse.de>
#
# /etc/init.d/courier-imap
#
### BEGIN INIT INFO
# Provides:          courier-pop3-ssl
# Required-Start:    $syslog $remote_fs courier-authdaemon
# X-UnitedLinux-Should-Start:
# Required-Stop:     $syslog $remote_fs
# X-UnitedLinux-Should-Stop:
# Default-Start:     3 5
# Default-Stop:      0 1 2 6
# Short-Description: Courier-POP3 server for SSL connections
# Description:       Start the Courier-IMAP POP3 server, which is an
#                    POP3 server for Maildir mailboxes. This version
#                    only accecpts SSL connections.
### END INIT INFO

# Check for missing binaries (stale symlinks should not happen)
POP3_BIN=/usr/sbin/pop3d
test -x $POP3_BIN || exit 5

. /etc/rc.status

# Read configuration
. /etc/courier/pop3d
. /etc/courier/pop3d-ssl

# Reset status of this service
rc_reset

case "$1" in
    start)
        /etc/init.d/courier-authdaemon status > /dev/null || /etc/init.d/courier-authdaemon start
	echo -n "Starting Courier-POP3 (ssl) "
	if test ! -f $TLS_CERTFILE
	then
		echo -n " generating-SSL-certificate..."
		/usr/sbin/mkpop3dcert >/dev/null 2>&1
	fi
	/usr/bin/env - /bin/sh -c " set -a ;
	    . /etc/courier/pop3d ; \
	    . /etc/courier/pop3d-ssl ; \
	    POP3_TLS=1; export POP3_TLS; \
	    POP3_STARTTLS=NO; export POP3_STARTTLS; \
	    POP3_TLS_REQUIRED=0; export POP3_TLS_REQUIRED; \
	    /usr/lib/courier-imap/couriertcpd -address=$SSLADDRESS \
		    -stderrlogger=/usr/sbin/courierlogger \
		    -stderrloggername=pop3d-ssl  -maxprocs=$MAXDAEMONS \
		    -maxperip=$MAXPERIP -pid=$SSLPIDFILE $TCPDOPTS \
		    $SSLPORT $COURIERTLS -server -tcpd /usr/sbin/pop3login \
		    /usr/lib/courier-imap/authlib/authdaemon \
		    $POP3_BIN Maildir"
	rc_status -v
	;;
    stop)
	echo -n "Shutting down Courier-POP3 (ssl) "
	/usr/lib/courier-imap/couriertcpd -pid=$SSLPIDFILE -stop
	rc_status -v
	rm -f $SSLPIDFILE*
	;;
    try-restart)
	$0 status >/dev/null &&  $0 restart
	rc_status
	;;
    restart)
	$0 stop
	$0 start
	rc_status
	;;
    force-reload)
	echo -n "Reload Courier-POP3 (ssl) "
	$0 stop  &&  $0 start
	rc_status
	;;
    reload)
	echo -n "Reload Courier-POP3 (ssl) "
	rc_failed 3
	rc_status -v
	;;
    status)
	echo -n "Checking for Courier-POP3 (ssl) "
	checkproc -p $SSLPIDFILE /usr/lib/courier-imap/couriertcpd
	rc_status -v
	;;
    probe)
	test /etc/courier/pop3d -nt $SSLPIDFILE -o /etc/courier/pop3d-ssl -nt $SSLPIDFILE && echo restart
	;;
    *)
	echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload|probe}"
	exit 1
	;;
esac
rc_exit
