#!/bin/bash

# deps: bash, coreutils, apt, dialog, grep, sudo, nala
# Last update 2025/05/20 by pavroo
# it uses Nala

# nala opts: install, remove, autoremove, autopurge, purge, update, upgrade, full-upgrade, autoremove, clean, show, search, list, fetch, --help

DIALOG="`which dialog`"
YESNO="--yesno "
HEIGHT="20"
WIDTH="80"
MENUHEIGHT="12"
TITLE="--title "
ENTRY="--inputbox "
MENU="--menu"
TEXT=""
TITLETEXT="Sparky Package Tool Nala (SPTN)"

CHECKNALA=`which nala`
if [ "$CHECKNALA" = "" ]; then
	clear
	echo ""
	echo "  No Nala found."
	echo "  Install nala package and try again."
	echo "  Press <ENTER> to exit..." && read
	echo ""
	exit 0
fi

#$TEXT"Choose your action:"
mainmenu () {

$DIALOG $TITLE"$TITLETEXT" $MENU $TEXT"" $HEIGHT $WIDTH $MENUHEIGHT \
Back "Back to main menu" \
Update "Update package list only" \
SafeUpgrade "Safe package upgrade" \
FullUpgrade "Full package upgrade" \
Check "Check is a package installed" \
Info "Print info about a package" \
Search "Search of available packages" \
Install "Install a package" \
Remove "Uninstall a package" \
Kernel "Install or remove Linux kernels" \
Clean "Clean the APT cache" \
Exit "Exit" 2>/tmp/choice.$$

#Download "Download a deb package only (via APT)" \
#Simulate "Simulate a package installation only (via APT)" \
#ReInstall "Reinstall a package (via APT)" \
#Fix "Fix broken packages (via APT)" \

# TODO: LocalInstall

NEWACTION=`cat /tmp/choice.$$`

if [ "$?" = "0" ]; then
	NEWACTION=`cat /tmp/choice.$$`
else
	clear
	exit 0
fi
sudo rm /tmp/choice.$$

if [ "$NEWACTION" = "Back" ]; then
	backmenu
elif [ "$NEWACTION" = "Update" ]; then
	updatemenu
elif [ "$NEWACTION" = "SafeUpgrade" ]; then
	safeupgrademenu
elif [ "$NEWACTION" = "FullUpgrade" ]; then
	fullupgrademenu
elif [ "$NEWACTION" = "Check" ]; then
	checkmenu
elif [ "$NEWACTION" = "Info" ]; then
	infomenu
elif [ "$NEWACTION" = "Search" ]; then
	searchmenu
elif [ "$NEWACTION" = "Download" ]; then
	downloadmenu
elif [ "$NEWACTION" = "Simulate" ]; then
	simulatemenu
elif [ "$NEWACTION" = "Install" ]; then
	installmenu
elif [ "$NEWACTION" = "ReInstall" ]; then
	reinstallmenu
elif [ "$NEWACTION" = "Remove" ]; then
	removemenu
elif [ "$NEWACTION" = "Kernel" ]; then
	kernelmenu
elif [ "$NEWACTION" = "Fix" ]; then
	fixmenu
elif [ "$NEWACTION" = "Clean" ]; then
	cleanmenu
else
	clear
	exit 0	
fi

}

# back to main
backmenu () {
clear
spt
}

# update
updatemenu () {
clear
sudo nala update
echo 'Done, package list updated, press <ENTER> to Exit...' && read
mainmenu
}

# safe upgrade
safeupgrademenu () {
clear
sudo nala upgrade
echo 'Done, packages safe upgraded, press <ENTER> to Exit...' && read
mainmenu
}

# full upgrade
fullupgrademenu () {
clear
sudo nala full-upgrade
echo 'Done, all packages full upgraded, press <ENTER> to Exit...' && read
mainmenu
}

# check
checkmenu () {
clear
$DIALOG $TITLE"$TITLETEXT" $ENTRY $TEXT"\nType in a key name of a packege to check is already installed" $HEIGHT $WIDTH 2>/tmp/choice.$$
if [ "$?" = "0" ]; then
	PACKAGE=`cat /tmp/choice.$$`
else
	clear
	mainmenu
fi
sudo rm /tmp/choice.$$

clear
dpkg-query -l | grep $PACKAGE
echo 'Done, the installed package list printed, press <ENTER> to Exit...' && read
mainmenu
}

# info
infomenu () {
clear
$DIALOG $TITLE"$TITLETEXT" $ENTRY $TEXT"\nType in a name of a package to print info about it" $HEIGHT $WIDTH 2>/tmp/choice.$$
if [ "$?" = "0" ]; then
	PACKAGE=`cat /tmp/choice.$$`
else
	clear
	mainmenu
fi
sudo rm /tmp/choice.$$
clear

nala show $PACKAGE
echo 'Done, press <ENTER> to Exit...' && read
mainmenu
}

#search
searchmenu () {
clear
$DIALOG $TITLE"$TITLETEXT" $ENTRY $TEXT"\nType in a key name of a package to search it in the repository" $HEIGHT $WIDTH 2>/tmp/choice.$$
if [ "$?" = "0" ]; then
	PACKAGE=`cat /tmp/choice.$$`
else
	clear
	mainmenu
fi
sudo rm /tmp/choice.$$
clear

nala search $PACKAGE
echo 'Done, the available package list printed, press <ENTER> to Exit...' && read
mainmenu
}

## download
#downloadmenu () {
#clear
#$DIALOG $TITLE"$TITLETEXT" $ENTRY $TEXT"\nType in a package name to be downloaded from the repository to the user's home dir" $HEIGHT $WIDTH 2>/tmp/choice.$$
#if [ "$?" = "0" ]; then
#	PACKAGE=`cat /tmp/choice.$$`
#else
#	clear
#	mainmenu
#fi
#rm /tmp/choice.$$
#clear

## no download option for nala, use apt instead
#apt-get download $PACKAGE
#echo 'Done, the deb package has been downloaded, press <ENTER> to Exit...' && read
#mainmenu
#}

## simulate
#simulatemenu () {
#clear
#$DIALOG $TITLE"$TITLETEXT" $ENTRY $TEXT"\nType in a name of a package to simulate installation" $HEIGHT $WIDTH 2>/tmp/choice.$$
#if [ "$?" = "0" ]; then
#	PACKAGE=`cat /tmp/choice.$$`
#else
#	clear
#	mainmenu
#fi
#rm /tmp/choice.$$
#clear

## no simulate option for nala, use apt instead
#sudo apt install -s $PACKAGE
#echo 'Done, press <ENTER> to Exit...' && read
#mainmenu
#}

# install
installmenu () {
clear
$DIALOG $TITLE"$TITLETEXT" $ENTRY $TEXT"\nType in a name of a package to be installed" $HEIGHT $WIDTH 2>/tmp/choice.$$
if [ "$?" = "0" ]; then
	PACKAGE=`cat /tmp/choice.$$`
else
	clear
	mainmenu
fi
sudo rm /tmp/choice.$$
clear

sudo nala install $PACKAGE
echo 'Done, the package has been installed, if you did not break the installation, press <ENTER> to Exit...' && read
mainmenu
}

## reinstall
#reinstallmenu () {
#clear
#$DIALOG $TITLE"$TITLETEXT" $ENTRY $TEXT"\nType in a name of a package to be re-installed" $HEIGHT $WIDTH 2>/tmp/choice.$$
#if [ "$?" = "0" ]; then
#	PACKAGE=`cat /tmp/choice.$$`
#else
#	clear
#	mainmenu
#fi
#rm /tmp/choice.$$
#clear

## no reinstall option for nala, use apt instead
#sudo apt install --reinstall $PACKAGE
#echo 'Done, the package has been re-installed, if you did not break the installation, press <ENTER> to Exit...' && read
#mainmenu
#}

# remove
removemenu () {
clear
$DIALOG $TITLE"$TITLETEXT" $ENTRY $TEXT"\nType in a name of a package to be uninstalled" $HEIGHT $WIDTH 2>/tmp/choice.$$
if [ "$?" = "0" ]; then
	PACKAGE=`cat /tmp/choice.$$`
else
	clear
	mainmenu
fi
sudo rm /tmp/choice.$$
clear

sudo nala purge $PACKAGE
echo 'Done, press <ENTER> to Exit...' && read
mainmenu
}

# kernels
kernelmenu () {
clear
/usr/lib/sparky-package-tool/kernel
}

## fix
#fixmenu () {
#clear
## no fix option for nala, use apt instead
#sudo apt install -f
#echo 'Done, press <ENTER> to Exit...' && read
#mainmenu
#}

# clean
cleanmenu () {
clear
$DIALOG $TITLE"$TITLETEXT" $YESNO $TEXT"\n\nRemove unnecessary packages?" $HEIGHT $WIDTH
if [ $? = 0 ]; then
	clear
	sudo nala autoremove && echo 'Done, Press <ENTER> to Next Step...' && read
fi

$DIALOG $TITLE"$TITLETEXT" $YESNO $TEXT"\n\nClean up APT cache?" $HEIGHT $WIDTH
if [ $? = 0 ]; then
	clear
	sudo nala clean && echo 'Done, Press <ENTER> to Exit...' && read
fi
clear
mainmenu
}

mainmenu
