#!/usr/bin/env bash
#
# DebloatMe - openSUSE
# https://debloatme.com/suse
#
# What this script does:
#   1. Configures Zypper to ask before installing packages with unmet
#      vendor/dependency expectations (stricter default behaviour)
#   2. Lets you pick, from a checklist, which optional patterns and
#      preinstalled apps to remove (LibreOffice, Office, Games, and a
#      broader list of GNOME/KDE extras) - nothing is removed unless
#      you select it
#   3. Clears the Zypper 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. Pattern removal can remove more
# than a single application if other packages depend on that pattern.

set -euo pipefail

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

# 1. Stricter dependency installation behaviour
echo
echo "-> Setting zypper to a stricter dependency-resolution mode..."
sudo sed -i 's/^solver.onlyRequires.*/solver.onlyRequires = true/' /etc/zypp/zypp.conf 2>/dev/null || \
  echo "solver.onlyRequires = true" | sudo tee -a /etc/zypp/zypp.conf >/dev/null

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

# Each candidate is "type:name:label" - type is 'pattern' or 'pkg'
candidates=()
labels=()
types=()

check_pattern() {
  local name="$1" label="$2"
  if zypper se -t pattern --installed-only "$name" 2>/dev/null | grep -q "^i"; then
    types+=("pattern"); candidates+=("$name"); labels+=("$label")
  fi
}

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

check_pkg "libreoffice" "LibreOffice (full suite)"
check_pattern "office" "Office pattern"
check_pattern "games" "Games pattern"
check_pattern "multimedia" "Multimedia pattern"
check_pkg "gnome-maps" "GNOME Maps"
check_pkg "gnome-weather" "GNOME Weather"
check_pkg "gnome-contacts" "GNOME Contacts"
check_pkg "gnome-clocks" "GNOME Clocks"
check_pkg "gnome-music" "GNOME Music"
check_pkg "gnome-characters" "GNOME Character Map"
check_pkg "gnome-logs" "GNOME Logs"
check_pkg "gnome-boxes" "GNOME Boxes"
check_pkg "totem" "GNOME Videos (Totem)"
check_pkg "rhythmbox" "Rhythmbox"
check_pkg "cheese" "Cheese (webcam booth)"
check_pkg "shotwell" "Shotwell (photo manager)"
check_pkg "simple-scan" "Simple Scan"
check_pkg "akonadi-server" "Akonadi (KDE PIM backend)"
check_pkg "kmail" "KMail"
check_pkg "korganizer" "KOrganizer"
check_pkg "kontact" "Kontact"
check_pkg "akregator" "Akregator (RSS reader)"
check_pkg "elisa-player" "Elisa (music player)"
check_pkg "dragonplayer" "Dragon Player"
check_pkg "kolourpaint" "KolourPaint"
check_pkg "transmission-gtk" "Transmission (torrent client)"
check_pkg "hexchat" "HexChat (IRC client)"
check_pkg "MozillaThunderbird" "Thunderbird (mail client)"
check_pkg "evolution" "Evolution (mail client)"

if [[ ${#candidates[@]} -eq 0 ]]; then
  echo "   None of the optional patterns/apps on the list are installed."
else
  echo
  echo "Optional patterns/apps found on this system:"
  for i in "${!candidates[@]}"; do
    printf "  [%d] %s\n" "$((i + 1))" "${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 "${!candidates[@]}"; do indices+=("$i"); done
  elif [[ -n "$selection" ]]; then
    for n in $selection; do
      idx=$((n - 1))
      if [[ $idx -ge 0 && $idx -lt ${#candidates[@]} ]]; then
        indices+=("$idx")
      fi
    done
  fi

  if [[ ${#indices[@]} -eq 0 ]]; then
    echo "   Nothing selected. Skipping."
  else
    for idx in "${indices[@]}"; do
      name="${candidates[$idx]}"
      if [[ "${types[$idx]}" == "pattern" ]]; then
        echo "   Removing pattern: $name"
        sudo zypper --non-interactive remove -t pattern "$name" || true
      else
        echo "   Removing $name"
        sudo zypper --non-interactive remove "$name" || true
      fi
    done
  fi
fi

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

echo
echo "== Done. =="

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