#!/bin/sh

# Init file for Courier-IMAP authdaemon
#
# chkconfig: 35 70 30
# description: Courier-IMAP authentication daemon
#
# processname: authdaemond.*
# 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
# pidfile: /var/lib/courier-imap/pid

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

# System-wide network configuration
. /etc/sysconfig/network

# 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
LCKFILE=/var/lock/courier-imap/courier-authdaemon.lock

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

RETVAL=0
LOCKFILE=/var/lock/subsys/courier-authdaemond.$DEFAULT_AUTHD

start()
{
	echo -n "Starting Courier-IMAP authdaemon.$DEFAULT_AUTHD: "
	daemon $AUTHD_BIN start
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && touch "$LOCKFILE"
	return $RETVAL
}	

stop()
{
	echo -n "Shutting down Courier-IMAP authdaemon.$DEFAULT_AUTHD: "
	#killproc authdaemond.$DEFAULT_AUTHD
	$AUTHD_BIN stop
	RETVAL=$?
	success "authdaemond.$DEFAULT_AUTHD shutdown"
	echo
	if [ $RETVAL -eq 0 ]; then
	    rm -f "$LOCKFILE"
	    rm -f "$PIDFILE"
	    rm -f "$LCKFILE"
	fi
	return $RETVAL
}

reload()
{
	echo -n "Reloading Courier-IMAP authdaemon.$DEFAULT_AUTHD: "
	#killproc authdaemond.$DEFAULT_AUTHD
	$AUTHD_BIN restart
	success "authdaemond.$DEFAULT_AUTHD reloading"
	RETVAL=$?
	echo
	return $RETVAL
}

restart()
{
	stop
	start
}

# See how we were called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	restart)
		restart
		;;
	reload)
		reload
		;;
	condstop)
		# Stop the servce if it is already running, for example:
		if [ -e "$LOCKFILE" ]; then
			stop
		fi
		;;
	condrestart)
		# Restart the servce if it is already running, for example:
		if [ -e "$LOCKFILE" ]; then
			restart
		fi
		;;
	status)
		# report the status of the daemons in free-form format,
		# perhaps with the status function, for example:
		status authdaemond.$DEFAULT_AUTHD
		RETVAL=$?
		;;
	*)
		echo "Usage: ${0##*/} {start|stop|reload|restart|condstop|condrestart|status}"
		RETVAL=1
esac

exit $RETVAL
