#!/bin/bash -i
#
# Purpose : HamPack uninstaller

print_logo() {
  cat << "EOF"



 ██╗  ██╗ █████╗ ███╗   ███╗██████╗  █████╗  ██████╗██╗  ██╗
 ██║  ██║██╔══██╗████╗ ████║██╔══██╗██╔══██╗██╔════╝██║ ██╔╝
 ███████║███████║██╔████╔██║██████╔╝███████║██║     █████╔╝
 ██╔══██║██╔══██║██║╚██╔╝██║██╔═══╝ ██╔══██║██║     ██╔═██╗
 ██║  ██║██║  ██║██║ ╚═╝ ██║██║     ██║  ██║╚██████╗██║  ██╗
 ╚═╝  ╚═╝╚═╝  ╚═╝╚═╝     ╚═╝╚═╝     ╚═╝  ╚═╝ ╚═════╝╚═╝  ╚═╝




EOF
}

# --- Main ---

clear
print_logo

echo "This will remove all packages and files installed by HamPack."
echo ""
read -rp "Are you sure you want to continue? [y/N] " confirm
[[ "$confirm" =~ ^[yY] ]] || { echo "Aborted."; exit 0; }
echo ""

# Stop and remove graywolf systemd service
echo "Removing graywolf systemd service..."
if systemctl is-active --quiet graywolf 2>/dev/null; then
  sudo systemctl disable --now graywolf
fi
if [ -f /etc/systemd/system/graywolf.service ]; then
  sudo rm -f /etc/systemd/system/graywolf.service
  sudo systemctl daemon-reload
fi

# Remove compiled binaries
echo "Removing compiled binaries..."
rm -f ~/.local/bin/direwolf
rm -f ~/.local/bin/direwolf-*
rm -f ~/.local/bin/QtTermTCP
rm -f ~/.local/bin/kiwix-desktop
rm -f ~/.local/bin/kiwix-lib
rm -f ~/.local/bin/kiwix-serve
rm -f ~/.local/bin/kiwix-search
rm -f ~/.local/bin/kiwix-manage
rm -rf ~/.local/bin/yaac
rm -f ~/.local/bin/POTACAT.AppImage
rm -f ~/.local/bin/JS8Call
rm -f ~/.local/bin/dump1090
rm -f ~/.local/bin/graywolf
rm -f ~/.local/bin/graywolf-modem
rm -rf ~/.local/share/graywolf

# Remove HamPack scripts and binaries
echo "Removing HamPack scripts and binaries..."
rm -f ~/.local/bin/hampackrefresh
rm -f ~/.local/bin/hampackupdate
rm -f ~/.local/bin/hampackcheck
rm -f ~/.local/bin/hampack
rm -f ~/.local/bin/dx.py

# Uninstall not1mm
echo "Uninstalling not1mm..."
if command -v uv &>/dev/null; then
  uv tool uninstall not1mm 2>/dev/null || true
fi

# Remove Flatpak applications
echo "Removing Flatpak applications..."
FLATPAKS=(
  "io.sourceforge.wsjt.wsjtx"
  "io.github.foldynl.QLog"
)
for pak in "${FLATPAKS[@]}"; do
  if flatpak list 2>/dev/null | grep -qi "$pak"; then
    flatpak uninstall --noninteractive "$pak"
  fi
done

# Remove system packages
echo "Removing system packages..."
UTILITIES=(
  alsa-utils
  aria2
  at-spi2-core
  base-devel
  boost
  boost-libs
  cmake
  docopt
  fldigi
  flrig
  gpds
  gpsbabel
  gtk4
  hamlib
  jdk-openjdk
  libappindicator-gtk3
  libkiwix
  libtorrent-rasterbar
  libnotify
  libsecret
  libxcrypt-compat
  libxtst
  libzim
  meson
  ninja
  nss
  pat-bin
  python-yattag
  qgis
  qt6-base
  qt6-tools
  qt6-webengine
  splat
  util-linux-libs
  voacapl
  wine
  winetricks
  xdg-utils
  zim-tools
  zimwriterfs
)

APPLICATIONS=(
  chattervox-bin
  chirp-next
  gridtracker2
)

PACKAGES_TO_REMOVE=()
for pkg in "${UTILITIES[@]}" "${APPLICATIONS[@]}"; do
  if pacman -Qi "$pkg" &>/dev/null; then
    PACKAGES_TO_REMOVE+=("$pkg")
  fi
done

if [ ${#PACKAGES_TO_REMOVE[@]} -gt 0 ]; then
  echo "Removing packages: ${PACKAGES_TO_REMOVE[*]}"
  sudo pacman -Rns --noconfirm "${PACKAGES_TO_REMOVE[@]}" 2>/dev/null || \
    echo "Warning: some packages could not be removed (may be required by other software)."
else
  echo "No system packages to remove."
fi

# Remove Wine applications
echo "Removing Wine applications..."
rm -rf "$HOME/.wine/drive_c/VARA"
rm -rf "$HOME/.wine/drive_c/VARA FM"
rm -rf "$HOME/.wine/drive_c/VARA Terminal"
rm -rf "$HOME/.wine/drive_c/RMS Express"

# Remove desktop files
echo "Removing desktop files..."
rm -f ~/.local/share/applications/hampack.desktop
rm -f ~/.local/share/applications/js8call.desktop
rm -f ~/.local/share/applications/yaac.desktop
rm -f ~/.local/share/applications/potacat.desktop
rm -f ~/.local/share/applications/qttermtcp.desktop

update-desktop-database "$HOME/.local/share/applications/" 2>/dev/null || true

# Remove HamPack state and data
echo "Removing HamPack state and data..."
rm -rf ~/.local/state/HamPack
rm -rf ~/.local/share/HamPack

echo ""
echo "HamPack has been removed."
