#!/bin/sh
#
# Adapted for FreeBSD from courier.sysvinit
#
# NOTE: The 'restart' here does a "hard" stop, and a start.  Be gentle, use
# "courierd restart" for a kindler, gentler, restart.
#
# PROVIDE: mail
# REQUIRE: LOGIN courier_authdaemond
# BEFORE:  securelevel
# KEYWORD: shutdown

prefix="%%PREFIX%%"
exec_prefix="${prefix}"
sysconfdir="${prefix}/etc/courier"
certsdir="%%CERTSDIR%%"
sbindir="${exec_prefix}/sbin"
bindir="${exec_prefix}/bin"
libexecdir="${prefix}/libexec"
datadir="${prefix}/share/courier"

# Define these courier_* variables in one of these files:
#	/etc/rc.conf
#	/etc/rc.conf.local
#	/etc/rc.conf.d/courier
#
# DO NOT CHANGE THESE DEFAULT VALUES HERE
#
courier_enable=${courier_enable:-"NO"}	# Run Courier-MTA (YES/NO).

. /etc/rc.subr

name="courier"
rcvar=courier_enable

start_precmd="${name}_prestart"
start_cmd="${name}_start"
stop_cmd="${name}_stop"
extra_commands="restart"

courier_prestart()
{	# Ensure runtime directories exist with correct permissions
	/usr/bin/install -o %%BINOWN%% -g %%BINGRP%% -m 0755 -d %%LOCALSTATEDIR%%
	/usr/bin/install -o %%MAILOWN%% -g %%MAILGRP%% -m 0750 -d %%LOCALSTATEDIR%%/allfilters
	/usr/bin/install -o %%MAILOWN%% -g %%MAILGRP%% -m 0700 -d %%LOCALSTATEDIR%%/faxtmp
	/usr/bin/install -o %%MAILOWN%% -g %%MAILGRP%% -m 0750 -d %%LOCALSTATEDIR%%/filters
	/usr/bin/install -o %%MAILOWN%% -g %%MAILGRP%% -m 0750 -d %%LOCALSTATEDIR%%/msgq
	/usr/bin/install -o %%MAILOWN%% -g %%MAILGRP%% -m 0750 -d %%LOCALSTATEDIR%%/msgs
	/usr/bin/install -o %%MAILOWN%% -g %%MAILGRP%% -m 0770 -d %%LOCALSTATEDIR%%/tmp
	/usr/bin/install -o %%MAILOWN%% -g %%MAILGRP%% -m 0755 -d %%LOCALSTATEDIR%%/track
	/usr/bin/install -o %%MAILOWN%% -g %%MAILGRP%% -m 0400 -c /dev/null %%LOCALSTATEDIR%%/.noident

	/usr/bin/install -o %%BINOWN%% -g %%BINGRP%% -m 0755 -d %%CALENDIR%%
	/usr/bin/install -o %%CACHEOWN%% -g %%MAILGRP%% -m 0700 -d %%CALENDIR%%/localcache
	/usr/bin/install -o %%CACHEOWN%% -g %%MAILGRP%% -m 0750 -d %%CALENDIR%%/private
	/usr/bin/install -o %%CACHEOWN%% -g %%MAILGRP%% -m 0755 -d %%CALENDIR%%/public

	/usr/bin/install -o %%CACHEOWN%% -g mail -m 0700 -d %%CACHEDIR%%
}

courier_start()
{	# First time after install create aliases.dat and makesmtpaccess.dat
	echo -n "Starting"

	if [ -f ${sysconfdir}/crontab ]; then
		/usr/bin/crontab -u %%CACHEOWN%% ${sysconfdir}/crontab
	fi

	if [ ! -f ${sysconfdir}/aliases.dat \
	  -a -x ${sbindir}/makealiases ]; then
		${sbindir}/makealiases
	fi

	esmtpdcert=0

	ESMTPDSTART=""
	if [ -f ${sysconfdir}/esmtpd ]; then
		. ${sysconfdir}/esmtpd
		case "$ESMTPDSTART" in
		[Yy]*)
			esmtpdcert=1
			;;
		esac
		if [ ! -f ${sysconfdir}/esmtpacceptmailfor.dat \
		  -a -x ${sbindir}/makeacceptmailfor ]; then
			${sbindir}/makeacceptmailfor
		fi
		if [ ! -f ${sysconfdir}/${ACCESSFILE}.dat \
		  -a -x ${sbindir}/makesmtpaccess ]; then
			${sbindir}/makesmtpaccess
		fi
	fi

	ESMTPDSTART=""
	if [ -f ${sysconfdir}/esmtpd-msa ]; then
		. ${sysconfdir}/esmtpd-msa
		case "$ESMTPDSTART" in
		[Yy]*)
			esmtpdcert=1
			;;
		esac
		if [ ! -f ${sysconfdir}/${ACCESSFILE}.dat \
		  -a -x ${sbindir}/makesmtpaccess-msa ]; then
			${sbindir}/makesmtpaccess-msa
		fi
	fi

	if [ -x ${sbindir}/courierfilter ]; then
		echo -n " courierfilter"
		${sbindir}/courierfilter start
	fi

	if [ -x ${sbindir}/courierldapaliasd ]; then
		echo -n " courierldapaliasd"
		${sbindir}/courierldapaliasd start
	fi

	if [ -x ${libexecdir}/courier/sqwebmaild \
	  -a -x ${sbindir}/webmaild ]; then
		echo -n " webmail"
		${sbindir}/webmaild start
	fi

	if [ -x ${sbindir}/courier ]; then
		echo -n " courierd"
		${sbindir}/courier start
	fi

	if [ "$esmtpdcert" = 1 ]; then
		# If we do not have a certificate, make one up.
		if [ ! -f ${certsdir}/esmtpd.pem \
		  -a -x "$COURIERTLS" \
		  -a -x ${sbindir}/mkesmtpdcert ]; then
			echo -n " generating-ESMTP-SSL-certificate..."
			${sbindir}/mkesmtpdcert >/dev/null 2>&1
		fi
	fi

	ESMTPDSTART=""
	if [ -f ${sysconfdir}/esmtpd ]; then
		. ${sysconfdir}/esmtpd
		case "$ESMTPDSTART" in
		[Yy]*)
			if [ -x ${sbindir}/esmtpd ]; then
				echo -n " esmtpd"
				${sbindir}/esmtpd start
			fi
			;;
		esac
	fi

	ESMTPDSTART=""
	if [ -f ${sysconfdir}/esmtpd-msa ]; then
		. ${sysconfdir}/esmtpd-msa
		case "$ESMTPDSTART" in
		[Yy]*)
			if [ -x ${sbindir}/esmtpd-msa ]; then
				echo -n " esmtpd-msa"
				${sbindir}/esmtpd-msa start
			fi
			;;
		esac
	fi

	ESMTPDSSLSTART=""
	if [ -f ${sysconfdir}/esmtpd-ssl ]; then
		. ${sysconfdir}/esmtpd-ssl
		case "$ESMTPDSSLSTART" in
		[Yy]*)
			if [ -x "$COURIERTLS" \
			  -a -x ${sbindir}/esmtpd-ssl ]; then
				echo -n " esmtpd-ssl"
				${sbindir}/esmtpd-ssl start
			fi
			;;
		esac
	fi

	IMAPDSTART=""
	if [ -f ${sysconfdir}/imapd ]; then
		. ${sysconfdir}/imapd
		case "$IMAPDSTART" in
		[Yy]*)
			if [ -x ${sbindir}/imapd ]; then
				echo -n " imapd"
				${sbindir}/imapd start
			fi
			;;
		esac
	fi

	IMAPDSSLSTART=""
	if [ -f ${sysconfdir}/imapd-ssl ]; then
		. ${sysconfdir}/imapd-ssl
		case "$IMAPDSSLSTART" in
		[Yy]*)	# If we do not have a certificate, make one up.
			if [ -x "$COURIERTLS" ]; then
				if [ ! -f ${certsdir}/imapd.pem \
				  -a -x ${sbindir}/mkimapdcert ]; then
					echo -n " generating-IMAP-SSL-certificate..."
					${sbindir}/mkimapdcert >/dev/null 2>&1
					# double usage - DH params need to exist
					esmtpdcert=1
				fi
				if [ -x ${sbindir}/imapd-ssl ]; then
					echo -n " imapd-ssl"
					${sbindir}/imapd-ssl start
				fi
			fi
			;;
		esac
	fi

	POP3DSTART=""
	if [ -f ${sysconfdir}/pop3d ]; then 
		. ${sysconfdir}/pop3d
		case "$POP3DSTART" in
		[Yy]*)
			if [ -x ${sbindir}/pop3d ]; then
				echo -n " pop3d"
				${sbindir}/pop3d start
			fi
			;;
		esac
	fi

	POP3DSSLSTART=""
	if [ -f ${sysconfdir}/pop3d-ssl ]; then
		. ${sysconfdir}/pop3d-ssl
		case "$POP3DSSLSTART" in
		[Yy]*)	# If we do not have a certificate, make one up.
			if [ -x "$COURIERTLS" ]; then
				if [ ! -f ${certsdir}/pop3d.pem \
				  -a -x ${sbindir}/mkpop3dcert ]; then 
					echo -n " generating-POP3-SSL-certificate..."
					${sbindir}/mkpop3dcert >/dev/null 2>&1
					# double usage - DH params need to exist
					esmtpdcert=1
				fi
				if [ -x ${sbindir}/pop3d-ssl ]; then
					echo -n " pop3d-ssl"
					${sbindir}/pop3d-ssl start
				fi
			fi
			;;
		esac
	fi

	if [ "$esmtpdcert" = 1 ]; then
		# If we do not have DH params, make them up.
		if [ ! -f ${certsdir}/dhparams.pem \
		  -a -x ${sbindir}/mkdhparams ]; then
			echo -n " generating-DH-params..."
			${sbindir}/mkdhparams >/dev/null 2>&1
		fi
	fi

	LISTS=""
	if [ -f ${sysconfdir}/webmlmrc ]; then 
		. ${sysconfdir}/webmlmrc
		if [ -n "$LISTS" \
		  -a -x ${bindir}/webmlmd \
		  -a -x ${bindir}/webmlmd.rc ]; then
			echo -n " webmlmd"
			${bindir}/webmlmd.rc start ${sysconfdir}/webmlmrc
		fi
	fi
	echo "."
}

courier_stop()
{	# kill courier services in the reverse order of starting them
	echo -n "Stopping"

	if [ -x ${bindir}/webmlmd \
	  -a -x ${bindir}/webmlmd.rc ]; then
		echo -n " webmlmd"
		${bindir}/webmlmd.rc stop ${sysconfdir}/webmlmrc
	fi

	if [ -x ${sbindir}/pop3d-ssl ]; then
		echo -n " pop3d-ssl"
		${sbindir}/pop3d-ssl stop
	fi

	if [ -x ${sbindir}/pop3d ]; then
		echo -n " pop3d"
		${sbindir}/pop3d stop
	fi

	if [ -x ${sbindir}/imapd-ssl ]; then
		echo -n " imapd-ssl"
		${sbindir}/imapd-ssl stop
	fi

	if [ -x ${sbindir}/imapd ]; then
		echo -n " imapd"
		${sbindir}/imapd stop
	fi

	if [ -x ${sbindir}/esmtpd-ssl ]; then
		echo -n " esmtpd-ssl"
		${sbindir}/esmtpd-ssl stop
	fi

	if [ -x ${sbindir}/esmtpd-msa ]; then
		echo -n " esmtpd-msa"
		${sbindir}/esmtpd-msa stop
	fi

	if [ -x ${sbindir}/esmtpd ]; then
		echo -n " esmtpd"
		${sbindir}/esmtpd stop
	fi

	if [ -x ${sbindir}/courier ]; then
		echo -n " courierd"
		${sbindir}/courier stop
	fi

	if [ -x ${sbindir}/webmaild ]; then
		echo -n " webmail"
		${sbindir}/webmaild stop
	fi

	if [ -x ${sbindir}/courierldapaliasd ]; then
		echo -n " courierldapaliasd"
		${sbindir}/courierldapaliasd stop
	fi

	if [ -x ${sbindir}/courierfilter ]; then
		echo -n " courierfilter"
		${sbindir}/courierfilter stop
	fi

	echo y | /usr/bin/crontab -u %%CACHEOWN%% -r || true
	echo "."
}

load_rc_config $name
run_rc_command "$1"
