#!/bin/sh
set -eu

. /etc/default/reprepro-notify-incoming-changes

if [ -z "$MAILFROM" ]; then
	echo "Config error in /etc/default/reprepro-notify-incoming-changes: missing MAILFROM"
	exit 1
fi
if [ -z "$MAILTO" ]; then
        echo "Config error in /etc/default/reprepro-notify-incoming-changes: missing MAILTO"
        exit 2
fi
if [ -z "$REPREPRODIR" ]; then
        echo "Config error in /etc/default/reprepro-notify-incoming-changes: missing REPREPRODIR"
        exit 3
fi

HOSTNAME=$(/bin/hostname)
SUBJECT="Notification of incoming change file on $HOSTNAME"
DATE=$(/bin/date -uIseconds)

printf 'From: %s\nTo: %s\nSubject: %s\nHello there,\n\nIt is %s and this little daemon just saw the following file appear on %s:\n\n%s%s\n\nFYI!\n' "$MAILFROM" "$MAILTO" "$SUBJECT" "$DATE" "$HOSTNAME" "$REPREPRODIR" "$1" | /usr/sbin/sendmail -t

printf 'Incoming change detected: %s%s\n' "$REPREPRODIR" "$1"
