#!/bin/bash
set -e

NAME="SMTP"

function stop {
	echo "Stopping ${NAME}"
	kill $(cat /run/saslauthd/saslauthd.pid)
	kill $(cat /run/sendmail/mta/sendmail.pid|head -1)
}

trap 'stop' SIGTERM

if [ -z `hostname --domain` ]; then
	echo "You must start this container with --hostname= specifying a domain name"
	exit 1
fi

if [ "$1" == "start" ]; then
	/usr/sbin/saslauthd -m /run/saslauthd -a pam
	cd /etc/mail && make clean && make && make && exec /usr/sbin/sendmail -q1h -bD &

	wait
else
	exec $@
fi