#!/usr/bin/env bash
#
# Removes a Nuages install created by install-linux.sh.
#
#   ./uninstall-linux.sh            # per-user
#   ./uninstall-linux.sh --system   # system-wide (uses sudo)
#
set -euo pipefail

MODE="user"
while [[ $# -gt 0 ]]; do
    case "$1" in
        --system) MODE="system"; shift ;;
        *) echo "Unknown option: $1" >&2; exit 1 ;;
    esac
done

if [[ "$MODE" == "system" ]]; then
    SUDO="sudo"; [[ "$(id -u)" -eq 0 ]] && SUDO=""
    INSTALL_DIR="/opt/nuages"
    DESKTOP_FILE="/usr/share/applications/nuages.desktop"
    ICON="/usr/share/icons/hicolor/512x512/apps/nuages.png"
    BIN_LINK="/usr/local/bin/nuages"
else
    SUDO=""
    INSTALL_DIR="$HOME/.local/share/nuages"
    DESKTOP_FILE="$HOME/.local/share/applications/nuages.desktop"
    ICON="$HOME/.local/share/icons/hicolor/512x512/apps/nuages.png"
    BIN_LINK="$HOME/.local/bin/nuages"
fi

echo "Uninstalling Nuages ($MODE)"
pkill -f "$INSTALL_DIR" 2>/dev/null || true

$SUDO rm -rf "$INSTALL_DIR"
$SUDO rm -f "$DESKTOP_FILE" "$ICON" "$BIN_LINK"
echo "  - files, launcher, icon and symlink removed"

if command -v update-desktop-database >/dev/null 2>&1; then
    $SUDO update-desktop-database "$(dirname "$DESKTOP_FILE")" 2>/dev/null || true
fi

echo "Done."
