#!/usr/bin/env bash
# adom-google installer — runs via `adom-wiki pkg install adom/adom-google`.
#
# The pkg tarball is a LEAN BOOTSTRAP (skills + docs + this script — no binaries,
# no images). The real CLIs are release assets, pulled from this package's OWN
# release version: the VERSION file ships next to this script and assets live at
#   /download/<owner>/<slug>/<version>/<filename>
# There is no "latest" download alias on the wiki; pinning to our own VERSION
# means tarball and binary can never skew.
#
# Skillpack layout: MAIN skill = repo-root SKILL.md (adom-google); sub-skills live
# under skills/<name>/SKILL.md. Installed by COPYING real files (NO symlinks).
set -euo pipefail
HERE="$(cd "$(dirname "$0")" && pwd)"
cd "$HERE"

VERSION="$(tr -d '[:space:]' < "$HERE/VERSION")"
BASE_URL="https://wiki.adom.inc/download/adom/adom-google/$VERSION"
BIN_DIR="$HOME/.local/bin"
SKILLS_DIR="$HOME/.claude/skills"
mkdir -p "$BIN_DIR" "$SKILLS_DIR"

fetch_bin() {              # download $BASE_URL/<name> → ~/.local/bin/<name>
  local name="$1" dest="$BIN_DIR/$1"
  # Download to a temp file and rename over dest: writing directly onto a
  # RUNNING binary fails with ETXTBSY (curl exit 23); rename(2) replaces a
  # busy executable cleanly.
  local tmp="$dest.tmp.$$"
  trap 'rm -f "$tmp"' RETURN
  curl -fsSL "$BASE_URL/$name" -o "$tmp"
  chmod +x "$tmp"
  rm -rf "$dest"           # clear any old file OR symlink
  mv -f "$tmp" "$dest"
  echo "   bin   → $dest"
}

install_skill() {          # install_skill <install-name> <source SKILL.md path>
  local name="$1" src="$2"
  [ -f "$src" ] || { echo "[adom-google] WARN: $src missing" >&2; return; }
  rm -rf "$SKILLS_DIR/$name"            # clear any prior dir OR symlink from an old install
  mkdir -p "$SKILLS_DIR/$name"
  cp "$src" "$SKILLS_DIR/$name/SKILL.md"
  echo "   skill → $SKILLS_DIR/$name/SKILL.md"
}

echo "[adom-google] installing v$VERSION (binaries from wiki release assets)…"
fetch_bin     adom-google
fetch_bin     adom-gmail                                            # deprecation shim → adom-google
install_skill adom-google             SKILL.md                      # MAIN skill (repo root)
install_skill adom-google-onboarding  skills/adom-google-onboarding/SKILL.md   # sub-skill

INSTALLED_VER="$("$BIN_DIR/adom-google" --version 2>/dev/null | awk '{print $NF}' || echo '?')"
echo "   adom-google --version → $INSTALLED_VER"

case ":$PATH:" in
  *":$HOME/.local/bin:"*) ;;
  *) echo "[adom-google] NOTE: $HOME/.local/bin is not on PATH — add it to your shell rc." ;;
esac

echo "✅ adom-google installed (main skill + onboarding sub-skill). It ships with NO OAuth provider — set one up:"
echo "   • personal @gmail / any org (zero setup): adom-google connect-personal   (Adom's hosted Connector)"
echo "   • new org (self-serve): adom-google onboard   (the AI drives your browser through Google setup)"
echo "   • self-host (manual):   adom-google provider set --gateway https://your-gateway --client-id <id>"
echo "   • Adom staff:           adom-wiki pkg install adom/adom-google-adom   (drops the Adom provider)"
echo "   Then: 'adom-google setup' (asks safe vs full) and 'adom-google auth'."
