#!/usr/bin/env bash
# Fully uninstall Adom Hydrogen (macOS): the app, all Hydrogen state, the workspace
# machine + Hydrogen's downloaded Lima runtime + cached golden image, and the webview data.
#
# Surgical about Lima: deletes ONLY Hydrogen'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/Adom Hydrogen.app"
SUPPORT="$HOME/Library/Application Support"
log() { echo "  $*"; }

echo "Uninstalling Adom Hydrogen (macOS)…"

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

# 2. Tear down Hydrogen's Lima VM (only hd-builder). Find a usable limactl: Hydrogen'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 (both the current and the pre-rename bundle name).
if [ -d "$APP" ]; then rm -rf "$APP" && log "removed $APP"; else log "app not in /Applications"; fi
rm -rf "/Applications/Hydrogen Desktop.app" 2>/dev/null || true

# 4. Remove ALL Hydrogen state: app data, downloaded Lima + cached image, webview data,
#    prefs, and saved window state.
rm -rf \
  "$SUPPORT/adom-hydrogen" \
  "$SUPPORT/hydrogen-desktop" \
  "$SUPPORT/adom" \
  "$HOME/Library/Caches/inc.adom.hydrogen" \
  "$HOME/Library/WebKit/inc.adom.hydrogen" \
  "$HOME/Library/HTTPStorages/inc.adom.hydrogen" \
  "$HOME/Library/HTTPStorages/inc.adom.hydrogen.binarycookies" \
  "$HOME/Library/Preferences/inc.adom.hydrogen.plist" \
  "$HOME/Library/Saved Application State/inc.adom.hydrogen.savedState" \
  "$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: Adom Hydrogen fully uninstalled."
