#!/bin/sh
#
# courier-authdaemon	Courier-IMAP authentication daemon.
#
# chkconfig: 35 70 30
# description: Courier-IMAP authentication daemon is used as \
#               proxy between Courier's POP3/IMAP servers and \
#               auth-info storages (eg. LDAP, SQL, DB files).
# processname: authdaemond
# config: /etc/courier-authlib/authdaemon.conf
# config: /etc/courier-authlib/authdaemon-ldap.conf
# config: /etc/courier-authlib/authdaemon-mysql.conf
# config: /etc/courier-authlib/authdaemon-pgsql.conf
# pidfile: /var/run/courier-authdaemon.pid
### BEGIN INIT INFO
# Provides: authdaemond
# Required-Start: $network $syslog
# Required-Stop: $network $syslog
# Default-Start:
# Default-Stop:
# Description: Courier-IMAP authentication daemon
# Short-Description: start and stop courier-authdaemon
### END INIT INFO

# Do not load RH compatibility interface.
WITHOUT_RC_COMPAT=1

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

LIBEXEC_DIR=/usr/libexec/courier-authlib

AUTHD_BIN=$LIBEXEC_DIR/authdaemond
DAEMON=/usr/sbin/courier-authdaemon

. /etc/courier-authlib/authdaemon.conf

[ -n "$COURIER_UID" ] || COURIER_UID="courier"
[ -n "$COURIER_GID" ] || COURIER_GID="courier"

PIDFILE=/var/run/courier-authdaemon.pid
LOCKFILE=/var/lock/subsys/courier-authdaemon

[ -x $AUTHD_BIN ] || exit;

RETVAL=0

start()
{
	msg_starting $"Courier authentication daemon"
	$DAEMON "$LOGGEROPTS" -pid="$PIDFILE" -lock="$LOCKFILE" \
		-user="$COURIER_UID" -group="$COURIER_GID" \
		-droproot -respawn \
		-start -name=courier-authdaemon "$AUTHD_BIN"

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

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

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

exit $RETVAL
