#!/bin/sh
##
## RJP 19.6.2025 PKG HELPER for making BSD based systems easier to use
# An alternative way to check, install and remove packages
# 5.7.2025 fixed update entry and package selection
##aika=$(date "+%F-%H-%M")
# packages yad, xterm and sh must be installed
# 8.7.2025 Update entry has changed by removing yad message and adding xterm message, because lost network connection causes wrong information
# 10.7.2025 more simple update section
# 12.7.2025 network check added
#
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

1=$({                                   
echo '#!/bin/sh
PROGRAM=`yad --center --width=500 --height=100 --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/sh -c "pkg search $PROGRAM && yad --center --borders=5 --width=100 --button=OK:1"
else exit 0
fi '
} > /tmp/status) 

2=$({ 
echo '#!/bin/sh
PROGRAM=`yad --center --width=500 --height=100 --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 -e /bin/sh -c "sudo pkg install $PROGRAM && yad --center --borders=5 --width=100 --button=OK:1"
yad --center --width=500 --height=100 --text-align=center --text="$PROGRAM is installed or if script died without OK, there has no that program as $PROGRAM "
else exit 0
fi '
} > /tmp/install) 


3=$({ 
echo '#!/bin/sh
PROGRAM=`yad --center --width=500 --height=100 --text-align=center --text="Program you want to remove" --entry --entry-label=is --entry-text=""`
if [ $? = 0 ];
then
xterm -e /bin/sh -c "sudo pkg delete $PROGRAM "
yad --center --width=500 --height=100 --text-align=center --text="$PROGRAM is removed or it has not been here"
fi'
} > /tmp/remove)

4=$({ 
echo '#!/bin/sh
LIST=$(pkg info --all )
echo "$LIST" | yad --list --title "Search Results" --width=600 --height=500 --text "INFORMATION" --column "Search Results" --button=OK:1'
} > /tmp/results)

5=$({ 
echo '#!/bin/sh
yad --center --width=400 --height=100 --text-align=center --text="Update your system?"
if [ $? = 0 ];
then
xterm -hold -e /bin/sh -c "sudo pkg update -f && sudo pkg upgrade "

fi'
} > /tmp/update)

6=$({ 
echo '#!/bin/sh
yad  --list --width=700 --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/all.txt > /tmp/install2 \
--print-column=2 --separator= && \
--print --filename=/tmp/install2
echo $(cat /tmp/install2) > /tmp/install3
'
} > /tmp/packages)

7=$({ 
echo '#!/bin/sh
xterm -e "sudo pkg install $(cat /tmp/install3) " 
sleep 1
yad --center --width=300 --height=100 --text-align=center --title="INFORMATION" --text=" If some package was not available or it was installed already, so the script died from that reason. Otherwise you saw terminal windows who told you the installation process. You can close this window"
'
} > /tmp/multi)

8=$({ 
echo 'This is a graphical script or program, which can help to manage BSD system packages. Free to use and modify. Copyright RJP@2025. More info from https://puolanka.info/.
'
} > /tmp/readme.txt)

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

##awk 'ORS=NR%2?FS:RS' /tmp/all.txt > /tmp/all0.txt

##awk '{print $1}' /tmp/all0.txt > /tmp/all.txt

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

echo '7-zip
atril
audacious
avidemux
bash
caja
caja-extensions
chromium
conky
dbus
dconf
dconf-editor
engrampa
eom
firefox ' > /tmp/all.txt

echo 'fuse-zip
fusefs-exfat
fusefs-ext2
fusefs-hfsfuse
gnome-icon-theme
hicolor-icon-theme
icewm
icewm-extra-themes
la-capitaine-icon-theme
marco
mate
mate-applets
mate-backgrounds 
mate-control-center
mate-desktop ' >> /tmp/all.txt

echo 'mate-menus
mate-panel
mate-polkit
mate-session-manager
mate-settings-daemon
mate-system-monitor
mate-terminal
mate-utils
nano
networkmgr
nordic-theme
octopkg
pkg
pluma
polkit ' >> /tmp/all.txt

echo 'pop-icon-theme
pulseaudio
rox-filer
sudo
thunar
twm
unzip
upower
win98se-icon-theme
xauth
xdg-user-dirs
xdg-utils
xgamma
xorg
xrandr ' >> /tmp/all.txt

echo '
vlc
xterm
yad
yt-dlp
zenity
lightdm
lightdm-gtk-greeter
 ' >> /tmp/all.txt


#!/bin/sh
yad --form --columns=4 --rows=6 --width=900 --title="PKG HELPER" --text=" PKG HELPER:" \
\
--field="Program Status Checker":fbtn "/tmp/./status " \
--field="Program Installer":fbtn "/tmp/./install " \
--field="Program Remover":fbtn "/tmp/./remove " \
--field="Install Updates":fbtn "/tmp/./update " \
--field="Selection for package installation":fbtn "/tmp/./packages " \
--field="Install selected packages":fbtn "/tmp/./multi " \
--field="Autoremove packages":fbtn "xterm -e sh -c 'sudo pkg autoremove ; exec sh' & " \
--field="installed packages ":fbtn "/tmp/./results " \
--field="User installed packages":fbtn "xterm -e sh -c 'pkg prime-list ; exec sh' & " \
--field="Package stats":fbtn "xterm -e sh -c 'pkg stats; exec sh' & " \
--field="clear package cache":fbtn "xterm -e sh -c 'sudo pkg clean; exec sh' & " \
--field="Auditing Installed":fbtn "xterm -e sh -c 'sudo pkg audit -F; exec sh' & " \
--field="clear full package cache":fbtn "xterm -e sh -c 'sudo pkg clean -a ; exec sh' & " \
--field="README":fbtn "sh -c 'xdg-open /tmp/readme.txt ; exec sh' & " \
--button=Exit:1

cd /tmp && rm status install remove results update available packages multi all.txt all0.txt audio.txt browser.txt office.txt picture.txt media.txt system.txt video.txt install2 install3
