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

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

. /etc/courier-imap/imapd-ssl
. /etc/courier-imap/imapd

imapd_bin=/usr/bin/imapd
imaplogin_bin=/usr/sbin/imaplogin
daemon=/usr/sbin/courierlogger
tcpd_bin=/usr/lib64/couriertcpd
prog="Courier IMAP server"
pidfile=$PIDFILE

lockfile=/var/lock/subsys/courier-imapd

[ -x $imapd_bin -a -x $imaplogin_bin ] || exit;
[ -x $daemon -a -x $tcpd_bin ] || exit;

start()
{
	[ "$IMAPDSTART" != "NO" ] || exit;
	[ -z "$TLS_CACHEFILE" ] || rm -f $TLS_CACHEFILE

	umask $IMAP_UMASK >/dev/null
	/usr/bin/env -i /bin/sh -c " set -a ;
		prefix=/usr ;
		exec_prefix=/usr ;
		bindir=/usr/bin ;
		libexecdir=/usr/lib64 ;
		. /etc/courier-imap/imapd-ssl ;
		. /etc/courier-imap/imapd ;
		IMAP_STARTTLS=$IMAPDSTARTTLS ; 
		PROXY_HOSTNAME=$PROXY_HOSTNAME ; 
		TLS_PROTOCOL=$TLS_STARTTLS_PROTOCOL ; 
		$daemon $LOGGEROPTS 			-pid=$pidfile ${lockfile:+-lockfile=$lockfile} 			-start -name=courier-imapd 				$tcpd_bin 				-address=$ADDRESS 				-maxprocs=$MAXDAEMONS 				-maxperip=$MAXPERIP 				$TCPDOPTS $PORT 				$imaplogin_bin $imapd_bin ${MAILDIRPATH}"
}

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


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 $?
