#!/usr/bin/env bash
set -euo pipefail
APP_NAME="Govibe Nexus Audio Player"
APP_ID="govibe-nexus-audio-player"
BIN_NAME="govibe-nexus-audio-player"
MODE="user"
case "${1:-}" in
  --system) MODE="system" ;;
  --help|-h)
    echo "Usage: ./uninstall.sh [--system]"
    echo "Default: removes current-user install. If run with sudo, removes from SUDO_USER's home."
    exit 0
    ;;
  "") ;;
  *) echo "Unknown option: $1" >&2; exit 1 ;;
esac
resolve_target_user() {
  if [ "$MODE" = "user" ] && [ "${EUID:-$(id -u)}" -eq 0 ] && [ -n "${SUDO_USER:-}" ] && [ "${SUDO_USER:-}" != "root" ]; then printf '%s\n' "$SUDO_USER"; else id -un; fi
}
resolve_home() {
  local user="$1" home=""
  if command -v getent >/dev/null 2>&1; then home="$(getent passwd "$user" | awk -F: '{print $6}' || true)"; fi
  [ -z "$home" ] && home="$(eval echo "~$user")"
  printf '%s\n' "$home"
}
TARGET_USER="$(resolve_target_user)"
TARGET_HOME="$(resolve_home "$TARGET_USER")"
run_root() { if [ "${EUID:-$(id -u)}" -eq 0 ]; then "$@"; elif command -v sudo >/dev/null 2>&1; then sudo "$@"; else echo "Root permission required for --system." >&2; exit 1; fi; }
remove_user() {
  rm -f "$TARGET_HOME/.local/bin/$BIN_NAME"
  rm -f "$TARGET_HOME/.local/share/applications/$APP_ID.desktop"
  if [ "$APP_ID" = "govibe-process-manager" ]; then
    rm -f "$TARGET_HOME/.local/share/applications/org.govibe.ProcessManager.desktop"
    rm -rf "$TARGET_HOME/.local/share/govibe-process-manager"
  fi
  rm -rf "$TARGET_HOME/.local/share/doc/$APP_ID" "$TARGET_HOME/.local/share/metainfo/org.govibe"* 2>/dev/null || true
  rm -f "$TARGET_HOME/.local/share/icons/hicolor/16x16/apps/$APP_ID.png" "$TARGET_HOME/.local/share/icons/hicolor/24x24/apps/$APP_ID.png" "$TARGET_HOME/.local/share/icons/hicolor/32x32/apps/$APP_ID.png" "$TARGET_HOME/.local/share/icons/hicolor/48x48/apps/$APP_ID.png" "$TARGET_HOME/.local/share/icons/hicolor/64x64/apps/$APP_ID.png" "$TARGET_HOME/.local/share/icons/hicolor/128x128/apps/$APP_ID.png" "$TARGET_HOME/.local/share/icons/hicolor/256x256/apps/$APP_ID.png" "$TARGET_HOME/.local/share/icons/hicolor/scalable/apps/$APP_ID.svg" "$TARGET_HOME/.local/share/pixmaps/$APP_ID.svg" 2>/dev/null || true
  if [ "$APP_ID" = "govibepcspecs" ]; then rm -rf "$TARGET_HOME/.local/lib/govibepcspecs"; fi
}
remove_system() {
  run_root rm -f "/usr/bin/$BIN_NAME"
  run_root rm -f "/usr/share/applications/$APP_ID.desktop"
  if [ "$APP_ID" = "govibe-process-manager" ]; then run_root rm -f /usr/share/applications/org.govibe.ProcessManager.desktop; run_root rm -rf /usr/share/govibe-process-manager; fi
  run_root rm -rf "/usr/share/doc/$APP_ID" 2>/dev/null || true
  run_root rm -f /usr/share/icons/hicolor/*/apps/$APP_ID.png /usr/share/icons/hicolor/scalable/apps/$APP_ID.svg /usr/share/pixmaps/$APP_ID.svg 2>/dev/null || true
  if [ "$APP_ID" = "govibepcspecs" ]; then run_root rm -rf /usr/lib/govibepcspecs; fi
}
if [ "$MODE" = "system" ]; then echo "Removing system-wide $APP_NAME..."; remove_system; else echo "Removing $APP_NAME from user: $TARGET_USER"; remove_user; fi
echo "Removed: $APP_NAME"
