#!/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_no "${NETWORKING}" ; then
	msg_network_down "Courier IMAP - Authdaemon"
	exit 1
fi

# 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"
		exit 1
	fi
	;;
  stop)
	if [ -f /var/lock/subsys/authdaemon ]; then
		msg_stopping "Courier IMAP - Authdaemon"
		daemon /usr/lib/courier-imap/authlib/authdaemond.$METHOD stop
		RETVAL=$?
		rm -f /var/lock/subsys/authdaemon
	else
		msg_not_running "Courier IMAP - Authdaemon"
		exit 1
	fi
	;;
  restart|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
	;;
  *)
	msg_usage "$0 {start|stop|restart|reload|status}"
	exit 1
esac

exit $RETVAL

