#! /bin/sh
#
# chkconfig: - 80 30
# courier-authdaemon  Courier-IMAP authentication daemon.
#
### BEGIN INIT INFO
# Provides:			 courier-authdaemon
# Required-Start:	 $syslog $remote_fs
# Required-Stop:	 $syslog $remote_fs
# Default-Start:	 2 3 5
# Default-Stop:		 0 1 6
# Short-Description: Courier-IMAP authentication daemon
# Description:		 Start the Courier-IMAP authentication daemon
### END INIT INFO

if [ -f /etc/rc.d/init.d/functions ]; then
	. /etc/rc.d/init.d/functions
fi

. /etc/courier-imap/authlib/authdaemonrc

auth_bin=/usr/lib/courier-authlib/authdaemond
daemon=/usr/sbin/courierlogger
prog="Courier IMAP authentication daemon"

pidfile=/var/run/courier-authdaemon.pid
lockfile=/var/lock/subsys/courier-authdaemon

[ -n "$COURIER_UID" ] || courier_uid="popuser"
[ -n "$COURIER_GID" ] || courier_gid="popuser"

[ -x $daemon -a -x $auth_bin ] || exit;

start()
{
	install -m 0700 -o "$courier_uid" -g 0 -d "/var/run/authdaemon.courier-imap"
	/usr/bin/env - /bin/sh -c " set -a ;
		prefix=/usr ;
		exec_prefix=/usr ;
		sbindir=/usr/sbin ;
		localstatedir=/var/run ;
		. /etc/courier-imap/authlib/authdaemonrc ;
		$daemon -name=courier-authdaemon $LOGGEROPTS 			-pid=$pidfile 			${lockfile:+-lockfile=$lockfile} 			-start $auth_bin"

# Should be placed before start
#		-user=\"$courier_uid\" -group=\"$courier_uid\" -droproot #
}

stop()
{
	$daemon -pid="$pidfile" ${lockfile:+-lockfile=$lockfile} -stop "$auth_bin"
}


case "$1" in
	start)
		echo -n $"Starting $prog: "
		start
		rc=$?
		if [ $rc -eq 0 ]; then
			success $"$prog startup"
		else
			failure $"$prog startup"
		fi
		echo
		exit $rc
	;;

	stop)
		echo -n $"Stopping $prog: "
		stop
		rc=$?
		if [ $rc -eq 0 ]; then
			success $"$prog shutdown"
			rm -f "$pidfile" "$lockfile"
		else
			failure $"$prog shutdown"
		fi
		echo
		exit $rc
	;;

	status)
		status -p "$pidfile" `basename $daemon`
	;;

	restart|reload|graceful)
		$0 stop
		$0 start
	;;

	condrestart)
		[ -f $lockfile ] && stop && start || :
	;;

	*)
		echo "Usage: $0 {start|stop|status|restart|reload|condrestart}"
		exit 1
	;;

esac

exit $?
