#!/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.(plain|ldap|mysql|pgsql)
# config: /etc/courier-imap/authdaemon.conf
# config: /etc/courier-imap/authdaemon-ldap.conf
# config: /etc/courier-imap/authdaemon-mysql.conf
# config: /etc/courier-imap/authdaemon-pgsql.conf
# config: /etc/sysconfig/courier-imap
# pidfile: /var/run/courier-authdaemon.pid

# Do not load RH compatibility interface.
WITHOUT_RC_COMPAT=1

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

# System-wide Courier-IMAP configuration
. /etc/sysconfig/courier-imap

LIBEXEC_DIR=/usr/lib/courier-imap

AUTHD_BIN=$LIBEXEC_DIR/authlib/authdaemond.$DEFAULT_AUTHD

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

[ "x$DEFAULT_AUTHD" != "x" -a -x $AUTHD_BIN ] || exit;
[ "x$START_AUTHD" == "xno" ] && exit;

RETVAL=0

start()
{
	msg_starting $"Courier authentication daemon"
	start_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --no-announce $AUTHD_BIN start

	RETVAL=$?
	return $RETVAL
}	

stop()
{
	msg_stopping $"Courier authentication daemon"
	stop_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --no-announce -- $AUTHD_BIN stop

	RETVAL=$?
	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 $AUTHD_BIN
		RETVAL=$?
		;;
	*)
		msg_usage "${0##*/} {start|stop|restart|condstop|condrestart|status}"
		RETVAL=1
esac

exit $RETVAL
