#! /bin/sh
# $Id: imapd.rc.in,v 1.26 2004/04/18 15:54:38 mrsam Exp $
#
# Copyright 1998 - 2002 Double Precision, Inc.
# See COPYING for distribution information.


prefix=/usr
exec_prefix=/usr
bindir=${exec_prefix}/bin
libexecdir=/usr/libexec/courier-imap

if test ! -f /etc/courier-imap/imapd
then
	echo "/etc/courier-imap/imapd does not exist, forgot make install-configure?"
	exit 1
fi

if test ! -f /etc/courier-imap/imapd-ssl
then
	echo "/etc/courier-imap/imapd-ssl does not exist, forgot make install-configure?"
	exit 1
fi

TLS_CACHEFILE=""
. /etc/courier-imap/imapd-ssl
. /etc/courier-imap/imapd

case $1 in
start)
	LIBAUTHMODULES=""
	for f in `echo $AUTHMODULES`
	do
		LIBAUTHMODULES="$LIBAUTHMODULES /usr/libexec/courier-imap/authlib/$f"
	done

	if test -x ${libexecdir}/authlib/authdaemond
	then
		/usr/bin/env - DEBUG_LOGIN="$DEBUG_LOGIN" ${libexecdir}/authlib/authdaemond start
	fi

	if test "$TLS_CACHEFILE" != ""
	then
		rm -f $TLS_CACHEFILE
	fi

	ulimit -v $IMAP_ULIMITD
	/usr/bin/env - /bin/sh -c " set -a ;
			prefix=/usr ;
			exec_prefix=/usr ;
			bindir=${exec_prefix}/bin ;
			libexecdir=/usr/libexec/courier-imap ;
				. /etc/courier-imap/imapd ; \
				. /etc/courier-imap/imapd-ssl ; \
		IMAP_STARTTLS=$IMAPDSTARTTLS ; export IMAP_STARTTLS ; \
		TLS_PROTOCOL=$TLS_STARTTLS_PROTOCOL ; \
		/usr/libexec/courier-imap/couriertcpd -address=$ADDRESS \
			-stderrlogger=${exec_prefix}/sbin/courierlogger \
			-stderrloggername=imapd \
			-maxprocs=$MAXDAEMONS -maxperip=$MAXPERIP \
			-pid=$PIDFILE $TCPDOPTS \
			$PORT ${exec_prefix}/sbin/imaplogin $LIBAUTHMODULES \
				${exec_prefix}/bin/imapd ${MAILDIRPATH}"
	;;
stop)
	/usr/libexec/courier-imap/couriertcpd -pid=$PIDFILE -stop
	if test -x ${libexecdir}/authlib/authdaemond
	then
		${libexecdir}/authlib/authdaemond stop
	fi
	;;
esac
exit 0
