#!/usr/bin/env bash
#
# DebloatMe - GNOME desktop environment
# https://debloatme.com/gnome
#
# Lets you pick, from a checklist, which bundled apps to remove
# (GNOME Tour, Weather, Maps, LibreOffice, and a broader list of common
# extras) - nothing is removed unless you select it - and applies a few
# privacy-related gsettings changes.
#
# Reads confirmation prompts from /dev/tty so this still works when the
# script is piped straight from curl (curl ... | bash).
#
# Review this script before running it.

set -euo pipefail

echo "== DebloatMe: GNOME =="
echo "This removes selected apps from the list below - nothing else changes."
read -r -p "Continue? [y/N] " confirm </dev/tty
if [[ ! "$confirm" =~ ^[Yy]$ ]]; then
  echo "Aborted. No changes made."
  exit 0
fi

remove_pkg() {
  local pkg="$1"
  if command -v dpkg >/dev/null 2>&1 && dpkg -s "$pkg" >/dev/null 2>&1; then
    sudo apt-get -y purge "$pkg" || true
  elif command -v pacman >/dev/null 2>&1 && pacman -Qi "$pkg" >/dev/null 2>&1; then
    sudo pacman -Rns --noconfirm "$pkg" || true
  elif command -v rpm >/dev/null 2>&1 && rpm -q "$pkg" >/dev/null 2>&1; then
    sudo zypper --non-interactive remove "$pkg" 2>/dev/null || sudo dnf -y remove "$pkg" || true
  fi
}

is_installed() {
  local pkg="$1"
  if command -v dpkg >/dev/null 2>&1 && dpkg -s "$pkg" >/dev/null 2>&1; then return 0; fi
  if command -v pacman >/dev/null 2>&1 && pacman -Qi "$pkg" >/dev/null 2>&1; then return 0; fi
  if command -v rpm >/dev/null 2>&1 && rpm -q "$pkg" >/dev/null 2>&1; then return 0; fi
  return 1
}

echo
echo "-> Scanning for optional installed apps..."

candidate_pkgs=()
candidate_labels=()

check_candidate() {
  local pkg="$1" label="$2"
  if is_installed "$pkg"; then
    candidate_pkgs+=("$pkg")
    candidate_labels+=("$label")
  fi
}

check_candidate "libreoffice" "LibreOffice (full suite)"
check_candidate "gnome-tour" "GNOME Tour"
check_candidate "gnome-weather" "GNOME Weather"
check_candidate "gnome-maps" "GNOME Maps"
check_candidate "gnome-contacts" "GNOME Contacts"
check_candidate "gnome-clocks" "GNOME Clocks"
check_candidate "gnome-calculator" "GNOME Calculator"
check_candidate "gnome-music" "GNOME Music"
check_candidate "gnome-characters" "GNOME Character Map"
check_candidate "gnome-logs" "GNOME Logs"
check_candidate "gnome-boxes" "GNOME Boxes"
check_candidate "gnome-connections" "GNOME Connections"
check_candidate "yelp" "GNOME User Docs (Yelp)"
check_candidate "orca" "Orca screen reader"
check_candidate "simple-scan" "Simple Scan"
check_candidate "totem" "GNOME Videos (Totem)"
check_candidate "rhythmbox" "Rhythmbox"
check_candidate "cheese" "Cheese (webcam booth)"
check_candidate "shotwell" "Shotwell (photo manager)"
check_candidate "gnome-2048" "GNOME 2048"
check_candidate "aisleriot" "Aisleriot (Solitaire)"
check_candidate "iagno" "Iagno (Reversi)"
check_candidate "four-in-a-row" "Four-in-a-row"
check_candidate "gnome-mahjongg" "GNOME Mahjongg"
check_candidate "gnome-mines" "GNOME Mines"
check_candidate "gnome-sudoku" "GNOME Sudoku"
check_candidate "seahorse" "Seahorse (password/keys manager)"
check_candidate "transmission-gtk" "Transmission (torrent client)"
check_candidate "deja-dup" "Deja Dup (backup tool)"
check_candidate "remmina" "Remmina (remote desktop client)"

if [[ ${#candidate_pkgs[@]} -eq 0 ]]; then
  echo "   None of the optional apps on the list are installed."
else
  echo
  echo "Optional apps found on this system:"
  for i in "${!candidate_pkgs[@]}"; do
    printf "  [%d] %s\n" "$((i + 1))" "${candidate_labels[$i]}"
  done
  echo
  read -r -p "Enter numbers to remove (space-separated), 'all', or press Enter to skip: " selection </dev/tty

  to_remove=()
  if [[ "$selection" == "all" ]]; then
    to_remove=("${candidate_pkgs[@]}")
  elif [[ -n "$selection" ]]; then
    for n in $selection; do
      idx=$((n - 1))
      if [[ $idx -ge 0 && $idx -lt ${#candidate_pkgs[@]} ]]; then
        to_remove+=("${candidate_pkgs[$idx]}")
      fi
    done
  fi

  if [[ ${#to_remove[@]} -eq 0 ]]; then
    echo "   Nothing selected. Skipping."
  else
    for pkg in "${to_remove[@]}"; do
      echo "   Removing $pkg"
      remove_pkg "$pkg"
    done
  fi
fi

echo
echo "-> Applying privacy-related gsettings changes (where supported)..."
gsettings set org.gnome.desktop.privacy report-technical-problems false 2>/dev/null || true
gsettings set org.gnome.desktop.privacy send-software-usage-stats false 2>/dev/null || true
gsettings set org.gnome.desktop.search-providers disable-external true 2>/dev/null || true

echo
echo "== Done. GNOME remains installed and functional. =="

rm -- "$0" 2>/dev/null || true
