#!/bin/sh
# ExecStopPost= hook: replaces the failure mail that safe_utm5_* used to send.
service=$1
result=$SERVICE_RESULT
status=$EXIT_STATUS

if [ -z "$result" ] && command -v systemctl >/dev/null 2>&1; then
	props=`systemctl show -p Result -p ExecMainStatus "$service" 2>/dev/null`
	result=`echo "$props" | sed -n 's/^Result=//p'`
	status=`echo "$props" | sed -n 's/^ExecMainStatus=//p'`
fi

if [ "${result:-success}" = "success" ]; then
	exit 0
fi

if ! command -v mail >/dev/null 2>&1; then
	exit 0
fi

printf '%s stopped on %s at %s: result=%s exit=%s\nSee journalctl -u %s\n' \
	"$service" "`hostname`" "`date`" "$result" "${status:-unknown}" "$service" |
	mail -s "$service stopped: $result" root
