#!/bin/sh

if [ "$#" -ne 2 ]; then
    echo "Usage: $0 <mail address> <systemd unit>" >&2
    exit 1
fi

if [ -z "$1" ]; then
    echo "$0: <mail address> is empty!" >&2
    exit 1
fi

include () {
    test -f "$1" && . "$1"
}

include /usr/etc/default/systemd-status-mail
include /etc/default/systemd-status-mail

HOSTNAME=${HOSTNAME:-$(hostname)}

if [ -z "$FROM" ]; then
    FROM="systemd <root@$HOSTNAME>"
fi

if [ -z "${MAILER}" ]; then
    if [ -x /usr/sbin/sendmail ]; then
	MAILER="sendmail"
    elif [ -x /usr/bin/mailx ]; then
	MAILER="mailx"
    else
	echo "ERROR: $0 - no mail program found!" >&2
    fi
fi

case $MAILER in
    sendmail)
	/usr/sbin/sendmail -t <<EOF
To: $1
From: $FROM
Subject: $2 ($HOSTNAME)
Content-Transfer-Encoding: 8bit
Content-Type: text/plain; charset=UTF-8

$(systemctl status --lines=500 --full "$2" 2>&1) 
EOF
	;;
    mailx)
	if [ -n "${RELAYHOST}" ]; then
	    RELAY="-Ssmtp=${RELAYHOST}"
	fi
	mailx $MAILX_OPTIONS -Ssendwait -s "$2 ($HOSTNAME)" -r "$FROM" "$RELAY" "$1" <<EOF
$(systemctl status --lines=500 --full "$2" 2>&1)
EOF
	;;
    *)
	echo "ERROR: \"$MAILER\" is not a valid entry!" >&2
	echo "       Only \"sendmail\" and \"mailx\" are." >&2
	;;
esac
