#!/bin/sh
set -eu

if [ "$(whoami)" = "root" ] && [ -n "${SUDO_USER:-}" ]
then
    notify="sudo --preserve-env --set-home --user $SUDO_USER notify"
else
    notify='notify'
fi

eval "$*" || code="$?"
code="${code:-0}"

if [ "$code" -eq 0 ]
then
    echo "$(basename "${1#__}") has finished." | $notify
else
    echo "$(basename "${1#__}") has failed." | $notify --urgency=critical
fi

return "$code"
