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

SOURCE="${BASH_SOURCE[0]}"
while [ -L "$SOURCE" ]; do
  TARGET="$(readlink "$SOURCE")"
  if [[ "$TARGET" = /* ]]; then
    SOURCE="$TARGET"
  else
    SOURCE="$(cd -P "$(dirname "$SOURCE")" && pwd)/$TARGET"
  fi
done
ROOT="$(cd -P "$(dirname "$SOURCE")/.." && pwd)"

case "${1:-}" in
  --install-hooks)
    shift
    exec bun run "$ROOT/scripts/install-hooks.ts" "$@"
    ;;
  --install-skill)
    shift
    exec bun run "$ROOT/scripts/install-skill.ts" "$@"
    ;;
  --install-codex-hooks)
    shift
    exec bun run "$ROOT/scripts/install-codex-hooks.ts" "$@"
    ;;
  --install)
    shift
    # Combo: install skill globally to all hosts AND install hooks to a target repo.
    # Usage: gxpm-init --install [--target <repo>] [--skip-nexus-script]
    bun run "$ROOT/scripts/install-skill.ts" --host all
    exec bun run "$ROOT/scripts/install-hooks.ts" "$@"
    ;;
  *)
    exec bun run "$ROOT/scripts/gen-skill-docs.ts" "$@"
    ;;
esac
