#! /bin/sh
#
# Copyright 1998 - 2008 Double Precision, Inc.
# See COPYING for distribution information.


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

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)
	if test "$TLS_CACHEFILE" != ""
	then
		rm -f $TLS_CACHEFILE
	fi

	umask $IMAP_UMASK
	ulimit -v $IMAP_ULIMITD
	/usr/bin/env -i /bin/sh -c " set -a ;
			prefix=/usr ;
			exec_prefix=/usr ;
			bindir=${exec_prefix}/bin ;
			libexecdir=/usr/libexec ;
				. /etc/courier-imap/imapd-ssl ; \
				. /etc/courier-imap/imapd ; \
		IMAP_STARTTLS=$IMAPDSTARTTLS ; \
		PROXY_HOSTNAME=$PROXY_HOSTNAME ; \
		TLS_PROTOCOL=$TLS_STARTTLS_PROTOCOL ; \
		/usr/libexec/slackrepo/mkdir -p `dirname $PIDFILE` ; \
		/usr/sbin/courierlogger -pid=$PIDFILE -start $LOGGEROPTS \
		/usr/libexec/couriertcpd -address=$ADDRESS \
			-maxprocs=$MAXDAEMONS -maxperip=$MAXPERIP \
			$TCPDOPTS \
			$PORT ${exec_prefix}/sbin/imaplogin ${exec_prefix}/bin/imapd ${MAILDIRPATH}"
	;;
stop)
	/usr/sbin/courierlogger -pid=$PIDFILE -stop
	;;
esac
exit 0
