#!/bin/sh
# hms installer: downloads the hms wheel, checks its SHA-256, installs it into its own virtual environment and
# registers the MCP server with Claude Code and Codex. Nothing runs with sudo.
#   curl -fsSL https://lavett.dev/hms/latest/install.sh | sh
# HMS_DOWNLOAD_BASE overrides where the wheel comes from; HMS_PREFIX where the environment goes.
set -eu
VERSION=0.2.0
WHEEL=hms-0.2.0-py3-none-any.whl
SHA256=b9a8d470c236635c78ddfdaae601b7955f34b073c66c20c90b81eb2288df740e
BASE=${HMS_DOWNLOAD_BASE:-https://lavett.dev/hms}
PREFIX=${HMS_PREFIX:-${XDG_DATA_HOME:-$HOME/.local/share}/hms/venv}

say() { printf 'hms: %s\n' "$*"; }
die() { printf 'hms: %s\n' "$*" >&2; exit 1; }

case $(uname -s) in
  # The contained session runs the system Python with the distro's D-Bus and AT-SPI bindings: the
  # environment is built on that interpreter and sees its packages.
  Linux) OS=linux; PY=/usr/bin/python3; VENV_OPTS=--system-site-packages ;;
  Darwin) OS=mac; PY=$(command -v python3 || true); VENV_OPTS= ;;
  *) die "unsupported system $(uname -s): hms runs on Linux (KDE Plasma 6) and macOS" ;;
esac
[ -n "$PY" ] && [ -x "$PY" ] || die "python3 not found"
"$PY" -c 'import sys; sys.exit(sys.version_info < (3, 12))' || die "hms needs Python 3.12 or newer ($PY is older)"
if [ "$OS" = mac ] && ! xcode-select -p >/dev/null 2>&1; then
  die "the macOS session helper is built with Xcode's command line tools: run xcode-select --install, then this again"
fi

TMP=$(mktemp -d)
trap 'rm -rf "$TMP"' EXIT INT TERM
say "downloading hms $VERSION"
curl -fsSL -o "$TMP/$WHEEL" "$BASE/v$VERSION/$WHEEL" || die "download failed: $BASE/v$VERSION/$WHEEL"
if command -v sha256sum >/dev/null 2>&1; then GOT=$(sha256sum "$TMP/$WHEEL" | cut -d' ' -f1)
else GOT=$(shasum -a 256 "$TMP/$WHEEL" | cut -d' ' -f1); fi
[ "$GOT" = "$SHA256" ] || die "checksum mismatch for $WHEEL: expected $SHA256, got $GOT"

say "installing into $PREFIX"
"$PY" -m venv $VENV_OPTS "$PREFIX"
"$PREFIX/bin/python" -m pip install -q --disable-pip-version-check --upgrade "$TMP/$WHEEL"
"$PREFIX/bin/hms" install all || say "nothing registered yet: install Claude Code or Codex, then run: hms install all"
case ":$PATH:" in *":$HOME/.local/bin:"*) ;; *) say "add $HOME/.local/bin to PATH to run hms from a terminal" ;; esac

if [ "$OS" = linux ]; then
  # The session's requirements come from the distribution. Until hms doctor finds them all, hms cannot start one,
  # so the install does not count as done.
  if ! "$PREFIX/bin/hms" doctor --json 2>/dev/null | "$PREFIX/bin/python" -c '
import json, sys
try:
    problems = json.load(sys.stdin).get("problems") or []
except ValueError:
    problems = ["hms doctor did not run"]
for p in problems:
    print("hms: missing:", p)
sys.exit(1 if problems else 0)
'; then
    say "install them with your package manager (Arch: kwin at-spi2-core python-gobject python-dbus python-numpy"
    say "  python-pillow python-websockets tesseract tesseract-data-eng bubblewrap libei firefox pipewire gcc pkgconf wayland)"
    die "hms $VERSION is installed, but it cannot start a session until hms doctor finds everything above"
  fi
else
  PKG=$("$PREFIX/bin/python" -c 'import hms, pathlib; print(pathlib.Path(hms.__file__).parent)')
  say "one-time macOS setup (creates hms's own hidden login session; asks for your password):"
  say "  sudo sh \"$PKG/native/macos-session-setup.sh\""
  say "then give hms.app Accessibility and Screen Recording in System Settings > Privacy & Security,"
  say "and run hms from a terminal on the Mac's desktop (the keychain and the CLIs' logins are not reachable over SSH)"
fi
say "installed hms $VERSION. Ask Claude Code or Codex for GUI work, or run: hms --help"
