#!/sbin/openrc-run
#
# OpenRC init script for exim4.
# Based on Debian SysV packaging.
#

description="exim Mail Transport Agent"

set -e

[ -x /usr/lib/exim4/exim4 ] || exit 0

. /lib/lsb/init-functions 2>/dev/null || true

if [ -n "$EX4DEBUG" ]; then
	echo "now debugging exim4"
	set -x
fi

LANG=C
export LANG

EXIMDAEMONOPTS='-bd -q30m'
UPEX4OPTS=''
[ -f /etc/default/exim4 ] && . /etc/default/exim4
PIDFILE="/run/exim4/exim.pid"
DAEMON="/usr/sbin/exim4"
NAME="exim4"

upex4conf() {
	UPEX4CONF="update-exim4.conf"
	OLDIFS="$IFS"
	IFS=:
	for p in $PATH; do
		if [ -x "$p/$UPEX4CONF" ]; then
			IFS="$OLDIFS"
			"$p/$UPEX4CONF" $UPEX4OPTS $1
			return 0
		fi
	done
	IFS="$OLDIFS"
}

log()
{
	case "$1" in
		[[:digit:]]*) success=$1; shift;;
		*) :;;
	esac
	log_action_begin_msg "$1"; shift
	log_action_end_msg ${success:-0} "$*"
}

start_exim()
{
	[ -e /run/exim4 ] || \
		install -d -oDebian-exim -gDebian-exim -m750 /run/exim4
	start_daemon -p "$PIDFILE" "$DAEMON" $EXIMDAEMONOPTS
	log_progress_msg "exim4"
}

stop_exim()
{
	if [ -f "$PIDFILE" ]; then
		start-stop-daemon --stop --retry 5 --quiet --oknodo --remove-pidfile \
			--pidfile "$PIDFILE" \
			--exec "$DAEMON"
		if [ $? -eq 2 ] ; then rm -f "$PIDFILE" ; fi
		log_progress_msg "exim4_listener"
	fi
}

reload_exim()
{
	start-stop-daemon --stop --signal HUP --quiet --oknodo \
		--pidfile "$PIDFILE" \
		--exec "$DAEMON"
	log_progress_msg "exim4"
}

kill_all_exims()
{
	SIG="${1:-TERM}"
	for pid in $(pidof $NAME); do
		if [ "$(readlink /proc/$pid/root)" = "/" ]; then
			kill -$SIG $pid
		fi
	done
}

isconfigvalid()
{
	if ! $DAEMON -bV > /dev/null ; then
		log 1 "Warning! Invalid configuration file for $NAME. Exiting."
		return 1
	fi
}

warn_paniclog()
{
	if [ -s "/var/log/exim4/paniclog" ]; then
		if [ -z "$E4BCD_PANICLOG_NOISE" ] || grep -vq "$E4BCD_PANICLOG_NOISE" /var/log/exim4/paniclog; then
			echo "ALERT: exim paniclog /var/log/exim4/paniclog has non-zero size, mail system possibly broken" 1>&2
		fi
	fi
}

depend() {
	need net localmount
	use dns logger
	after bootmisc
}

extra_commands="force_stop"
extra_started_commands="force_stop"

start() {
	ebegin "Starting MTA"
	upex4conf
	isconfigvalid || { eend 1; return 1; }
	start_exim
	eend 0
	warn_paniclog
}

stop() {
	ebegin "Stopping MTA"
	stop_exim
	eend 0
	warn_paniclog
}

restart() {
	upex4conf --check
	ebegin "Stopping MTA for restart"
	stop_exim
	upex4conf
	isconfigvalid || { eend 1; return 1; }
	eend 0
	sleep 2
	ebegin "Restarting MTA"
	start_exim
	eend 0
	warn_paniclog
}

reload() {
	ebegin "Reloading $NAME configuration files"
	upex4conf
	isconfigvalid || { eend 1; return 1; }
	reload_exim
	eend 0
	warn_paniclog
}

status() {
	log_action_begin_msg "checking $NAME"
	if pidofproc -p "$PIDFILE" "$DAEMON" >/dev/null; then
		log_action_end_msg 0 "running"
		return 0
	else
		if [ -e "$PIDFILE" ]; then
			log_action_end_msg 1 "$NAME failed"
			return 1
		else
			log_action_end_msg 0 "not running"
			return 3
		fi
	fi
}

force_stop() {
	kill_all_exims "${1:-TERM}"
}
