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

# Prefer the authenticated GitHub identity for commits. The image-level fallback still lets
# setup and CI run before `gh auth login` has populated the persistent credential volume.
if [[ "${TAUCETI_SKIP_GIT_IDENTITY:-0}" != 1 ]]; then
  if gh auth status >/dev/null 2>&1; then
    login="$(gh api user --jq .login 2>/dev/null || true)"
    account_id="$(gh api user --jq .id 2>/dev/null || true)"
    if [[ -n "$login" && "$account_id" =~ ^[0-9]+$ ]]; then
      git config --system user.name "$login"
      git config --system user.email "${account_id}+${login}@users.noreply.github.com"
    fi
  fi
  git_name="$(git config --system user.name)"
  git_email="$(git config --system user.email)"
  if [[ "$git_name" == "EpsilonEridani Worker" ]]; then
    echo >&2 "epsiloneridani-entrypoint: WARNING: GitHub identity unavailable; commits use the fallback identity"
  fi
  echo "epsiloneridani-entrypoint: git author $git_name <$git_email>"
fi

# Compose supplies the stable `./epsiloneridani work --loop` command as separate arguments.
# Append operator options from .env using Python's shell-like parser; never evaluate them
# as shell code. Restrict this to the worker command so setup and diagnostic overrides are
# unaffected.
if [[ -n "${TAUCETI_WORKER_ARGS:-}" && "${1:-}" == "./epsiloneridani" && "${2:-}" == "work" && "${3:-}" == "--loop" ]]; then
  exec python3 -c '
import os
import shlex
import sys

try:
    extra = shlex.split(os.environ["TAUCETI_WORKER_ARGS"])
except ValueError as error:
    sys.exit(f"epsiloneridani-entrypoint: TAUCETI_WORKER_ARGS has invalid quoting: {error}")
argv = sys.argv[1:] + extra
os.execvp(argv[0], argv)
' "$@"
fi

exec "$@"
