#!/bin/bash
set -e -u -o pipefail

echo "Running command $*"

tmpfile=$(mktemp)
trap 'rm -f "$tmpfile"' EXIT

# burp will prepend WARNING to anything written to stderr, and MESSAGE to
# anything written to stdout. Monitoring as implemented in profile_burp uses
# the prefix to determine if there are any errors during backups, therefore
# all output is sent to stdout and a single error message is logged to stderr
# in case the called script fails.
if "$@" &> "$tmpfile"; then
    cat "$tmpfile"
    echo "Command $* completed successfully"
else
    cat "$tmpfile"
    >&2 echo "Command $* failed"
fi

# burp will abort the backup, if the exit code of any pre-backup script
# is not 0.
exit 0
