#!/bin/sh
#
# git-gau-ac - Apply a command and record its output as the commit message.

set -e
set -u

# Required binaries
CAT=/bin/cat
ECHO=/bin/echo
GIT=/usr/bin/git
MKTEMP=/bin/mktemp
RM=/bin/rm
XARGS=/usr/bin/xargs

# Replicate output of command to stdout. Regrettably it is not possible to use
# `tee` here since not all shells support -o pipefail.
LOGFILE="$(${MKTEMP})"
collect_logs() {
    # Send log to stdout.
    ${CAT} "${LOGFILE}"
    ${RM} -f "${LOGFILE}"
}
trap collect_logs EXIT

# Print usage message and exit.
if [ "${#}" -lt 1 ] || [ "${1:-h}" = "-h" ] || [ "${1:---help}" = "--help" ]; then
    ${ECHO} "${0}: command [args...]" >&2
    exit 1
fi

# Perform a checkout.
${ECHO} "${GAU_CHECKOUT_ARGS:---quiet}" | ${XARGS} ${GIT} checkout

# Run command and record commit message.
"${@}" > "${LOGFILE}"

# Record changes to files.
${ECHO} "${GAU_ADD_ARGS:---all}" | ${XARGS} ${GIT} add
${ECHO} -F "${LOGFILE}" "${GAU_COMMIT_ARGS:---quiet}" | ${XARGS} ${GIT} commit
