#!/bin/sh
# IMAP Auth Daemon
#
# chkconfig:	345 80 20
# description:	IMAP Auth Daemon

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

# Get network config
. /etc/sysconfig/network

METHOD="plain"

[ -f /etc/sysconfig/authdaemon ] && . /etc/sysconfig/authdaemon

# Check that networking is up.
if is_yes "${NETWORKING}"; then
	if [ ! -f /var/lock/subsys/network -a "$1" != stop -a "$1" != status ]; then
		msg_network_down "Courier IMAP - Authdaemon"
		exit 1
	fi
else
	exit 0
fi

RETVAL=0
# See how we were called.
case "$1" in
  start)
	# Check if the service is already running?
	if [ ! -f /var/lock/subsys/authdaemon ]; then
		msg_starting "Courier IMAP - Authdaemon"
		daemon /usr/lib/courier-imap/authlib/authdaemond.$METHOD start
		RETVAL=$?
		[ $RETVAL -eq 0 ] && touch /var/lock/subsys/authdaemon
	else
		msg_already_running "Courier IMAP - Authdaemon"
	fi
	;;
  stop)
	if [ -f /var/lock/subsys/authdaemon ]; then
		msg_stopping "Courier IMAP - Authdaemon"
		daemon /usr/lib/courier-imap/authlib/authdaemond.$METHOD stop
		rm -f /var/lock/subsys/authdaemon
	else
		msg_not_running "Courier IMAP - Authdaemon"
	fi
	;;
  restart|force-reload)
	# restart related services, if running..
	if [ -f /var/lock/subsys/courier-mta -a \
	   -x /etc/rc.d/init.d/courier-mta ]; then
		/etc/rc.d/init.d/courier-mta stop
		crun=1
	fi
	if [ -f /var/lock/subsys/courier-imap -a \
	   -x /etc/rc.d/init.d/courier-imap ]; then
		/etc/rc.d/init.d/courier-imap stop
		imaprun=1
	fi
	if [ -f /var/lock/subsys/courier-pop3 -a \
	   -x /etc/rc.d/init.d/courier-pop3 ]; then
		/etc/rc.d/init.d/courier-pop3 stop
		poprun=1
	fi
	$0 stop
	$0 start
	RETVAL=$?
	[ "$imaprun" -eq 1 ] && /etc/rc.d/init.d/courier-imap start
	[ "$poprun" -eq 1 ] && /etc/rc.d/init.d/courier-pop3 start
	[ "$crun" -eq 1 ] && /etc/rc.d/init.d/courier-mta start
	unset crun
	unset poprun
	unset imaprun
	;;
  status)
	status authdaemond.$METHOD
	exit $?
	;;
  *)
	msg_usage "$0 {start|stop|restart|force-reload|status}"
	exit 3
	;;
esac

exit $RETVAL
