#!/bin/bash
##
## RJP 27.3.2024 https://puolanka.info/goto/apt-package-helper/
# An alternative way to check, install and remove packages
# 30.3.2024 Added available updates list and free package selection
# 2.4.2024 Some programs added for multi selection
# 3.4.2024 added more functions
# 22.4.2024 titles added to boxes
# 12.7.2025 xterm set is stead of x-terminal-emulator
# 12.7.2025 network check added
# 12.7.2025 .Xresources function for Monospace font added
# 29.5.2026 more simple dialogs made for install and remove sections
#

if ! nc -zw1 google.com 443; then 
yad --width=400 --text-align=center --center --title=$"No network connection:" --text="
     No network connection
       
    Add network connection and try again  "
fi

touch ~/.Xresources
echo ' ! Use a truetype font and size.
xterm*faceName: Monospace ' | grep -xFv -f $HOME/.Xresources >> $HOME/.Xresources
echo 'xterm*faceSize: 14 ' | grep -xFv -f $HOME/.Xresources >> $HOME/.Xresources
xrdb -merge ~/.Xresources

1=$({                                   
echo '#!/bin/bash
PROGRAM=`yad --center --width=500 --height=100 --title=$"Program Status" --text-align=center --text="You can check program status by writing program name to text box and pressing then ok" --entry --entry-label=NAME --entry-text=""`
if [ $? = 0 ];
then
xterm -hold -e /bin/bash -c "apt-cache policy $PROGRAM && yad --center --borders=5 --width=100 --button=OK:1"
else exit 0
fi '
} > /tmp/status) 

2=$({ 
echo '#!/bin/bash
PROGRAM=`yad --center --width=700 --height=100 --title=$"Install Program" --text-align=center --text="Program you want to install. If there has no that program, script ends" --entry --entry-label=SEARCH --entry-text=""`
if [ $? = 0 ];
then
xterm -hold -e /bin/bash -c "sudo apt install $PROGRAM && yad --center --borders=5 --width=100 --button=OK:1"
else exit 0
fi '
} > /tmp/install) 


3=$({ 
echo '#!/bin/bash
PROGRAM=`yad --center --width=500 --height=100 --title=$"Remove Program" --text-align=center --text="Program you want to remove" --entry --entry-label=is --entry-text=""`
if [ $? = 0 ];
then
xterm -hold -e /bin/bash -c "sudo apt remove $PROGRAM "
fi'
} > /tmp/remove)

4=$({ 
echo '#!/bin/bash
LIST=$(apt-cache search . |sort -d |less)
echo "$LIST" | yad --list --title "Search Results" --width=1000 --height=500 --text "INFORMATION" --column "Search Results" --button=OK:1'
} > /tmp/results)

5=$({ 
echo '#!/bin/bash
yad --center --width=400 --height=100 --title=$"Update Manager" --text-align=center --text="Update your system?"
if [ $? = 0 ];
then
xterm -hold -e /bin/bash -c "sudo apt update && sudo apt upgrade && sudo apt-get -fy install && sleep 3"
yad --center --width=400 --height=100 --title=$"System Upgraded" --text-align=center --text="System Upgraded"
fi'
} > /tmp/update)

6=$({ 
echo '#!/bin/bash
xterm -e /bin/bash -c "sudo apt update "
sleep 1
apt list --upgradable > /tmp/aptlist
yad  --list --width=1500 --height=500  \
--button=exit:1 \
--column=Available \
--column=Upgradeable < /tmp/aptlist 
--print-column=2 --separator= && \'
} > /tmp/available)

7=$({ 
echo '#!/bin/bash
yad  --list --title=$"Select Programs to Install" --width=800 --height=500 --checklist \
--button="Select programs and then run Install selected packages from the main menu":0 --button=Exit:1 \
--column="Select for installation" \
--column=Package < /tmp/list > /tmp/install2 \
--print-column=2 --separator= && \
--print --filename=/tmp/install2
echo $(cat /tmp/install2) > /tmp/install3
'
} > /tmp/packages)

8=$({ 
echo '#!/bin/bash
xterm -hold -e /bin/bash -c "sudo apt install $(cat /tmp/install3) "
'
} > /tmp/multi)

9=$({ 
echo 'false
chromium
false
gimp
false
caja
false
pluma
false
atril
false
eom
false
engrampa
false
mate-terminal
false
caja-open-terminal
false
mate-utils
false
xterm
false
vlc
false
mpv
false
smplayer
false
audacious
false
audacity
false
k3b
false
pdfarranger
false
geany
false
thunderbird
false
gftp
false
dillo
false
otter-browser
false
seamonkey
false
xsane
false
scribus
false
kdenlive
false
krita
false
redshift
false
grsync'
} > /tmp/list)


chmod +x /tmp/status
chmod +x /tmp/install
chmod +x /tmp/remove
chmod +x /tmp/results
chmod +x /tmp/update
chmod +x /tmp/available
chmod +x /tmp/packages
chmod +x /tmp/multi


#!/bin/bash
yad --form --title=$"APT PACKAGE HELPER" --columns=4 --rows=3 --width=1000 --text="APT PACKAGE HELPER:" \
\
--field="Program Status Checker":fbtn "/tmp/./status " \
--field="Program Installer":fbtn "/tmp/./install " \
--field="Program Remover":fbtn "/tmp/./remove " \
--field="Available Updates":fbtn "/tmp/./available " \
--field="Selection for package installation":fbtn "/tmp/./packages " \
--field="Install selected packages":fbtn "/tmp/./multi " \
--field="Install Updates":fbtn "/tmp/./update " \
--field="All available packages (this takes time)":fbtn "/tmp/./results " \
--field="Update history":fbtn "xterm -e bash -c 'grep "upgrade" /var/log/dpkg.log; exec bash' & " \
--field="Install history":fbtn "xterm -e bash -c 'grep "install" /var/log/dpkg.log; exec bash' & " \
--field="APT autoremove":fbtn "xterm -e bash -c 'sudo apt autoremove; exec bash' & " \
--field="APT clean":fbtn "xterm -e bash -c 'sudo apt clean; exec bash' & " \
--button=Exit:1

