#!/bin/bash
# AXEM-SX third-party application installer (whiptail menu)
set -e
if ! command -v whiptail >/dev/null 2>&1; then
    echo "whiptail not found. Install 'newt' and try again." >&2
    exit 1
fi
if [ "$(id -u)" -eq 0 ]; then
    echo "Run this WITHOUT sudo. Flatpak installs go to your user." >&2
    echo "Anti-Gravity (the only system-wide RPM) will prompt for sudo." >&2
    exit 1
fi

# Make sure Flathub is configured (idempotent)
flatpak remote-add --if-not-exists --user flathub \
    https://dl.flathub.org/repo/flathub.flatpakrepo

CHOICES=$(whiptail --title "AXEM-SX third-party installer" \
    --checklist "Pick the apps to install (SPACE to toggle, ENTER to confirm):" \
    18 70 8 \
    "spotify"      "Spotify (Flathub)"           OFF \
    "discord"      "Discord (Flathub)"           OFF \
    "vscode"       "Visual Studio Code (Flathub)" OFF \
    "antigravity"  "Anti-Gravity (AXEM-SX OBS)"  OFF \
    3>&1 1>&2 2>&3) || exit 0

for app in $CHOICES; do
    app=${app//\"/}
    case "$app" in
        spotify)     flatpak install --user -y flathub com.spotify.Client ;;
        discord)     flatpak install --user -y flathub com.discordapp.Discord ;;
        vscode)      flatpak install --user -y flathub com.visualstudio.code ;;
        antigravity) sudo zypper --non-interactive install anti-gravity ;;
    esac
done

echo "==> Selected installs complete."
