#!/usr/bin/env bash
set -Eeuo pipefail
LOG_FILE="$(mktemp)"
exec &> >(tee -a "$LOG_FILE")

finish() {
  status=$?
  if [[ $status = 0 ]]; then
    STATUS="✅ SUCCESS (status $status)"
  else
    STATUS="❌ FAILURE (status $status)"
  fi

  echo "Sending report email..."

  SUBJECT="Job report on $(hostname): ${STATUS} for annex-job.sh"
  gpg -easr eu@euandre.org < "$LOG_FILE" | \
    mail -s "$SUBJECT" eu@euandre.org
  echo 'Mail sent!'
}
trap finish EXIT

echo "Starting: $(date -Is)"
~/dev/libre/dotfiles/scripts/ad-hoc/annex.sh
echo "Finishing: $(date -Is)"
