#!/usr/bin/env bash
#
# DebloatMe - Fedora
# https://debloatme.com/fedora
#
# What this script does:
#   1. Runs dnf autoremove to clear packages nothing else depends on
#   2. Trims the systemd journal to a bounded size
#   3. Clears the dnf package cache
#   4. Lets you pick, from a checklist, which optional bundled apps to
#      remove (LibreOffice, a broader list of default GNOME apps) -
#      nothing is removed unless you select it
#
# Reads confirmation prompts from /dev/tty so this still works when the
# script is piped straight from curl (curl ... | bash).
#
# Written against Fedora Workstation (GNOME). Review this script before
# running it. Removing packages can have side effects depending on what
# you've built on top of them.

set -euo pipefail

echo "== DebloatMe: Fedora =="
echo "This will modify installed packages, journal logs and the dnf cache."
read -r -p "Continue? [y/N] " confirm </dev/tty
if [[ ! "$confirm" =~ ^[Yy]$ ]]; then
  echo "Aborted. No changes made."
  exit 0
fi

# 1. dnf autoremove
echo
echo "-> Running dnf autoremove..."
sudo dnf -y autoremove

# 2. Trim systemd journal
echo
echo "-> Trimming systemd journal to 200M..."
sudo journalctl --vacuum-size=200M

# 3. Clear dnf cache
echo
echo "-> Clearing dnf package cache..."
sudo dnf clean all

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

candidate_ids=()
candidate_labels=()

lo_installed=$(rpm -qa 'libreoffice*' 2>/dev/null || true)
if [[ -n "$lo_installed" ]]; then
  candidate_ids+=("__libreoffice__")
  candidate_labels+=("LibreOffice (full suite)")
fi

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

check_pkg "gnome-tour" "GNOME Tour"
check_pkg "gnome-maps" "GNOME Maps"
check_pkg "gnome-weather" "GNOME Weather"
check_pkg "gnome-contacts" "GNOME Contacts"
check_pkg "gnome-connections" "GNOME Connections"
check_pkg "gnome-clocks" "GNOME Clocks"
check_pkg "gnome-characters" "GNOME Character Map"
check_pkg "gnome-logs" "GNOME Logs"
check_pkg "rhythmbox" "Rhythmbox"
check_pkg "gnome-boxes" "GNOME Boxes"
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 "thunderbird" "Thunderbird (mail client)"

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 dnf -y remove $lo_installed || true
      else
        echo "   Removing $id"
        sudo dnf -y remove "$id" || true
      fi
    done
  fi
fi

echo
echo "== Done. =="

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