#!/bin/bash
#
#================= Info ======================
# List rpm package, and more. 
#
# Distro: PCLinuxOS
# Author: D.M-Wilhelm (leiche)
# Website: http://pclinuxos.com
# Licence: GPLv2
# Required: xterm, gtkdialog, rpm
#           dnf5
#
Encoding=UTF-8
#
# i18n - Internationalization - Internationalisierung
export TEXTDOMAIN="rpmApp"
export TEXTDOMAINDIR="/usr/share/locale"
#
#some variables
TITLE="PCLinuxOS-RPM"
VERSION="0"
#if [ -f /usr/bin/sudo ];then
#GSU='xterm -e sudo'
#else
GSU='xterm -title "Enter_root_password" -e su -c'
#fi
TMP=$HOME/.cache/SiCuSys
if [ -f /usr/bin/kwrite ];then
EDITOR=kwrite
elif [ -f /usr/bin/gedit ];then
EDITOR=gedit
elif [ -f /usr/bin/medit ];then
EDITOR=medit
elif [ -f /usr/bin/mousepad ];then
EDITOR=mousepad
elif [ -f /usr/bin/featherpad ];then
EDITOR=featherpad
else
EDITOR=leafpad
fi
#
#check if dnf5 installed
if [ ! -f /usr/bin/dnf5 ]; then
  DNF='<vbox tooltip-text="'$"DNF5 is required."'">	
			<button>
        		<label>InstallDNF5</label>
        		<input file icon="install"></input>
        		<action>'$GSU' "apt-get install lib64dnf5-cli ; read"</action>
			</button> 
		</vbox>'
fi		
#
#create a searchfile
echo $"Search for an installed package, or a new package.
List packages recently added to the repositories." > $TMP/searchfile
#
{
export QUESTION='
<window window_position="2" title="'$TITLE' '$VERSION'" width-request="800" icon-name="info">
	<vbox>
			<vbox>
				<frame> 
					<vbox scrollable="true">					  
						<text>
							<label>"'$"Are sure to reboot now? 
Make sure you have completed all tasks. Save any open files!"'"</label>
						</text>  
					</vbox> 
				</frame>
			</vbox>
			
			<hbox>
				<vbox>
					<button tooltip-text="'$"Reboot now your system..."'">
						<input file icon="gtk-yes"></input>
						<variable>QUESTION</variable>
						<label>'$"Yes"'</label>
						<action>reboot &</action>
						<action type="exit">DIALOG</action>
					</button>
				</vbox>  
				<vbox> 
					<button>
						<label>'$"Cancel"'</label>
						<input file icon="gtk-cancel"></input>
						<action type="closewindow">QUESTION</action>
					</button>
				</vbox> 
			</hbox>
	</vbox>
</window>
'
}
{
RPMAPP='
<window window_position="1" title="'$TITLE' '$VERSION'" width-request="800" icon-name="synaptic" resizable="false">
	<vbox>
		<frame>
			<vbox>
				<text use-markup="True">
					<label>"'$"<big><b>Packages: </b></big>`rpm -qa | grep -c pclos`"'"</label>
				</text>
				<text use-markup="True">
					<label>"'$"The latest package that is installed on your system.

<b>`rpm -qa --last | head -n 1`</b>"'"</label>
				</text>
			</vbox>
			<vbox>
				<frame> 
					<vbox  tooltip-text="'$"If a package installed, and found, you can remove it with the remove button."'">					
						<tree rules_hint="true" exported_column="0">
							<width>400</width><height>200</height>
							<label>'$"Results"'</label>
							<variable>FIND</variable>
							<input file>'$TMP'/searchfile</input>
						</tree>  
					</vbox>
					<hbox>
						<text use-markup="True"><label>"<big><b>'$"Type a package name -->"'</b></big>"</label></text>
						<entry>
							<variable>ENTRY</variable>
						</entry>
						<vbox>
						<button tooltip-text="'$"Search for a package to install..."'">
							<input file icon="gtk-find"></input>
							<label>'$"NewPackage"'</label>
							<action>dnf5 search $ENTRY > '$TMP'/searchfile</action>
							<action type="refresh">FIND</action>
							<action function="enable">NSTL</action>
							<action function="disable">RMV</action>
							<action function="disable">RMV1</action>
						</button>
						<button tooltip-text="'$"Search for an installed package..."'">
							<input file icon="gtk-find"></input>
							<label>'$"InstalledPackage"'</label>
							<action>rpm -qa | grep $ENTRY > '$TMP'/searchfile</action>
							<action type="refresh">FIND</action>
							<action function="enable">RMV</action>
							<action function="enable">RMV1</action>
							<action function="disable">NSTL</action>
						</button>
						</vbox>
					</hbox>	
				</frame>
				<hbox>
					<button  tooltip-text="'$"Make sure you have completed all tasks. Save any open files!"'">
						<label>'$"Reboot"'</label>
						<input file icon="exec"></input>
						<action type="launch">QUESTION</action>
						##<action>reboot &</action>
						##<action type="exit">DIALOG</action>
					</button>
					'$DNF'
					<button tooltip-text="'$"List only packages recently added to the repositories."'">
						<input file icon="list-add"></input>
						<label>'$"List"'</label>
						<action>dnf5 list --recent > '$TMP'/searchfile</action>
						<action type="refresh">FIND</action>
					</button>
					<button  tooltip-text="'$"You can remove a selected package"'">
						<input file icon="remove"></input>
						<visible>false</visible>
						<variable>RMV</variable>
						<label>'$"Remove"'</label>
						<action>'$GSU' "dnf5 rm $FIND ; read"</action>
					</button>
					<button  tooltip-text="'$"Get some informations of a new selected packages"'">
						<input file icon="gtk-info"></input>
						<label>'$"Info"'</label>
						<action>xterm -e "dnf5 info `echo $FIND | cut -d " " -f1` ; read"</action>
					</button>					
					<button  tooltip-text="'$"Install a new selected packages"'">
						<input file icon="install"></input>
						<visible>false</visible>
						<variable>NSTL</variable>						
						<label>'$"Install"'</label>
						<action>'$GSU' "dnf5 in `echo $FIND | cut -d " " -f1` ; read"</action>
					</button>
					<button  tooltip-text="'$"Note: it will only update your system, not show any new package or new kernel version"'">
						<input file icon="system-software-update"></input>
						<label>'$"Upgrade"'</label>
						<action>'$GSU' "dnf5 update --refresh ; read"</action>
					</button>
					<button tooltip-text="'$"Read the dnf5 logfile..."'">
						<input file icon="fileopen"></input>
						<label>'$"Logfile"'</label>
						<action>'$EDITOR' /var/log/dnf5.log &</action>
					</button>
					<vbox tooltip-text="'$"See changelog, indeed a history..."'">  
					<button>
						<input file icon="gtk-find"></input>
						<label>'$"Changelog"'</label>
						<visible>false</visible>
						<variable>RMV1</variable>
						<action>xterm -hold -e rpm -q --changelog $FIND &</action>
					</button>
				</vbox>
					<button>
						<label>'$"Close"'</label>
						<input file stock="gtk-close"></input>
						<action type="exit">DIALOG</action>
					</button>
				</hbox>
			</vbox>
		</frame>
	</vbox>
</window>
'
}
export GUI="`echo "$RPMAPP" | sed -e 's/##.*//'`" 
gtkdialog -p GUI #> /dev/null 2>&1
rm -r $TMP/searchfile
