#!/usr/bin/env bash
# qualiow — launcher shim so `${CLAUDE_PLUGIN_ROOT}/bin/qualiow` (or a checkout's
# `bin/qualiow`) works even when this repo was installed as a Claude Code plugin
# straight from git — a marketplace/plugin install has no `dist/` (tsup output)
# and no `node_modules` (tsup does not bundle deps), so the real CLI entry point
# `dist/cli/index.js` cannot run in place. When a built local copy exists, run it
# directly; otherwise fall through to the published npm package via `npx`, pinned
# to this checkout's own `.claude-plugin/plugin.json` version so the shim and the
# skills it ships alongside stay in lockstep. Release order matters for that pin:
# `npm publish` must land before this version becomes the current git tag, or the
# pinned `npx` fetch 404s — see docs/ARCHITECTURE-DECISIONS.md ADR-012.
set -euo pipefail

SELF="${BASH_SOURCE[0]}"
while [ -L "$SELF" ]; do
  LINK="$(readlink "$SELF")"
  case "$LINK" in /*) SELF="$LINK" ;; *) SELF="$(dirname "$SELF")/$LINK" ;; esac
done
# `pwd -P` on both sides: the tmp/symlinked-path case must still compare equal.
ROOT="$(cd "$(dirname "$SELF")/.." && pwd -P)"

if [ -f "$ROOT/dist/cli/index.js" ] && [ -d "$ROOT/node_modules" ]; then
  exec node "$ROOT/dist/cli/index.js" "$@"
fi

# No build here. If the working directory is inside this very tree, `npx` resolves
# the *local* project context first — it finds a package.json named
# qualiow-exploratory-testing with no linked bin and fails with a bare
# "sh: qualiow: command not found". Say what is actually wrong instead.
case "$(pwd -P)/" in
  "$ROOT"/*)
    cat >&2 <<MSG
qualiow: no CLI build in $ROOT (dist/ and node_modules are both missing), and the
         working directory is inside that tree, so npx cannot resolve the published
         package from here.

  Build it once:   cd "$ROOT" && npm install && npm run build
  Or run from a project directory outside $ROOT.
MSG
    exit 1
    ;;
esac

export CLAUDE_PLUGIN_ROOT="${CLAUDE_PLUGIN_ROOT:-$ROOT}"

VER="$(node -p "try{require('$ROOT/.claude-plugin/plugin.json').version}catch{''}" 2>/dev/null || true)"
[ -z "$VER" ] && VER="latest"

exec npx -y -p "qualiow-exploratory-testing@$VER" qualiow "$@"
