#!/bin/bash

# omarchy:summary=Show a fuzzy-finder TUI for picking new Arch and OPR packages to install.
# omarchy:requires-sudo=true

# Fedora fork: lists the packages available from the configured dnf
# repositories, which include Fedora, RPM Fusion and Omadora's COPRs.
fzf_args=(
  --multi
  --preview 'dnf info --quiet {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:green,marker:green'
)

pkg_names=$(dnf repoquery --quiet --available --queryformat '%{name}\n' 2>/dev/null | sort -u | fzf "${fzf_args[@]}")

if [[ -n $pkg_names ]]; then
  source omarchy-sudo-keepalive

  # Convert newline-separated selections to space-separated for dnf
  echo "$pkg_names" | tr '\n' ' ' | xargs sudo dnf install -y
  code=$?
  # Upstream's AUR picker, which is this picker on Fedora, refreshes the
  # locate database so new files are findable at once.
  sudo updatedb --prune-bind-mounts=no --add-prunepaths=/.snapshots
  omarchy-show-done $code
fi
