#!/usr/bin/env bash
# adom-bridge installer — runs in the Adom container via `adom-wiki pkg install`.
#
# Bundled-artifact model (v1.8.147+): the Linux CLI companion + the Claude skill
# bundle ship INSIDE this package's tarball (dist/linux/, skills/). We SYMLINK
# them onto PATH / into ~/.claude/skills. Self-contained since v1.9.116: no
# external helper CLI is required (the pre-1.9.116 script hard-required the
# retired `adompkg` tool, so `adom-wiki pkg install` failed and rolled back on
# any container without it — wiki discussion #117). No download, no
# version.json dependency: the binary is right here, SHA-fixed by the published
# tarball.
#
# What it does NOT do: install the Windows/macOS GUI. That runs on the user's
# laptop (the signed Windows installer is a wiki release download; AD
# auto-updates itself). The skills tell Claude how to get the GUI onto the
# laptop.
set -euo pipefail
HERE="$(cd "$(dirname "$0")" && pwd)"
cd "$HERE"

# ln -sfn with a copy fallback (filesystems/sandboxes that refuse symlinks).
link_or_copy() { # <target> <linkpath>
  local target="$1" linkpath="$2"
  mkdir -p "$(dirname "$linkpath")"
  if ln -sfn "$target" "$linkpath" 2>/dev/null; then
    return 0
  fi
  rm -rf "$linkpath" 2>/dev/null || true
  cp -r "$target" "$linkpath"
}

OS="$(uname -s | tr '[:upper:]' '[:lower:]')"
case "$OS" in
  linux)
    CLI="$HERE/dist/linux/adom-bridge-cli"
    if [ ! -f "$CLI" ]; then
      echo "[adom-bridge] FATAL: bundled Linux CLI missing ($CLI). Package was published without staging." >&2
      exit 1
    fi
    chmod +x "$CLI"
    # v2.0.17: the CANONICAL CLI name is adom-bridge-cli (matches the executable
    # filename and what Hydrogen's baked adom-relay.service execs — a 2.0.16
    # install that exposed only "adom-bridge" left that service crash-looping
    # exit 127 and the relay dead). Link BOTH names: adom-bridge-cli is
    # authoritative; adom-bridge stays as a convenience alias.
    link_or_copy "$CLI" "$HOME/.local/bin/adom-bridge-cli"
    link_or_copy "$CLI" "$HOME/.local/bin/adom-bridge"
    # Remove the dead pre-rename CLI symlink (only if it is OUR old install:
    # a symlink into the retired adom-desktop module dir).
    old_cli="$HOME/.local/bin/adom-desktop"
    if [ -L "$old_cli" ] && readlink "$old_cli" | grep -q "adom_modules/adom/adom-desktop/"; then
      rm -f "$old_cli"
      echo "[adom-bridge] removed stale pre-rename CLI symlink ~/.local/bin/adom-desktop"
    fi
    case ":$PATH:" in
      *":$HOME/.local/bin:"*) ;;
      *) echo "[adom-bridge] NOTE: $HOME/.local/bin is not on PATH — add it to your shell rc." ;;
    esac
    echo "[adom-bridge] CLI -> $("$HOME/.local/bin/adom-bridge-cli" --version 2>/dev/null || echo installed)"
    ;;
  *mingw*|*msys*|*cygwin*)
    echo "[adom-bridge] On Windows, install the GUI from https://wiki.adom.inc/adom/adom-bridge (Download box). It bundles adom-bridge-cli.exe."
    ;;
  darwin)
    echo "[adom-bridge] macOS GUI not bundled yet — see https://wiki.adom.inc/adom/adom-bridge"
    ;;
  *)
    echo "[adom-bridge] unsupported OS: $OS" >&2
    exit 1
    ;;
esac

# v2.0.17: purge the PRE-RENAME ab-core skill dirs. The 2026-08-08 rename moved
# every skill slug adom-desktop-* -> adom-bridge-*, but installs only ADDED the
# new dirs — the old ones lingered and Claude kept loading both generations
# (John saw /adom-desktop-caller-identity in HD's container). Fixed list of
# exactly the slugs OUR old pkg shipped; bridge-owned skill pkgs (installed via
# sync_skills) and Hydrogen's own hd-*/hydrogen-* skills are deliberately
# untouched. Best-effort: cleanup failure must not fail the install.
LEGACY_SKILLS="adom-desktop adom-desktop-auth adom-desktop-bridge-ownership \
adom-desktop-bridge-sdk adom-desktop-bridge-sdk-manifest adom-desktop-bridge-sdk-runtime \
adom-desktop-bridge-sdk-callback adom-desktop-bridge-sdk-publish adom-desktop-bridge-sdk-audit \
adom-desktop-bridges-guide adom-desktop-caller-identity adom-desktop-cli-guide \
adom-desktop-direct-api adom-desktop-discovery adom-desktop-embedded adom-desktop-fonts \
adom-desktop-installer adom-desktop-local-ai adom-desktop-multi-ad adom-desktop-notify \
adom-desktop-permissions adom-desktop-presence adom-desktop-recording adom-desktop-runtimes \
adom-desktop-thesis adom-desktop-window-capture adom-desktop-window-identity \
adom-desktop-windows-host"
purged=0
for s in $LEGACY_SKILLS; do
  if [ -e "$HOME/.claude/skills/$s" ]; then
    rm -rf "$HOME/.claude/skills/$s" 2>/dev/null && purged=$((purged + 1)) || true
  fi
done
[ "$purged" -gt 0 ] && echo "[adom-bridge] purged $purged pre-rename adom-desktop-* skill dir(s)"

# Claude skill bundle — symlink each skills/<slug>/ into ~/.claude/skills/<slug>.
# Best-effort per skill: one bad slug must not roll back the whole install.
if [ -d "$HERE/skills" ]; then
  linked=0
  for d in "$HERE"/skills/*/; do
    [ -f "$d/SKILL.md" ] || continue
    slug="$(basename "$d")"
    if link_or_copy "${d%/}" "$HOME/.claude/skills/$slug"; then
      linked=$((linked + 1))
    else
      echo "[adom-bridge] WARN: could not link skill $slug (continuing)" >&2
    fi
  done
  echo "[adom-bridge] linked $linked skill(s) into ~/.claude/skills/"
fi

echo "[adom-bridge] done. Try: adom-bridge-cli ping   (needs the Adom Bridge GUI running on the user's laptop)"
