#!/bin/sh
#
# canary - silences given command (both stdout and stderr) if its exit status
# is zero (a success) and then propagates that status back to the caller
#
# like a canary in a coal mine, this program exists to vocalize failures.
# it also prints the given command and its failing exit status to stderr.
#
# usage:
#     canary COMMAND [ARGUMENTS...]
#     canary SHELL_EVAL_EXPRESSION
#
# ---
# source: https://github.com/sunaku/home

output=$(
  # capture stderr alongside stdout
  exec 2>&1

  # SHELL_EVAL_EXPRESSION was given
  test "$#" -eq 1 && set -- eval "$@"

  # execute the given command line
  "$@"
)
status="$?"

if [ "${status}" -ne 0 ]; then
  echo "canary: command exited with nonzero exit status ${status}" >&2
  echo "${output}" # propagate output of failed command back to caller
fi

exit "${status}"
