#!/usr/bin/env bash
set -euo pipefail

USER_INSTALL=1           # --user (default) or --system
APP_NAME="ScanXm"
APP_ID="scanxm"
DESKTOP_FILENAME="${APP_ID}.desktop"

CURRENT_USER="$(id -un 2>/dev/null || echo "${LOGNAME:-unknown}")"

# XDG-style config path for this app (per-user)
# Will be: $XDG_CONFIG_HOME/ScanXm/init.ini or $HOME/.config/ScanXm/init.ini
CONFIG_BASE="${XDG_CONFIG_HOME:-"$HOME/.config"}"
USER_CONFIG_DIR="${CONFIG_BASE}/${APP_NAME}"
USER_CONFIG_FILE="${USER_CONFIG_DIR}/init.ini"

# Args
while [[ $# -gt 0 ]]; do
  case "$1" in
    --user) USER_INSTALL=1 ;;
    --system) USER_INSTALL=0 ;;
    -h|--help)
      cat <<EOF
Usage: $0 [--user|--system]

--user    Uninstall for current user (default)
--system  Uninstall system-wide (requires root)
EOF
      exit 0
      ;;
    *)
      echo "Unknown option: $1" >&2
      exit 1
      ;;
  esac
  shift
done

# Paths (user)
USER_BIN_LINK="$HOME/.local/bin/${APP_ID}"
USER_APPS_DIR="$HOME/.local/share/applications"
USER_HICOLOR_DIR="$HOME/.local/share/icons/hicolor"
USER_ICON_FILE="$USER_HICOLOR_DIR/256x256/apps/${APP_ID}.png"
if command -v xdg-user-dir >/dev/null 2>&1; then
  USER_DESKTOP_DIR="$(xdg-user-dir DESKTOP || true)"
fi
USER_DESKTOP_DIR="${USER_DESKTOP_DIR:-"$HOME/Desktop"}"
USER_DESKTOP_FILE="$USER_DESKTOP_DIR/${APP_NAME}.desktop"

# Paths (system)
SYSTEM_BIN_LINK="/usr/local/bin/${APP_ID}"
SYSTEM_APPS_DIR="/usr/share/applications"
SYSTEM_HICOLOR_DIR="/usr/share/icons/hicolor"
SYSTEM_ICON_FILE="$SYSTEM_HICOLOR_DIR/256x256/apps/${APP_ID}.png"

have_cmd() { command -v "$1" >/dev/null 2>&1; }

refresh_caches_user() {
  have_cmd update-desktop-database && update-desktop-database "$USER_APPS_DIR" >/dev/null 2>&1 || true
  have_cmd gtk-update-icon-cache && gtk-update-icon-cache -f "$USER_HICOLOR_DIR" >/dev/null 2>&1 || true
  have_cmd xdg-desktop-menu && xdg-desktop-menu forceupdate >/dev/null 2>&1 || true
}

refresh_caches_system() {
  have_cmd update-desktop-database && update-desktop-database "$SYSTEM_APPS_DIR" >/dev/null 2>&1 || true
  have_cmd gtk-update-icon-cache && gtk-update-icon-cache -f "$SYSTEM_HICOLOR_DIR" >/dev/null 2>&1 || true
  have_cmd xdg-desktop-menu && xdg-desktop-menu forceupdate >/dev/null 2>&1 || true
}

uninstall_user() {
  echo "[*] Uninstalling for user: ${CURRENT_USER}"

  # Remove user-level launcher bits
  rm -f "$USER_BIN_LINK"
  rm -f "$USER_APPS_DIR/${DESKTOP_FILENAME}"
  rm -f "$USER_ICON_FILE"
  rm -f "$USER_DESKTOP_FILE"

  # Remove per-user config file
  if [[ -f "$USER_CONFIG_FILE" ]]; then
    echo "[*] Removing user config file: $USER_CONFIG_FILE"
    rm -f "$USER_CONFIG_FILE"
  fi

  # Optionally remove the config directory if it's empty
  if [[ -d "$USER_CONFIG_DIR" ]]; then
    rmdir "$USER_CONFIG_DIR" 2>/dev/null || true
  fi

  refresh_caches_user

  echo "[+] Removed user launcher, symlink, icon, Desktop shortcut, and config file (if present)."
}

uninstall_system() {
  if [[ "$(id -u)" -ne 0 ]]; then
    echo "ERROR: --system uninstall requires root. Re-run with sudo." >&2
    exit 1
  fi

  echo "[*] Uninstalling system-wide"

  rm -f "$SYSTEM_BIN_LINK"
  rm -f "$SYSTEM_APPS_DIR/${DESKTOP_FILENAME}"
  rm -f "$SYSTEM_ICON_FILE"

  refresh_caches_system

  echo "[+] Removed system launcher, symlink, and icon."
  echo "[i] Per-user configuration and Desktop shortcuts (if any) must be removed by each user (or via --user)."
}

if [[ "$USER_INSTALL" -eq 1 ]]; then
  uninstall_user
else
  uninstall_system
fi
