#!/bin/bash
########################################################
#                    PkgBuddy                          # 
#   Simple local rpm package installer (dnf edition)   #
#                                                      #
#   Copyright (c) 2025 lxgator @gmail.com              #
#   GNU GENERAL PUBLIC LICENSE Version 3               #
########################################################

ICON='/usr/share/icons/hicolor/48x48/apps/pkgbuddy.png'
RICON='/usr/share/pkgbuddy/error.svg'
BICON='/usr/share/icons/Adwaita/16x16/apps/system-software-install.png'
OK='/usr/share/pkgbuddy/ok.svg'
CANCEL='/usr/share/pkgbuddy/cancel.svg'
REMOVE='/usr/share/pkgbuddy/error.png'
IPNG='/usr/share/pkgbuddy/info.png'
USERS=$(who | awk '{print $1}' | head -n1)

# Create a local desktop file
if ! test -d /home/$USERS/.local/share/applications; then
mkdir -p /home/$USERS/.local/share/applications
fi
if ! test -f /home/$USERS/.local/share/applications/pkgbuddy.desktop; then
cp -rf /usr/share/pkgbuddy/pkgbuddy.desktop /home/$USERS/.local/share/applications
chown -R $USERS:$USERS /home/$USERS/.local/share/applications/pkgbuddy.desktop
fi
if ! test -d /root/.local/share/applications; then
mkdir -p /root/.local/share/applications
fi
if ! test -f /root/.local/share/applications/pkgbuddy.desktop; then
cp -rf /usr/share/pkgbuddy/pkgbuddy.desktop /root/.local/share/applications
fi
if ! test -d /root/.local/share/applications; then
mkdir -p /root/.local/share/applications
fi
FILENAME=$@
RM=$(basename "$FILENAME")
# Remove everything after the first '-' (version/release/arch)
PKGNAME=$(echo "$RM" | sed 's/-[^-]*-[^-]*\.[^.]*\.rpm$//')

# Reject source packages
if [[ ${FILENAME: -8} == ".src.rpm" ]]; then
    yad --error --width=475 --height=100 --fixed --image=$RICON \
        --window-icon=$RICON --center --on-top --title="Error" \
        --text="\nThis package is a src package! Installation is not possible" \
        --button="Ok"!$OK:0
    exit
fi

# Reject non-rpm
if [[ ${FILENAME: -4} != ".rpm" ]]; then
    exit
fi

rm -f /tmp/pkgbuddy 2>/dev/null

# Ask user options
ENTRY=$(yad --width=475 --height=100 --center --on-top --fixed --image=$ICON --window-icon=$ICON \
    --form --title="PkgBuddy" \
    --text="\nPkgBuddy wants to install...\n\n<span color=\"#e07b39\"><b>$RM</b></span>\n" \
    --button="Cancel"!$CANCEL:1 \
    --button="Install"!$OK:0)

ret=$?
[[ $ret -eq 1 ]] && exit

# Network check
wget -q --tries=3 --timeout=10 --delete-after "https://www.google.com/" || {
    yad --error --width=400 --height=100 --fixed --image=$RICON \
        --window-icon=$RICON --center --on-top --title="Error" \
        --text="\nCheck Network, PkgBuddy connection failed" \
        --button="Cancel"!$CANCEL:0
    exit
}

# Check if dnf is running
if pgrep -x dnf >/dev/null ; then
    yad --error --width=400 --height=100 --fixed --image=$RICON \
        --window-icon=$RICON --center --on-top --title="Error" \
        --text="\nDNF Package Manager is running!\nClose it to continue." \
        --button="Ok"!$OK:0
    exit
fi

# Function to show a minimal pulsating progress bar while a command runs
run_with_progress() {
    local CMD="$1"
    {
        SECONDS_PASSED=0
        while true; do
            echo "#Please wait..."
            sleep 0.1
            SECONDS_PASSED=$((SECONDS_PASSED+1))
            # Keep progress bar at least 0.5s (5 * 0.1s)
            if [ $SECONDS_PASSED -gt 5 ]; then
                break
            fi
        done
    } | yad --progress --pulsate --title="PkgBuddy" \
            --text="\n<span color=\"#e07b39\"><b>$RM</b></span>" \
            --width=475 --height=100 --center --image=$ICON \
            --button=Info!$IPNG:7 \
            --window-icon=$ICON --button="Ok"!$OK --auto-close &
    local PROG_PID=$!
    eval "$CMD"
    wait $PROG_PID
    pkill -f "yad --progress --pulsate"
}

# Install the package
run_with_progress "dnf -y install \"$FILENAME\" --allowerasing > /tmp/pkgbuddy 2>&1"
chmod 0777 /tmp/pkgbuddy

# Dependency error handling
if grep -q "requires" /tmp/pkgbuddy; then
    yad --text="\n<span color=\"#e07b39\"><b>$RM</b></span>\n\nfailed with missing dependencies\nCheck info for probable causes" \
        --title="Install Failure" --on-top --width=425 --height=125 \
        --window-icon=$RICON --image=$RICON \
        --button=Info!$IPNG:2 \
        --button="Cancel"!$CANCEL:1
    if [ $? = "2" ]; then 
        INFO=$(cat /tmp/pkgbuddy)
        yad --window-icon=$ICON --center --on-top --height=100 --width=475 \
           --title="PkgBuddy" --text="$INFO" \
           --button="Close"!$CANCEL:1
    fi
    rm -f /tmp/pkgbuddy 2>/dev/null
    exit 0
fi

# End results
CHOICE=$(yad --window-icon=$ICON --center --on-top --image=$ICON --height=125 --width=475 --fixed \
    --text="\nPackage is <b>installed</b> and ready to use.\n\n<span color=\"#e07b39\"><b>$RM</b></span>\n" \
    --title="PkgBuddy" \
    --button="Info"!$IPNG:3 \
    --button="Remove"!$REMOVE:2 \
    --button="OK"!$OK:1)

case $? in
    1)  # OK
        rm -f /tmp/pkgbuddy 2>/dev/null
        exit
        ;;
    3)  # Info
        INFO=$(cat /tmp/pkgbuddy)
        yad --window-icon="$ICON" --fixed --on-top --height=200 --width=475 \
           --title="PkgBuddy" --text="$INFO" --center --scroll \
           --button="Close"!$CANCEL:1
        rm -f /tmp/pkgbuddy 2>/dev/null
        ;;
    2)  # Remove
        run_with_progress "dnf -y remove \"$PKGNAME\" > /tmp/pkgbuddy 2>&1"
        INFO=$(cat /tmp/pkgbuddy)
        yad --window-icon=$ICON --fixed --on-top --height=200 --width=475 \
           --title="PkgBuddy" --text="$INFO" --center --scroll \
           --button="Close"!$CANCEL:1
        rm -f /tmp/pkgbuddy 2>/dev/null
        ;;
esac

