# /bin/sh
#
# This is a sample /etc/init.d file for Courier-IMAP
#
# chkconfig: 2345 80 30
# description: Courier-IMAP - IMAP server
#
#
#

prefix=/usr/lib/courier-imap
exec_prefix=${prefix}

case "$1" in
start)
        cd /
	. ${prefix}/etc/imapd
	touch /var/lock/subsys/courier-imap

	echo -n "Starting Courier-IMAP server:"

	case x$IMAPDSTART in
	x[yY]*)
		# Start daemons.
		${exec_prefix}/libexec/imapd.rc start
		echo -n " imap"
		;;
	esac

	. ${prefix}/etc/imapd-ssl
	case x$IMAPDSSLSTART in
	x[yY]*)
		if test -x $COURIERTLS
		then

		# First time we start this, generate a dummy SSL certificate.

			if test ! -f ${prefix}/share/imapd.pem
			then
				echo -n " generating-SSL-certificate..."
				${prefix}/share/mkimapdcert >/dev/null 2>&1
			fi
			${exec_prefix}/libexec/imapd-ssl.rc start
			echo -n " imap-ssl"
		fi
		;;
	esac

	echo ""
	;;
stop)
#        echo -n "Stopping Courier-IMAP server:"
	. ${prefix}/etc/imapd
	. ${prefix}/etc/imapd-ssl
	${exec_prefix}/libexec/imapd.rc stop
#	echo -n " imap"
	if test -x $COURIERTLS
	then
		${exec_prefix}/libexec/imapd-ssl.rc stop
#		echo -n " imap-ssl"
	fi

	echo ""
	;;
restart|reload)
        echo -n "Restarting Courier-IMAP server:"
        echo
        $0 stop
        $0 start
        ;;
*)
        echo "Usage: courier-imap {start|stop|restart|reload}"
        exit 1

esac
exit 0
