#!/usr/bin/env bash

# omarchy:summary=Show a fuzzy-finder TUI for picking packages installed on the system to be removed.
# omarchy:requires-sudo=true

# Based: apt-mark showmanual is the explicitly installed set (yay -Qqe);
# apt-get purge --autoremove is pacman -Rns.
fzf_args=(
  --multi
  --preview 'apt-cache show {1}'
  --preview-label='alt-p: toggle description, alt-j/k: scroll, tab: multi-select'
  --preview-label-pos='bottom'
  --preview-window 'down:65%:wrap'
  --bind 'alt-p:toggle-preview'
  --bind 'alt-d:preview-half-page-down,alt-u:preview-half-page-up'
  --bind 'alt-k:preview-up,alt-j:preview-down'
  --color 'pointer:red,marker:red'
)

pkg_names=$(apt-mark showmanual | sort | fzf "${fzf_args[@]}")

if [[ -n $pkg_names ]]; then
  # Convert newline-separated selections to space-separated for apt
  echo "$pkg_names" | tr '\n' ' ' | xargs sudo apt-get -y purge --autoremove
  omarchy-show-done
fi
