#!/bin/bash
############################################
# Author: Daniel Meiß-Wilhelm              #
# Website: <www.pclinuxos.com/forum> 	   #
# Designed for PCLinuxOS			       #
# Release: 2023 - 2025  		           #
# License: GPL2   						   #
# Requires: gtkdialog, xterm, sed	       #
############################################
#
export TEXTDOMAIN=mynvidia
export TEXTDOMAINDIR="/usr/share/locale"
# Only root permissions 
script_dir="$( cd "$( dirname "$0" )" && pwd )"
script="`basename $0`"
script_path_n_name="${script_dir}/${script}"
GUI_SU="pkexec env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY"
if [ "$UID" != "0" ]; then
$GUI_SU bash $script_path_n_name; exit;
fi
# Some variables
WNAME="MyNvidia-Installer"
ICON="utilities-system-monitor"
DRNM="$(dirname "$(readlink -e "$0")")"
NVD340="$DRNM"/nvidia340.txt
NVD470="$DRNM"/nvidia470.txt
NVD525="$DRNM"/nvidia525.txt
RMV=`rpm -qa | grep   driver-video-nvidia | cut -d "." -f1 | sed  "s/-470//;s/-525//;s/-340//"`
TTFNM=`lspci | grep "VGA compatible controller" | cut -d ":" -f3 | grep -i nvidia | cut -d "]" -f1 | cut -d "[" -f2 | sed -e 's/Mobile//' | cut -d / -f1 | grep -c $`
if [ "$TTFNM" == "1" ];then
CRDNM=`lspci | grep "VGA compatible controller" | cut -d ":" -f3 | grep -i nvidia | cut -d "]" -f1 | cut -d "[" -f2 | sed -e 's/Mobile//' | cut -d / -f1`
READ0=$(cat $NVD340 | grep -c "`echo $CRDNM`")
READ1=$(cat $NVD470 | grep -c "`echo $CRDNM`")
READ2=$(cat $NVD525 | grep -c "`echo $CRDNM`")
	if [ $READ0 -ge "1" ];then 
		RDSTS=Nvidia340
		echo mynvidia-installer: $RDSTS
		NFTXT=$"You can use the $RDSTS driver or may a newer..."
		RDFL=$NVD340
		#fix me, it's need to long time for open
		MNFTN='<vbox tooltip-text="'$"See Supported NVIDIA GPU Products, Takes a little time..."'"><button><label>'$RDSTS'</label><input file stock="gtk-help"></input><action type="launch">INFO_NVIDIA</action></button></vbox>'
	elif [ $READ1 -ge "1" ];then 
		RDSTS=Nvidia470
		echo mynvidia-installer: $RDSTS
		NFTXT=$"You can use the $RDSTS driver or may a newer..."
		RDFL=$NVD470
		#fix me, it's need to long time for open
		MNFTN='<vbox tooltip-text="'$"See Supported NVIDIA GPU Products, Takes a little time..."'"><button><label>'$RDSTS'</label><input file stock="gtk-help"></input><action type="launch">INFO_NVIDIA</action></button></vbox>'
	elif [ $READ2 -ge "1" ];then 
		RDSTS=Nvidia525
		echo mynvidia-installer: $RDSTS
		NFTXT=$"You can use the $RDSTS driver or may a newer..."
		RDFL=$NVD525
		#fix me, it's need to long time for open
		MNFTN='<vbox tooltip-text="'$"See Supported NVIDIA GPU Products, Takes a little time..."'"><button><label>'$RDSTS'</label><input file stock="gtk-help"></input><action type="launch">INFO_NVIDIA</action></button></vbox>'
	else
		RDSTS=$"no nvidia"
		echo mynvidia-installer: $RDSTS
		NFTXT=$"<b>No Nvidia Card found...</b>"
	fi
else	
NFTXT=$"<b>No Nvidia Card found...</b>"
fi
# commands for dnf or apt
if [ -f /usr/bin/dnf5 ];then
REMOVE="dnf5 rm"
else
REMOVE="apt-get remove"
fi
if [ `rpm -qa | grep -c  driver-video-nvidia ` -ge 1 ];then
STS=`rpm -qa | grep   driver-video-nvidia`
# If installed a nvidia driver, may it must first removed dialog
export INFO_DIALOG='
<window title="'$WNAME'" icon-name="'$ICON'">
	<vbox>
		<hbox border-width="30">
			<text use-markup="True">
				<label>"'$"<b>The '$STS'
driver are installed!</b> 


If you want to install a new driver, you must remove first the current driver."'"</label>
			</text>
		</hbox>
		<hseparator></hseparator>
		<vbox>
			<button>
				<label>'$"Remove"'</label>
				<action>xterm -title "MyNvidiaInstaller_'$NVD'" -e "'$REMOVE' '$RMV'"</action>
			</button>
		</vbox>
		<vbox>
			<button ok></button>
		</vbox>
	</vbox>
</window>
'
gtkdialog --program=INFO_DIALOG 
exit 0
fi
# What it is dialog
export ABOUT='
<window window_position="1" title="'$WNAME'" width-request="500" height-request="400" icon-name="'$ICON'">
	<vbox scrollable="true">
		<frame About>
			<text use-markup="True">
				<label>"'$"MyNvidiaInstaller

will install the Nvidia driver, and set it up.


 Designed for PCLinuxOS
 Author:	Daniel Meiß-Wilhelm
 Website: 	www.pclinuxos.com/forum
 Release: 	2023
 License: 	GPL2 
 
"'"</label>
			</text>
		</frame>
		<hbox>
		<vbox>
			<button>
				<label>'$"Close"'</label>
				<input file stock="gtk-ok"></input>
				<variable>ABOUT</variable>
				<action type="closewindow">ABOUT</action>
			</button>
		</vbox>
		</hbox>
	</vbox>
</window>
'
# See Supported NVIDIA GPU Products dialog
export INFO_NVIDIA='
<window title="'$WNAME'" width-request="600" height-request="800" icon-name="'$ICON'">
	<vbox>
		<vbox scrollable="true">
				<text selectable="true">
					<input file>'$RDFL'</input>
				</text>
			</vbox>
		<hseparator></hseparator>
		<vbox>
			<button>
				<label>'$"Close"'</label>
				<input file stock="gtk-ok"></input>
				<variable>INFO_NVIDIA</variable>
				<action type="closewindow">INFO_NVIDIA</action>
			</button>
		</vbox>
	</vbox>
</window>
'
# main NVIDIA Installer
export MyNvidiaInstaller='
<window window_position="1" title="'$WNAME'" width-request="300" icon-name="'$ICON'">
	<vbox>
		<frame '$"PCLinuxOS Nvidia Installer"'>
			<vbox tooltip-text="'$"Choose your Nvidia driver and click on Install button. "'">
				<text use-markup="True"><label>"'$"<b>Video Card '$CRDNM'</b> 
				
$NFTXT

Choose your driver: "'"</label></text>
					<comboboxentry width-request="60" case-sensitive="false" value-in-list="false">
						<variable>NVD</variable>
						<item>nvidia340</item>
						<item>nvidia470</item>
						<item>nvidia525</item>
					</comboboxentry>
			</vbox>
		</frame>	
		<vbox>
		'$MNFTN'
		<button>
			<label>"'$" Install "'"</label>
			<input file icon="document-save"></input>
			<action type="exit">save</action>
		</button>
		<button>
			<label>"'$" Cancel "'"</label>
			<input file icon="gtk-cancel"></input>
			<action type="exit">cancel</action>
		</button>
		<button>
			<label>'$"About"'</label>
			<input file stock="gtk-help"></input>
			<action type="launch">ABOUT</action>
		</button>
		</vbox>
	</vbox>
</window>
'
#-------------------------- end main dialog ----------------------------------------------
{
I=$IFS; IFS=""
for STATEMENT in  $(gtkdialog --center --program=MyNvidiaInstaller); do
	eval $STATEMENT
done
IFS=$I
	
	
# Process variables if dialog exit status == save
# If set NVIDIA 340 
if [ "$EXIT" == "save" ];then
	if [ $NVD ==  "nvidia340" ];then
	echo $NVD will installed
	if [ -f /usr/bin/apt ];then
		cat <<EOF >/etc/apt/sources.list.d/nvidia.list
rpm http://ftp.nluug.nl/pub/os/Linux/distr/pclinuxos/pclinuxos/apt/ pclinuxos/64bit nvidia340
EOF
		xterm -title "MyNvidiaInstaller_$NVD" -e "apt-get update && apt-get install dkms-nvidia340 -y && XFdrake --auto"
	else
		xterm -title "MyNvidiaInstaller_$NVD" -e "dnf5 config-manager setopt  nvidia340.enabled=1  nvidia470.enabled=0 nvidia525.enabled=0 && dnf5 in dkms-nvidia340 -y && XFdrake --auto"
	fi

export INFO_DIALOG='
<window title="'$WNAME_$NVD'" icon-name="'$ICON'">
	<vbox>
		<hbox border-width="30">
			<text use-markup="True">
				<label>"'$"<b>Installation finished</b> 

Please now reboot to use the Nvidia driver"'"</label>
			</text>
		</hbox>
		<hseparator></hseparator>
		<vbox>
			<button ok></button>
		</vbox>
	</vbox>
</window>
'
gtkdialog --program=INFO_DIALOG 
# If set NVIDIA 470
	elif [ $NVD ==  "nvidia470" ];then
	echo $NVD will installed
	if [ -f /usr/bin/apt ];then
		cat <<EOF >/etc/apt/sources.list.d/nvidia.list
rpm http://ftp.nluug.nl/pub/os/Linux/distr/pclinuxos/pclinuxos/apt/ pclinuxos/64bit nvidia470
EOF
		xterm -title "MyNvidiaInstaller_$NVD" -e "apt-get update && apt-get install dkms-nvidia-current -y && XFdrake --auto"
	else
		xterm -title "MyNvidiaInstaller_$NVD" -e "dnf5 config-manager setopt  nvidia340.enabled=0  nvidia470.enabled=1 nvidia525.enabled=0 && dnf5 in dkms-nvidia-current -y && XFdrake --auto"
	fi
export INFO_DIALOG='
<window title="'$WNAME_$NVD'" icon-name="'$ICON'">
	<vbox>
		<hbox border-width="30">
			<text use-markup="True">
				<label>"'$"<b>Installation finished</b> 

Please now reboot to use the Nvidia driver"'"</label>
			</text>
		</hbox>
		<hseparator></hseparator>
		<vbox>
			<button ok></button>
		</vbox>
	</vbox>
</window>
'
gtkdialog --program=INFO_DIALOG 
# If set NVIDIA 525
	elif [ $NVD ==  "nvidia525" ];then
	echo mynvidia-installer: $NVD will installed
	if [ -f /usr/bin/apt ];then
		cat <<EOF >/etc/apt/sources.list.d/nvidia.list
rpm http://ftp.nluug.nl/pub/os/Linux/distr/pclinuxos/pclinuxos/apt/ pclinuxos/64bit nvidia525
EOF
		xterm  -title "MyNvidiaInstaller_$NVD" -e "apt-get update && apt-get install dkms-nvidia-current -y && XFdrake --auto"
	else
				xterm -title "MyNvidiaInstaller_$NVD" -e "dnf5 config-manager setopt  nvidia340.enabled=0  nvidia470.enabled=0 nvidia525.enabled=1 && dnf5 in dkms-nvidia-current -y && XFdrake --auto"
	fi	
export INFO_DIALOG='
<window title="'$WNAME_$NVD'" icon-name="'$ICON'">
	<vbox>
		<hbox border-width="30">
			<text use-markup="True">
				<label>"'$"<b>Installation finished</b> 

Please now reboot to use the Nvidia driver"'"</label>
			</text>
		</hbox>
		<hseparator></hseparator>
		<vbox>
			<button ok></button>
		</vbox>
	</vbox>
</window>
'
gtkdialog --program=INFO_DIALOG 
	else
# Do nothing
	echo mynvidia-installer: $"Settings are not changed, exiting!"
	fi
fi
}
