#!/bin/bash
set -euo pipefail

echo "=== Cleaning up Remote Desktop setup ==="

# Remove VNC config
echo "[1/5] Removing VNC config..."
rm -rf ~/.vnc

# Remove Brave
echo "[2/5] Removing Brave..."
sudo apt remove -y --purge brave-browser 2>/dev/null || true
sudo rm -f /usr/share/keyrings/brave-browser-archive-keyring.gpg
sudo rm -f /etc/apt/sources.list.d/brave-browser-release.list
sudo rm -rf /etc/brave
sudo rm -f /usr/bin/brave-browser-stable
rm -rf ~/.config/BraveSoftware

# Remove Chromium (snap + deb)
echo "[3/5] Removing Chromium..."
sudo snap remove chromium 2>/dev/null || true
sudo apt remove -y --purge chromium-browser 2>/dev/null || true
rm -rf ~/.config/chromium

# Remove XFCE, TigerVNC, dbus-x11
echo "[4/5] Removing XFCE, TigerVNC, dbus-x11..."
sudo apt remove -y --purge xfce4 xfce4-goodies tigervnc-standalone-server tigervnc-common dbus-x11 2>/dev/null || true
sudo apt autoremove -y 2>/dev/null || true
rm -rf ~/.config/xfce4
rm -rf ~/.cache/xfce4

# Remove keyring data
echo "[5/5] Removing keyring data..."
rm -rf ~/.local/share/keyrings
rm -f ~/.config/autostart/gnome-keyring-*.desktop
sudo apt install -y -qq gnome-keyring > /dev/null 2>&1 || true  # Restore if needed by other apps

# Remove env vars
ENV_FILE="$(cd "$(dirname "$0")/.." && pwd)/.env"
if [ -f "$ENV_FILE" ]; then
  sed -i '/^VNC_PASSWORD=/d' "$ENV_FILE"
  sed -i '/^VNC_PORT=/d' "$ENV_FILE"
  echo "Removed VNC vars from .env"
fi

echo ""
echo "Done. All desktop packages and config removed."
echo "The app code (routes, components) is still in place."
