#!/usr/bin/env bash
# Uninstall on macOS: remove the gog-* skill symlinks this repo installed into
# the per-user Claude skills directory. Only removes symlinks pointing back into
# this repo. Pass --gog to also remove a gog binary this repo installed via the
# release fallback (Homebrew-managed gog is left for `brew uninstall`).
# (Linux: ./uninstall-linux.sh   Windows: uninstall.ps1)
#
#   ./uninstall-macos.sh            # remove skill links only
#   ./uninstall-macos.sh --gog      # also remove a self-installed gog binary
#   CLAUDE_SKILLS_DIR=... ./uninstall-macos.sh
set -euo pipefail

REMOVE_GOG=0
for a in "$@"; do
  case "$a" in
    --gog|--all) REMOVE_GOG=1 ;;
    -h|--help) sed -n '2,12p' "$0" | sed 's/^# \{0,1\}//'; exit 0 ;;
    *) echo "error: unknown option '$a'" >&2; exit 2 ;;
  esac
done

if [ "$(uname -s)" != "Darwin" ]; then
  echo "error: this is the macOS uninstaller; OS is '$(uname -s)'." >&2
  [ "$(uname -s)" = "Linux" ] && echo "       On Linux run: ./uninstall-linux.sh" >&2
  exit 1
fi

REPO_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
DEST_DIR="${CLAUDE_SKILLS_DIR:-$HOME/.claude/skills}"

removed=0
for dest in "$DEST_DIR"/gog-*; do
  [ -L "$dest" ] || continue
  target="$(readlink "$dest")"
  case "$target" in
    "$REPO_DIR"/*)
      rm "$dest"; echo "removed  $(basename "$dest")"; removed=$((removed + 1)) ;;
    *)
      echo "kept     $(basename "$dest") (points elsewhere: $target)" >&2 ;;
  esac
done
echo "Removed $removed skill symlink(s) from $DEST_DIR."

if [ "$REMOVE_GOG" -eq 1 ]; then
  echo
  binpath="$(command -v gog 2>/dev/null || true)"
  if [ -z "$binpath" ]; then
    echo "gog: not on PATH; nothing to remove."
  else
    case "$binpath" in
      "${GOG_BIN_DIR:-$HOME/.local/bin}/gog"|"$HOME/.local/bin/gog")
        rm -f "$binpath"; echo "gog: removed $binpath" ;;
      *Homebrew*|*Cellar*|*/brew/*)
        echo "gog: Homebrew-managed ($binpath) — remove with: brew uninstall openclaw/tap/gogcli" ;;
      *)
        echo "gog: $binpath was not installed by this repo; leaving it in place." ;;
    esac
  fi
fi
