#!/usr/bin/env bash
# One-shot installer: register context-mode plugin into a local OpenClaw instance.
# Usage: ./scripts/install-openclaw-plugin.sh [OPENCLAW_STATE_DIR]
# Default OPENCLAW_STATE_DIR: /openclaw

set -euo pipefail

PLUGIN_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
OPENCLAW_STATE_DIR="${1:-${OPENCLAW_STATE_DIR:-/openclaw}}"

# — preflight checks —
if ! command -v node &>/dev/null; then
  echo "✗ node is required but not found in PATH" >&2
  exit 1
fi
if [ ! -d "$OPENCLAW_STATE_DIR" ]; then
  echo "✗ OPENCLAW_STATE_DIR ($OPENCLAW_STATE_DIR) does not exist. Is OpenClaw installed?" >&2
  exit 1
fi

# OpenClaw uses openclaw.json as the single config file.
# https://docs.openclaw.ai/tools/plugin — plugins.entries, plugins.allow
OPENCLAW_JSON="$OPENCLAW_STATE_DIR/openclaw.json"
if [ ! -f "$OPENCLAW_JSON" ]; then
  echo "✗ $OPENCLAW_JSON not found." >&2
  echo "  Start OpenClaw once first, then re-run this script." >&2
  exit 1
fi

EXT_DIR="$OPENCLAW_STATE_DIR/extensions/context-mode"

echo "→ context-mode plugin installer"
echo "  plugin root : $PLUGIN_ROOT"
echo "  openclaw    : $OPENCLAW_STATE_DIR"

# 1. Build TypeScript + rebuild native bindings for system Node
echo "→ building..."
npm --prefix "$PLUGIN_ROOT" install --silent
npm --prefix "$PLUGIN_ROOT" run build
npm --prefix "$PLUGIN_ROOT" rebuild better-sqlite3

# 2. Create global extension dir (real directory — OpenClaw does NOT follow dir symlinks)
echo "→ installing extension into $EXT_DIR..."
mkdir -p "$EXT_DIR"
cp "$PLUGIN_ROOT/.openclaw-plugin/openclaw.plugin.json" "$EXT_DIR/openclaw.plugin.json"
cat > "$EXT_DIR/index.ts" << TSEOF
// context-mode OpenClaw global extension — auto-generated by install script.
// Uses absolute path so this stub can live in any extensions directory.
export { default } from "$PLUGIN_ROOT/build/adapters/openclaw/plugin.js";
TSEOF

# 3. Clear jiti cache so stale compiled TS doesn't survive extension dir update
#    Match both the new (build-adapters-openclaw-plugin.*.cjs) and legacy
#    (build-openclaw-plugin.*.cjs) cache filenames so a host that ran an
#    older install still gets cleared on upgrade.
echo "→ clearing jiti cache for context-mode..."
rm -f /tmp/jiti/context-mode-index.*.cjs /tmp/jiti/build-adapters-openclaw-plugin.*.cjs /tmp/jiti/build-openclaw-plugin.*.cjs 2>/dev/null || true
echo "  ✓ jiti cache cleared"

# 4. Verify plugin is discovered by OpenClaw
echo "→ verifying discovery..."
OPENCLAW_BIN="$(command -v openclaw 2>/dev/null || echo "")"
if [ -z "$OPENCLAW_BIN" ]; then
  # Try common npm-global locations
  for candidate in "$HOME/.npm-global/bin/openclaw" "$HOME/.local/bin/openclaw" "$(npm root -g 2>/dev/null)/../bin/openclaw"; do
    [ -x "$candidate" ] && OPENCLAW_BIN="$candidate" && break
  done
fi
if [ -n "$OPENCLAW_BIN" ] && OPENCLAW_STATE_DIR="$OPENCLAW_STATE_DIR" "$OPENCLAW_BIN" plugins list 2>&1 | grep -q 'context-mode'; then
  echo "  ✓ context-mode discovered"
else
  echo "  ⚠ could not verify discovery (openclaw not in PATH or plugin not found) — continuing anyway"
fi

# 5. Register in runtime config (idempotent) — delegates to
# scripts/lib/register-openclaw-config.mjs so the logic can be unit-tested
# and so we can extend it (now also registers mcp.servers.context-mode).
echo "→ registering in $OPENCLAW_JSON..."
node "$PLUGIN_ROOT/scripts/lib/register-openclaw-config.mjs" "$OPENCLAW_JSON" "$PLUGIN_ROOT"

# 6. Restart gateway
echo "→ restarting openclaw gateway..."
GATEWAY_PID=$(pgrep -f "node.*openclaw/dist/index.js" 2>/dev/null | head -1 || true)
if [ -n "$GATEWAY_PID" ]; then
  # SIGUSR1 triggers a clean config reload/restart in OpenClaw
  kill -USR1 "$GATEWAY_PID" 2>/dev/null && echo "  ✓ SIGUSR1 sent to gateway (PID $GATEWAY_PID)" || echo "  ⚠ could not signal gateway"
else
  echo "  ⚠ gateway not running — start it manually: OPENCLAW_STATE_DIR=$OPENCLAW_STATE_DIR openclaw gateway start"
fi

echo ""
echo "✓ done — context-mode plugin installed and active"
