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

root="${AGENTTEAM_EXTENSION_COMPILER_GUARD_ROOT:-$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd -P)}"
compiler_word="compiler"
command_name="agentteam-ext-${compiler_word}"
package_path="internal/extension/${compiler_word}"
build_target="build-ext-${compiler_word}"

failures=()
for retired_path in "cmd/${command_name}" "${package_path}"; do
  if [[ -e "${root}/${retired_path}" ]]; then
    failures+=("${retired_path}: retired Extension authoring implementation must not exist")
  fi
done

matches="$(
  rg -n -F \
    -e "${command_name}" \
    -e "${package_path}" \
    -e "${build_target}" \
    --glob '!docs/superpowers/**' \
    --glob '!guardrails/repository/no-extension-compiler.sh' \
    --glob '!guardrails/repository/no-extension-compiler-test.sh' \
    "${root}" 2>/dev/null || true
)"
if [[ -n "${matches}" ]]; then
  failures+=("retired Extension authoring command/package remains referenced:
${matches}")
fi

if (( ${#failures[@]} > 0 )); then
  printf 'Extension Compiler absence guard failed:\n' >&2
  printf -- '- %s\n' "${failures[@]}" >&2
  exit 1
fi

echo "Extension Compiler absence guard passed."
