#!/bin/sh
#
# Starts qpsmtpd for qpsmtpd.service from the debconf settings that
# /etc/init.d/qpsmtpd used. ExecStart cannot do it alone, as SERVER
# picks the binary and INTERFACES expands to one --listen-address each
#
# --detach and --pid-file are dropped, as systemd follows the process

set -e

PORT="25"
RUNAS="qpsmtpd"
INTERFACES=""
SERVER="forkserver"
EXTRA_ARGS=""

[ -f "/etc/qpsmtpd/debconf-settings" ] && . "/etc/qpsmtpd/debconf-settings"

[ -x "/usr/bin/qpsmtpd-$SERVER" ] || {
	echo "qpsmtpd: /usr/bin/qpsmtpd-$SERVER is not executable" >&2
	exit 1
}

QPSMTPD_CONFIG="/etc/qpsmtpd"
export QPSMTPD_CONFIG

LISTEN=""
for addr in $INTERFACES ; do
	LISTEN="$LISTEN --listen-address $addr"
done

exec "/usr/bin/qpsmtpd-$SERVER" \
	--port "$PORT" \
	--user "$RUNAS" \
	$LISTEN $EXTRA_ARGS
