#!/usr/bin/env bash
# Fully uninstall Hydrogen Desktop (macOS): the app, all HD state, the workspace
# machine + HD's downloaded Lima runtime + cached golden image, and the webview data.
#
# Surgical about Lima: deletes ONLY HD's `hd-builder` VM — never other Lima VMs the
# user may have, and never the shared Lima download cache (left in place; the script
# tells you how to reclaim it).
#
# Idempotent — safe to re-run; skips whatever is already gone.
set -uo pipefail   # NOT -e: keep going past pieces that are already absent

APP="/Applications/Hydrogen Desktop.app"
SUPPORT="$HOME/Library/Application Support"
log() { echo "  $*"; }

echo "Uninstalling Hydrogen Desktop (macOS)…"

# 1. Quit HD (and its embedded Adom Desktop) if running.
osascript -e 'quit app "Hydrogen Desktop"' >/dev/null 2>&1 || true
pkill -f "Hydrogen Desktop" 2>/dev/null || true
pkill -f "Adom Desktop" 2>/dev/null || true
sleep 1

# 2. Tear down HD's Lima VM (only hd-builder). Find a usable limactl: HD's downloaded
#    copy, a system install, or the one bundled in the app — whichever exists.
LIMACTL=""
for c in "$SUPPORT/adom/lima/bin/limactl" /opt/homebrew/bin/limactl /usr/local/bin/limactl \
         "$APP/Contents/Resources/lima/bin/limactl"; do
  [ -x "$c" ] && LIMACTL="$c" && break
done
if [ -n "$LIMACTL" ] && "$LIMACTL" list 2>/dev/null | grep -q '^hd-builder'; then
  log "stopping + deleting Lima VM hd-builder…"
  "$LIMACTL" stop   -f hd-builder >/dev/null 2>&1 || true
  "$LIMACTL" delete -f hd-builder >/dev/null 2>&1 || true
else
  log "no hd-builder Lima VM (or no limactl) — skipping VM teardown"
fi

# 3. Remove the app.
if [ -d "$APP" ]; then rm -rf "$APP" && log "removed $APP"; else log "app not in /Applications"; fi

# 4. Remove ALL HD state: app data, downloaded Lima + cached image, webview data,
#    prefs, and saved window state.
rm -rf \
  "$SUPPORT/hydrogen-desktop" \
  "$SUPPORT/adom" \
  "$HOME/Library/Caches/inc.adom.hydrogen-desktop" \
  "$HOME/Library/WebKit/inc.adom.hydrogen-desktop" \
  "$HOME/Library/HTTPStorages/inc.adom.hydrogen-desktop" \
  "$HOME/Library/HTTPStorages/inc.adom.hydrogen-desktop.binarycookies" \
  "$HOME/Library/Preferences/inc.adom.hydrogen-desktop.plist" \
  "$HOME/Library/Saved Application State/inc.adom.hydrogen-desktop.savedState" 2>/dev/null || true
log "removed app data, downloaded Lima, cached image, and webview data (cookies/session)"

# 5. If ~/.lima now holds no VMs (only _config), remove it. Never touch other VMs.
if [ -d "$HOME/.lima" ]; then
  vms="$(find "$HOME/.lima" -mindepth 1 -maxdepth 1 -type d ! -name '_*' 2>/dev/null | wc -l | tr -d ' ')"
  if [ "$vms" = "0" ]; then rm -rf "$HOME/.lima" && log "removed empty ~/.lima"
  else log "left ~/.lima — it still has $vms other Lima VM(s)"; fi
fi

# 6. Shared Lima download cache: left in place (it's keyed by URL and shared with any
#    other Lima usage). Reclaim it manually if you don't use Lima elsewhere.
if [ -d "$HOME/Library/Caches/lima" ]; then
  log "note: ~/Library/Caches/lima (shared Lima image cache, ~600MB) left in place —"
  log "      'rm -rf ~/Library/Caches/lima' to reclaim it if Lima isn't used elsewhere."
fi

echo "OK: Hydrogen Desktop fully uninstalled."