#!/bin/sh
set -eu

script_dir=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
xdg_data_home=${XDG_DATA_HOME:-"$HOME/.local/share"}
install_root="$xdg_data_home/platformless/apps/com.platformless.button-game"
case "$script_dir" in
  */platformless/apps/com.platformless.button-game) install_root="$script_dir" ;;
esac
desktop_entry="$xdg_data_home/applications/com.platformless.button-game.desktop"
icon_file="$xdg_data_home/icons/hicolor/256x256/apps/com.platformless.button-game.png"

case "$install_root" in
  */platformless/apps/com.platformless.button-game) ;;
  *)
    echo "refusing to uninstall unexpected launcher path: $install_root" >&2
    exit 1
    ;;
esac


remove_user_data=""
case "${1:-}" in
  "") ;;
  --remove-saves) remove_user_data="--remove-user-data" ;;
  --help|-h)
    echo "Usage: uninstall.sh [--remove-saves]"
    exit 0
    ;;
  *)
    echo "unknown option: $1" >&2
    exit 2
    ;;
esac
if [ -f "$install_root/install-plan.json" ]; then
  "$install_root/bin/launcher-core" uninstall-from-plan \
    --plan "$install_root/install-plan.json" $remove_user_data
fi

rm -f "$desktop_entry" "$icon_file"
if [ -d "$install_root" ]; then
  rm -rf "$install_root"
fi
rmdir "$xdg_data_home/platformless/apps" 2>/dev/null || true

echo "Uninstalled Button Game. Saves were preserved unless --remove-saves was requested."
