#!/usr/bin/bash
set -euo pipefail

PROGRAM_NAME="$(basename "$0")"
VENV_CLI="/opt/irongargoyle/.venv/bin/$PROGRAM_NAME"
FIRST_RUN_HELPER="/usr/libexec/irongargoyle/irongargoyle-first-run"

if [[ "$PROGRAM_NAME" == "irongargoyle" && "${1-}" == "install" && "${2-}" == "first-run" ]]; then
  exec "$FIRST_RUN_HELPER"
fi

if [[ -x "$VENV_CLI" ]]; then
  exec "$VENV_CLI" "$@"
fi

if [[ "$PROGRAM_NAME" == "irongargoyle" ]]; then
  echo "IronGargoyle runtime is not configured yet. Run: sudo irongargoyle install first-run" >&2
else
  echo "IronGargoyle runtime is not configured yet. Run: sudo irongargoyle install first-run before using $PROGRAM_NAME" >&2
fi
exit 1
