#! /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/local
exec_prefix=/usr/local
bindir=${exec_prefix}/bin
libexecdir=/usr/local/libexec/courier-imap

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

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

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

case $1 in
start)
	LIBAUTHMODULES=""
	for f in `echo $AUTHMODULES`
	do
		LIBAUTHMODULES="$LIBAUTHMODULES /usr/local/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/local ;
			exec_prefix=/usr/local ;
			bindir=${exec_prefix}/bin ;
			libexecdir=/usr/local/libexec/courier-imap ;
				. /usr/local/etc/courier-imap/imapd ; \
				. /usr/local/etc/courier-imap/imapd-ssl ; \
		IMAP_STARTTLS=$IMAPDSTARTTLS ; export IMAP_STARTTLS ; \
		TLS_PROTOCOL=$TLS_STARTTLS_PROTOCOL ; \
		/usr/local/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/local/libexec/courier-imap/couriertcpd -pid=$PIDFILE -stop
	if test -x ${libexecdir}/authlib/authdaemond
	then
		${libexecdir}/authlib/authdaemond stop
	fi
	;;
esac
exit 0
