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

# Do not load RH compatibility interface.
WITHOUT_RC_COMPAT=1

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

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

SBIN_DIR=/usr/sbin

POP3D_BIN=$SBIN_DIR/pop3d
POP3LOGIN_BIN=$SBIN_DIR/pop3login
DAEMON=$SBIN_DIR/courier-pop3d
TCPD_BIN=$SBIN_DIR/couriertcpd

LOCKFILE=/var/lock/subsys/courier-pop3d

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

RETVAL=0

start()
{
	init_tls "pop3d"
	msg_starting $"Courier POP3 daemon"
	/usr/bin/env -i /bin/sh -c " set -a ;
		prefix=/usr ;
		exec_prefix=/usr ;
		bindir=/usr/bin ;
		. /etc/courier-imap/pop3d-ssl ; \
		. /etc/courier-imap/pop3d ; \
		PROXY_HOSTNAME=$PROXY_HOSTNAME ; \
		TLS_PROTOCOL=$TLS_STARTTLS_PROTOCOL ; export TLS_PROTOCOL ; \
		$DAEMON "$LOGGEROPTS"\
		    -pid=$PIDFILE -lock=$LOCKFILE \
			-respawn -start -name=courier-pop3d \
			    $TCPD_BIN \
				-address=$ADDRESS \
				-maxprocs=$MAXDAEMONS \
				-maxperip=$MAXPERIP \
				$TCPDOPTS $PORT \
				$POP3LOGIN_BIN $POP3D_BIN ${MAILDIRPATH}"

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

stop()
{
	msg_stopping $"Courier POP3 daemon"
	$DAEMON -pid="$PIDFILE" -lock="$LOCKFILE" -stop

	RETVAL=$?
	if [ $RETVAL -eq 0 ]; then
	    success "courier-pop3d shutdown"
	    rm -f "$PIDFILE"
	    rm -f "$LOCKFILE"
	else
	    failure "courier-pop3d 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 "$PIDFILE" courier-pop3d
		RETVAL=$?
		;;
	*)
		msg_usage "${0##*/} {start|stop|restart|condstop|condrestart|status}"
		RETVAL=1
esac

exit $RETVAL
