#!/bin/bash
# Debinstall, a simple Debian Package Installer.
# Concept and Testing by Glen MacArthur, coded by Google Gemini 3.

# Collect all passed arguments into an array
DEB_FILES=("$@")
COUNT=${#DEB_FILES[@]}

# 1. Prepare the Summary Text
if [ "$COUNT" -eq 1 ]; then
    PKG_NAME=$(dpkg-deb -f "${DEB_FILES[0]}" Package)
    SUMMARY="<b>Package:</b> $PKG_NAME\n<b>File:</b> $(basename "${DEB_FILES[0]}")"
else
    SUMMARY="<b>Total Packages to Install:</b> $COUNT\n\n"
    # List the first 5 files as a preview
    for i in "${!DEB_FILES[@]}"; do
        if [ "$i" -lt 5 ]; then
            SUMMARY+="$(basename "${DEB_FILES[$i]}")\n"
        fi
    done
    [ "$COUNT" -gt 5 ] && SUMMARY+="<i>...and $((COUNT - 5)) more.</i>"
fi

# 2. The Warning Window
yad --warning --center --width=600 --title="Debian Package Installer" \
    --window-icon="debinstall" \
    --image="dialog-warning" \
    --text="<b>Security Warning!</b>\n\nYou are about to install <b>$COUNT</b> package(s) from outside the official repositories.\n\n$SUMMARY\n\n<b>Note:</b> Clicking 'Install' will open a terminal where you will be asked for your <b>sudo password</b> to finalize the installation." \
    --button="Abort:1" --button="Install:0"

if [ $? -ne 0 ]; then
    exit 0
fi

# 3. Launch Terminology
# We pass the entire array to apt install
terminology --title="Debian Package Installer" -e "bash -c 'echo -e \"\e[1;32mInstalling $COUNT package(s)...\e[0m\"; sudo apt install \"${DEB_FILES[@]}\"; echo -e \"\n\e[1;36mProcess finished. Press any key to close...\e[0m\"; read -n 1'"