#!/usr/bin/env bash
# AIQ Rank — Codex setup
# Installs or refreshes the AIQ Rank Codex plugin and its
# managed local scan + upload artifacts.
#
# Wrapped in `{ … }` so bash refuses to execute a truncated body if
# the curl|bash stream drops mid-transfer.
{
  set -euo pipefail

command -v codex >/dev/null || { echo "AIQ Rank setup requires the Codex CLI." >&2; exit 1; }

if codex plugin marketplace list | grep -q '^aiqrank[[:space:]]'; then
    codex plugin marketplace upgrade aiqrank
  else
    codex plugin marketplace add aiqrank/plugin
  fi

codex plugin add aiqrank@aiqrank

REPO="aiqrank/plugin"
  REF="${AIQRANK_REF:-main}"
  BASE="https://raw.githubusercontent.com/${REPO}/${REF}/plugins/aiqrank"
  PYTHON="$(command -v python3 || command -v python)"
  test -n "$PYTHON" || { echo "AIQ Rank setup requires Python 3." >&2; exit 1; }
  INSTALLER="$(mktemp "${TMPDIR:-/tmp}/aiqrank-install.XXXXXX.py")"
  trap 'rm -f "$INSTALLER"' EXIT
  curl -fsSL "$BASE/scripts/install_codex.py" -o "$INSTALLER"
  "$PYTHON" "$INSTALLER" --base "$BASE"
}
