#!/usr/bin/env bash
# pi-watch — launcher for pi-watch.mjs.
#
# The launcher deliberately does NOT run whatever copy of pi-watch.mjs happens
# to sit next to it. The Claude plugin cache stores every installed quirk
# version in its own immutable dir (…/cache/quirk-dev/quirk/<version>/), so a
# PATH symlink into one of those dirs would freeze at that version forever —
# `claude plugin update` lands new code in a *new* dir and never repoints the
# link. Instead, on every run we resolve the *currently installed* quirk path
# from installed_plugins.json, so pi-watch always tracks the latest update.
#
# Resolution order (first hit wins):
#   1. $PI_WATCH_DIR                    — explicit override (testing / power use)
#   2. dev checkout                     — own dir has the code AND is not inside
#                                         the plugin cache → run it (tracks repo)
#   3. installed plugin                 — installPath from installed_plugins.json
#   4. own dir                          — last-ditch, if it carries the code
#
# `pi-watch --where` prints the resolved source + dir (and whether the resolved
# copy has the GPT-6 alias ladder) and exits — use it to diagnose "which
# version am I actually running".
set -euo pipefail

# --- resolve this launcher's real directory (follow the PATH symlink) --------
SOURCE="${BASH_SOURCE[0]}"
while [ -L "$SOURCE" ]; do
    TARGET="$(readlink "$SOURCE")"
    if [[ "$TARGET" = /* ]]; then
        SOURCE="$TARGET"
    else
        SOURCE="$(cd "$(dirname "$SOURCE")" && pwd)/$TARGET"
    fi
done
DIR="$(cd "$(dirname "$SOURCE")" && pwd)"

CACHE_PREFIX="$HOME/.claude/plugins/cache/"
SUBPATH="skills/pi-dev/scripts/pi-watch"

# Reads the live installPath for the quirk plugin. Prefers the quirk@quirk-dev
# key, falls back to any quirk@* entry. Prints nothing (exit 1) if unavailable.
resolve_installed() {
    node -e '
        const fs = require("fs"), os = require("os"), path = require("path");
        try {
            const f = path.join(os.homedir(), ".claude/plugins/installed_plugins.json");
            const plugins = JSON.parse(fs.readFileSync(f, "utf8")).plugins || {};
            const key = plugins["quirk@quirk-dev"]
                ? "quirk@quirk-dev"
                : Object.keys(plugins).find((k) => k.startsWith("quirk@"));
            const entry = key && plugins[key] && plugins[key][0];
            if (!entry || !entry.installPath) process.exit(1);
            process.stdout.write(entry.installPath);
        } catch { process.exit(1); }
    ' 2>/dev/null || true
}

PLUGIN_DIR=""
SOURCE_KIND=""

if [ -n "${PI_WATCH_DIR:-}" ] && [ -f "$PI_WATCH_DIR/pi-watch.mjs" ]; then
    PLUGIN_DIR="$PI_WATCH_DIR"; SOURCE_KIND="override"
elif [[ "$DIR" != "$CACHE_PREFIX"* ]] && [ -f "$DIR/pi-watch.mjs" ]; then
    PLUGIN_DIR="$DIR"; SOURCE_KIND="dev-checkout"
else
    INSTALL_PATH="$(resolve_installed)"
    if [ -n "$INSTALL_PATH" ] && [ -f "$INSTALL_PATH/$SUBPATH/pi-watch.mjs" ]; then
        PLUGIN_DIR="$INSTALL_PATH/$SUBPATH"; SOURCE_KIND="installed"
    elif [ -f "$DIR/pi-watch.mjs" ]; then
        PLUGIN_DIR="$DIR"; SOURCE_KIND="own-dir"
    fi
fi

if [ -z "$PLUGIN_DIR" ]; then
    echo "pi-watch: could not locate an installed quirk pi-dev skill." >&2
    echo "  Install/enable the quirk plugin, or set PI_WATCH_DIR to a checkout's" >&2
    echo "  skills/pi-dev/scripts/pi-watch directory." >&2
    exit 1
fi

# --- diagnostics: pi-watch --where -------------------------------------------
for a in "$@"; do
    if [ "$a" = "--where" ]; then
        if grep -q "gpt-6.1-sol" "$PLUGIN_DIR/pi-watch.mjs" 2>/dev/null; then
            ladder="has gpt-6 alias ladder"
        else
            ladder="pre-gpt-6 alias ladder (stale)"
        fi
        echo "source : $SOURCE_KIND"
        echo "dir    : $PLUGIN_DIR"
        echo "ladder : $ladder"
        exit 0
    fi
done

# --- ensure deps for the resolved version ------------------------------------
# Published versions bundle node_modules, but self-heal if a version dir lacks
# the SDK (e.g. shipped without deps, or after a manual cache prune), or holds
# an SDK older than package.json's "^X.Y.Z" floor (e.g. a pre-1.x pin).
sdk_stale() {
    node -e '
        const fs = require("fs"), path = require("path");
        const dir = process.argv[1];
        try {
            const want = JSON.parse(fs.readFileSync(path.join(dir, "package.json"), "utf8"))
                .dependencies["@earendil-works/pi-coding-agent"].replace(/^\^/, "").split(".").map(Number);
            const have = JSON.parse(fs.readFileSync(path.join(dir,
                "node_modules/@earendil-works/pi-coding-agent/package.json"), "utf8")).version.split(".").map(Number);
            const ok = have[0] === want[0] && (have[1] > want[1] || (have[1] === want[1] && have[2] >= want[2]));
            process.exit(ok ? 1 : 0);
        } catch { process.exit(0); }
    ' "$1" 2>/dev/null
}

if sdk_stale "$PLUGIN_DIR"; then
    if command -v pnpm >/dev/null 2>&1; then
        echo "pi-watch: installing dependencies in $PLUGIN_DIR (missing or older than package.json requires)…" >&2
        if ! (cd "$PLUGIN_DIR" && pnpm install --silent) >&2; then
            echo "pi-watch: 'pnpm install' failed in $PLUGIN_DIR." >&2
            exit 1
        fi
    else
        echo "pi-watch: dependencies missing or outdated in $PLUGIN_DIR and pnpm is not on PATH." >&2
        echo "  Install pnpm (https://pnpm.io/installation) or run 'pnpm install' there." >&2
        exit 1
    fi
fi

exec node "$PLUGIN_DIR/pi-watch.mjs" "$@"
