#!/bin/sh
#
# courier-pop3s		Courier POP3 daemon with SSL support.
#
# chkconfig: 35 71 29
# description: Courier POP3 daemon with SSL support. This daemon
#               is running via Courier own tcpd daemon - couriertcpd
# processname: courier-pop3s
# config: /etc/courier-imap/pop3d
# config: /etc/courier-imap/pop3d-ssl
# pidfile: /var/run/courier-pop3s.pid

# Do not load RH compatibility interface.
WITHOUT_RC_COMPAT=1

# Source function library.
. /etc/init.d/functions
. cert-sh-functions

# POP3 daemon configuration
. /etc/courier-imap/pop3d
. /etc/courier-imap/ssl-sh-functions
. /etc/courier-imap/pop3d-ssl

SBIN_DIR=/usr/sbin

POP3D_BIN=$SBIN_DIR/pop3d
POP3LOGIN_BIN=$SBIN_DIR/pop3login
DAEMON=$SBIN_DIR/courier-pop3s
TCPD_BIN=$SBIN_DIR/couriertcpd
LOCKFILE=/var/lock/subsys/courier-pop3s

[ "$POP3DSSLSTART" != "NO" ] || exit;
[ -x $POP3D_BIN -a -x $POP3LOGIN_BIN ] || exit;
[ -x $DAEMON -a -x $TCPD_BIN ] || exit;
[ -x $COURIERTLS ] || exit;

RETVAL=0

start()
{
	init_tls "pop3d"
	msg_starting $"Courier POP3-SSL daemon"
	/usr/bin/env - /bin/sh -c " set -a ; \
		prefix=/usr ;
		exec_prefix=/usr ;
		bindir=/usr/bin ;
		. /etc/courier-imap/pop3d ; \
		. /etc/courier-imap/pop3d-ssl ; \
		POP3_TLS=1; export POP3_TLS; \
		POP3_STARTTLS=NO; export POP3_STARTTLS; \
		POP3_TLS_REQUIRED=0; export POP3_TLS_REQUIRED; \
		$DAEMON "$SSLLOGGEROPTS"\
		    -pid=$SSLPIDFILE -lock=$LOCKFILE \
			-respawn -start -name=courier-pop3s \
			    $TCPD_BIN \
				-address=$SSLADDRESS \
				-maxprocs=$MAXDAEMONS \
				-maxperip=$MAXPERIP \
				$TCPDOPTS $SSLPORT \
				    $COURIERTLS -server -tcpd \
					$POP3LOGIN_BIN $POP3D_BIN ${MAILDIRPATH}"

	RETVAL=$?
	if [ $RETVAL -eq 0 ]; then
	    success "courier-pop3s startup"
	else
	    failure "courier-pop3s startup"
	fi
	echo
	return $RETVAL
}	

stop()
{
	msg_stopping $"Courier POP3-SSL daemon"

	$DAEMON -pid="$SSLPIDFILE" -lock="$LOCKFILE" -stop

	RETVAL=$?
	if [ $RETVAL -eq 0 ]; then
	    success "courier-pop3s shutdown"
	    rm -f $SSLPIDFILE
	    rm -f $LOCKFILE
	else
	    failure "courier-pop3s shutdown"
	fi
	echo
	return $RETVAL
}

restart()
{
	stop
	start
}

# See how we were called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	reload)
		echo "Reload is not implemented. Please, use restart instead."
		;;
	restart)
		restart
		;;
	condstop)
		if [ -e $LOCKFILE ]; then
			stop
		fi
		;;
	condrestart)
		if [ -e $LOCKFILE ]; then
			restart
		fi
		;;
	status)
		status --pidfile "$SSLPIDFILE" courier-pop3s
		RETVAL=$?
		;;
	*)
		msg_usage "${0##*/} {start|stop|restart|condstop|condrestart|status}"
		RETVAL=1
esac

exit $RETVAL
