#!/usr/bin/env bash
#
# DebloatMe - Debian
# https://debloatme.com/debian
#
# What this script does:
#   1. Lets you pick, from a checklist, which optional apps to remove
#      (LibreOffice, default games, and a broader list of common
#      desktop bloat) - nothing is removed unless you select it
#   2. Runs apt autoremove with purge
#   3. Clears the apt package cache
#
# 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. Some listed packages may be relied
# on by other software on your system; the script checks before removing.

set -euo pipefail

echo "== DebloatMe: Debian =="
echo "This will remove selected packages and clear the apt cache."
read -r -p "Continue? [y/N] " confirm </dev/tty
if [[ ! "$confirm" =~ ^[Yy]$ ]]; then
  echo "Aborted. No changes made."
  exit 0
fi

# 1. Interactive checklist - nothing removed unless you pick it
echo
echo "-> Scanning for optional installed apps..."

candidate_ids=()
candidate_labels=()

lo_installed=$(dpkg -l 2>/dev/null | awk '/^ii  libreoffice/ {print $2}')
if [[ -n "$lo_installed" ]]; then
  candidate_ids+=("__libreoffice__")
  candidate_labels+=("LibreOffice (full suite)")
fi

check_pkg() {
  local pkg="$1" label="$2"
  if dpkg -s "$pkg" >/dev/null 2>&1; then
    candidate_ids+=("$pkg")
    candidate_labels+=("$label")
  fi
}

check_pkg "gnome-mines" "GNOME Mines"
check_pkg "gnome-sudoku" "GNOME Sudoku"
check_pkg "aisleriot" "Aisleriot (Solitaire)"
check_pkg "gnome-2048" "GNOME 2048"
check_pkg "iagno" "Iagno (Reversi)"
check_pkg "four-in-a-row" "Four-in-a-row"
check_pkg "popularity-contest" "Popularity Contest (usage stats)"
check_pkg "rhythmbox" "Rhythmbox"
check_pkg "totem" "GNOME Videos (Totem)"
check_pkg "cheese" "Cheese (webcam booth)"
check_pkg "shotwell" "Shotwell (photo manager)"
check_pkg "simple-scan" "Simple Scan"
check_pkg "transmission-gtk" "Transmission (torrent client)"
check_pkg "hexchat" "HexChat (IRC client)"
check_pkg "thunderbird" "Thunderbird (mail client)"
check_pkg "evolution" "Evolution (mail client)"
check_pkg "gnome-maps" "GNOME Maps"
check_pkg "gnome-weather" "GNOME Weather"
check_pkg "gnome-contacts" "GNOME Contacts"
check_pkg "gnome-clocks" "GNOME Clocks"

if [[ ${#candidate_ids[@]} -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_ids[@]}"; 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

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

  if [[ ${#indices[@]} -eq 0 ]]; then
    echo "   Nothing selected. Skipping."
  else
    for idx in "${indices[@]}"; do
      id="${candidate_ids[$idx]}"
      if [[ "$id" == "__libreoffice__" ]]; then
        echo "   Removing LibreOffice packages:"
        echo "$lo_installed" | sed 's/^/     /'
        sudo apt-get -y purge $lo_installed || true
      else
        echo "   Removing $id"
        sudo apt-get -y purge "$id" || true
      fi
    done
  fi
fi

# 2. autoremove
echo
echo "-> Running apt autoremove --purge..."
sudo apt-get -y autoremove --purge

# 3. Clear apt cache
echo
echo "-> Clearing apt package cache..."
sudo apt-get clean

echo
echo "== Done. =="

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