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


prefix=/usr/local
exec_prefix=/usr/local
bindir=/usr/local/bin
libexecdir=/usr/local/libexec

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

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

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

case $1 in
start)
	if test "$TLS_CACHEFILE" != ""
	then
		rm -f $TLS_CACHEFILE
	fi

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