#!/bin/bash
#GUI .deb package installer, by PPC, 18-05-2021, beta version 0.9
#For now, try to reuse yad-updater's translations
TEXTDOMAINDIR=/usr/share/locale
TEXTDOMAIN=debinstaller

###Exit if run without a .deb file
if [ -z "${1}" ]; then
    echo "Please run this script with the .deb package you want to install. Exiting... " & exit
fi

###This shows .deb file's info:
dpkg-deb -I $1 > /tmp/deb_file_contents.txt
sed -i -n '/Package:/,$p' /tmp/deb_file_contents.txt
package=$(sed -n -e 's/^.*Package: //p' /tmp/deb_file_contents.txt)
package_version=$(sed -n -e 's/^.*Version: //p' /tmp/deb_file_contents.txt)
sed  -i -n '/Description:/,$p' /tmp/deb_file_contents.txt

echo " \b $package -\b" $package_version > /tmp/description.txt
echo "" >> /tmp/description.txt
cat /tmp/deb_file_contents.txt| sed -e 's/Description: //' >> /tmp/description.txt
cat  /tmp/description.txt
text=$(cat /tmp/description.txt)

###Function to Enter sudo password:
func_enter_password() {
 ### Check if user is Root, if not, pop up window asking for password, to run updater. If Canceled, exits.
 # adapted from  https://stackoverflow.com/questions/42875809/checking-sudo-in-bash-script-with-if-statements 
 if [[ "$EUID" = 0 ]]; then
    echo $"already root"
	else
     gksudo "Debinstaller"
     if sudo true; then
        echo $"You are Root or running the script in sudo mode"
      else
        echo $"You entered the wrong password or you cancelled"
        exit 1
    fi
 fi
}

func_apt_install() {
	sudo apt install $1 &&  yad --window-icon='/usr/share/icons/papirus-antix/32x32/apps/gnome-debian.png' --title=$"Debinstaller" --fixed --width=300 --height=100 --center --image=/usr/share/icons/numix-square-antix/32x32/actions/gtk-ok.png --text=$"\n Finished!\n You can read the log or close this window.  " --button='X':0
}	
	
###Main Window:
##Check if package is installed, show main window with "uninstall button" or without "uninstall button", accordingly
dpkg -s $package > /tmp/check_if_package_is_installed.txt
installed=$(cat /tmp/check_if_package_is_installed.txt | wc -l)
echo $installed

	if test $installed -gt 2
	then
		yad  --window-icon="/usr/share/icons/papirus-antix/32x32/apps/gnome-debian.png" --title=$"Debinstaller: $package  $package_version" --width=550 --height=450 --center --form --field=":TXT" "$text" --button=$"Uninstall !/usr/share/icons/papirus-antix/24x24/actions/xml-attribute-delete.png":2 --button=$"Install !/usr/share/icons/papirus-antix/24x24/actions/add.png":1
	 else
		yad  --window-icon="/usr/share/icons/papirus-antix/32x32/apps/gnome-debian.png" --title=$"Debinstaller: $package  $package_version" --width=550 --height=450 --center --form --field=":TXT" "$text" --button=$"Install !/usr/share/icons/papirus-antix/24x24/actions/add.png":1
	fi

foo=$?

###User selected to TRY TO PURGE PACKAGE
 if [[ $foo -eq 2 ]]; then
	yad --window-icon='/usr/share/icons/papirus-antix/32x32/apps/gnome-debian.png' --title=$"Debinstaller" --fixed --width=300 --height=100 --center --image=/usr/share/icons/papirus-antix/32x32/emblems/emblem-rabbitvcs-modified.png --text=$"This option will completely UNINSTALL (PURGE) $package from your system.   \n Click OK if you are sure you want to continue " --button='x':0 --button="OK":1
	foo2=$?
		if [[ $foo2 -eq 1 ]]; then
			#sudo -k ###is forces the user to always enter the sudo password, just in case the "uninstall" button was clicked by accident
			func_enter_password
			x-terminal-emulator -T "Debinstaller: $package" -e /bin/bash -c "sudo apt purge -y $package &&  yad --window-icon='/usr/share/icons/papirus-antix/32x32/apps/gnome-debian.png' --title='Debinstaller' --fixed --width=300 --height=100 --center --image=/usr/share/icons/numix-square-antix/32x32/actions/gtk-ok.png --text=$'\n Finished!\n You can read the log or close this window.  ' --button='x':0 || yad --center --title $'Error'" 
		fi
	
		if [[ $foo2 -eq 0 ]]; then
			exit
		fi
 fi	

####User selected to TRY TO INSTALL PACKAGE:
 if [[ $foo -eq 1 ]]; then
 export -f func_apt_install
	func_enter_password
	### .Deb file install procedure
	message_instalation_done=$" Finished - You can read the log or close this window  "
	x-terminal-emulator -T $"Debinstaller: $package" -e /bin/bash -c "sudo apt install $1 && yad --center --title=Debinstaller --window-icon='/usr/share/icons/papirus-antix/32x32/apps/gnome-debian.png' --fixed --width=300 --height=100 --image=/usr/share/icons/numix-square-antix/32x32/actions/gtk-ok.png  --text='$message_instalation_done' --button='x'|| yad --center --title=Debinstaller --window-icon='/usr/share/icons/papirus-antix/32x32/apps/gnome-debian.png'  --fixed --width=300 --height=100 --image=/usr/share/icons/papirus-antix/32x32/emblems/emblem-rabbitvcs-modified.png --text=' Error installing the package! \n  Please read the log on the terminal window below  ' --button='x'"
 fi
