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

TMP_DIR=$(mktemp -d)
cleanup() {
  rm -rf "$TMP_DIR"
}
trap cleanup EXIT

command -v git >/dev/null || {
  printf "git not found. Install git and re-run.\n" >&2
  exit 1
}

command -v node >/dev/null || {
  printf "node is required for the Codex publisher. Install Node.js 18+ and re-run.\n" >&2
  exit 1
}

if [[ -z "${HOOKBUS_TOKEN:-}" && -f "$HOME/hookbus-light/.env" ]]; then
  set -a
  # shellcheck disable=SC1090
  source "$HOME/hookbus-light/.env"
  set +a
fi
export HOOKBUS_URL="${HOOKBUS_URL:-http://localhost:18800/event}"

git clone --quiet --depth 1 https://github.com/agentic-thinking/hookbus-publisher-codex.git "$TMP_DIR/src"
cd "$TMP_DIR/src"
exec bash install.sh "$@"
