#!/bin/sh

set -e

ask_install () {
    PACKAGE="$1"
    APT_NAME="$2"

    printf "Install %s? [y/N]: " "$PACKAGE"
    read answer
    case "$answer" in
        y|Y)
            echo "Installing $PACKAGE..."
            sudo apt install -y "$APT_NAME"
            ;;
        *)
            echo "Skipping $PACKAGE"
            ;;
    esac
    echo
}

install_brave () {
    PACKAGE="Brave Origin"
    printf "Install %s? [y/N]: " "$PACKAGE"
    read answer
    case "$answer" in
        y|Y)
            echo "Installing $PACKAGE..."
            curl -fsS https://dl.brave.com/install.sh | FLAVOR=origin sh
            ;;
        *)
            echo "Skipping $PACKAGE"
            ;;
    esac
    echo
}

install_discord () {
    PACKAGE="Discord"
    DISCORD_URL="https://discord.com/api/download?platform=linux&format=deb"
    DEB_FILE="discord_latest_amd64.deb" 
    printf "Install %s? [y/N]: " "$PACKAGE"
    read answer
    case "$answer" in
        y|Y)
            echo "Installing $PACKAGE..."
            wget -O "$DEB_FILE" "$DISCORD_URL"
            sudo dpkg -i "$DEB_FILE" || sudo apt -f install -y
            rm -f "$DEB_FILE"
            ;;
        *)
            echo "Skipping $PACKAGE"
            ;;
    esac
    echo
}

install_angry_ip_scanner () {
    PACKAGE="Angry IP Scanner"
    REPO="angryip/ipscan"
    ARCH="amd64"
    printf "Install %s? [y/N]: " "$PACKAGE"
    read answer
    case "$answer" in
        y|Y)
            echo "Installing $PACKAGE..."
            # Get latest release tag from GitHub API
            LATEST_TAG=$(curl -s https://api.github.com/repos/$REPO/releases/latest \
                | grep '"tag_name"' \
                | cut -d '"' -f4)

            if [[ -z "$LATEST_TAG" ]]; then
                echo "❌ Failed to determine latest version"
                exit 1
            fi
            # Extract version number (e.g. 3.6.2)
            VERSION="${LATEST_TAG#v}"
            DEB_FILE="ipscan_${VERSION}_${ARCH}.deb"
            DOWNLOAD_URL="https://github.com/$REPO/releases/download/$LATEST_TAG/$DEB_FILE"
            wget -q --show-progress "$DOWNLOAD_URL"
            sudo dpkg -i "$DEB_FILE" || sudo apt -f install -y

            # Cleanup
            rm -f "$DEB_FILE"
            ;;
        *)
            echo "Skipping $PACKAGE"
            ;;
    esac
    echo
}

install_onlyoffice () {
    PACKAGE="OnlyOffice"    
    printf "Install %s? [y/N]: " "$PACKAGE"
    read answer
    case "$answer" in
        y|Y)
            echo "Installing $PACKAGE..."
            #Create GPG directory
            mkdir -p -m 700 ~/.gnupg

            #Import the ONLYOFFICE GPG key
            gpg --no-default-keyring \
                --keyring gnupg-ring:/tmp/onlyoffice.gpg \
                --keyserver hkp://keyserver.ubuntu.com:80 \
                --recv-keys CB2DE8E5

            #Fix permissions and move the keyring
            chmod 644 /tmp/onlyoffice.gpg
            sudo chown root:root /tmp/onlyoffice.gpg
            sudo mv /tmp/onlyoffice.gpg /usr/share/keyrings/onlyoffice.gpg

            #Add the ONLYOFFICE repository
            echo "deb [signed-by=/usr/share/keyrings/onlyoffice.gpg] https://download.onlyoffice.com/repo/debian squeeze main" \
                | sudo tee /etc/apt/sources.list.d/onlyoffice.list > /dev/null

            #Update package lists
            sudo apt-get update

            #Install ONLYOFFICE Desktop Editors
            sudo apt-get install -y onlyoffice-desktopeditors

            #Remove LibreOffice
            sudo apt purge libreoffice*
            sudo apt autoremove --purge
            rm -rf ~/.config/libreoffice
            rm -rf ~/.cache/libreoffice
            rm -rf ~/.local/share/libreoffice
            ;;
        *)
            echo "Skipping $PACKAGE"
            ;;
    esac
    echo
}

install_sublime () {
    PACKAGE="Sublime Text Editor"    
    printf "Install %s? [y/N]: " "$PACKAGE"
    read answer
    case "$answer" in
        y|Y)
            echo "Installing $PACKAGE..."
            sudo wget -O- https://download.sublimetext.com/sublimehq-pub.gpg | gpg --dearmor | sudo tee /usr/share/keyrings/sublimehq.gpg
            echo 'deb [signed-by=/usr/share/keyrings/sublimehq.gpg] https://download.sublimetext.com/ apt/stable/' | sudo tee /etc/apt/sources.list.d/sublime-text.list
            sudo apt update
            sudo apt install sublime-text
            rm -f ./wget-log
            ;;
        *)
            echo "Skipping $PACKAGE"
            ;;
    esac
    echo
}

install_joplin () {
    PACKAGE="Joplin"    
    printf "Install %s? [y/N]: " "$PACKAGE"
    read answer
    case "$answer" in
        y|Y)
            echo "Installing $PACKAGE..."
            wget -O - https://raw.githubusercontent.com/laurent22/joplin/dev/Joplin_install_and_update.sh | bash
            ;;
        *)
            echo "Skipping $PACKAGE"
            ;;
    esac
    echo
}

install_flatseal () {
    PACKAGE="Flatseal"
    printf "Install %s? [y/N]: " "$PACKAGE"
    read answer
    case "$answer" in
        y|Y)
            echo "Installing $PACKAGE..."
            if ! command -v flatpak >/dev/null 2>&1; then
                echo "Flatpak not found, installing..."
                sudo apt install -y flatpak
            fi
            if ! flatpak remote-list | grep -q flathub; then
                flatpak remote-add --if-not-exists flathub https://flathub.org/repo/flathub.flatpakrepo
            fi
            flatpak install -y flathub com.github.tchx84.Flatseal
            ;;
        *)
            echo "Skipping $PACKAGE"
            ;;
    esac
    echo
}

install_stremio () {
    PACKAGE="Stremio"
    printf "Install %s? [y/N]: " "$PACKAGE"
    read answer
    case "$answer" in
        y|Y)
            echo "Installing $PACKAGE..."
            if ! command -v flatpak >/dev/null 2>&1; then
                echo "Flatpak not found, installing..."
                sudo apt install -y flatpak
            fi
            if ! flatpak remote-list | grep -q flathub; then
                flatpak remote-add --if-not-exists flathub https://flathub.org/repo/flathub.flatpakrepo
            fi
            flatpak install -y flathub com.stremio.Stremio
            ;;
        *)
            echo "Skipping $PACKAGE"
            ;;
    esac
    echo
}

install_balena_etcher () {
    PACKAGE="balenaEtcher"
    REPO="balena-io/etcher"
    printf "Install %s? [y/N]: " "$PACKAGE"
    read answer
    case "$answer" in
        y|Y)
            echo "Installing $PACKAGE..."
            DEB_URL=$(curl -s https://api.github.com/repos/$REPO/releases/latest \
                | grep "browser_download_url.*amd64.deb" \
                | cut -d '"' -f4)
 
            if [ -z "$DEB_URL" ]; then
                echo "❌ Failed to determine latest release"
                exit 1
            fi
            DEB_FILE=$(basename "$DEB_URL")
            wget -q --show-progress "$DEB_URL"
            sudo apt install -y "./$DEB_FILE"
 
            # Cleanup
            rm -f "$DEB_FILE"
            ;;
        *)
            echo "Skipping $PACKAGE"
            ;;
    esac
    echo
}

echo "Updating package list..."
sudo apt update
echo

# APT packages
ask_install "PCManFM" "pcmanfm"
ask_install "Wireshark" "wireshark"
ask_install "Flameshot" "flameshot"
ask_install "Remmina" "remmina"
ask_install "PuTTY" "putty"
ask_install "WireGuard" "wireguard"
ask_install "Steam" "steam"
ask_install "mpv" "mpv"
ask_install "Hydrapaper" "hydrapaper"
#wine
#WineZGUI
#TLP
#TLP-UI

# Not in standard Debian repos
install_brave
install_discord
install_angry_ip_scanner
install_onlyoffice
install_sublime
install_joplin
install_flatseal
install_stremio
install_balena_etcher
#"Visual Studio Code" "Requires Microsoft APT repo"

echo "Finished."