#!/bin/bash
############################################
# Author: Daniel Meiß-Wilhelm              #
# Website: <www.pclinuxos.com/forum> 	   #
# Designed for PCLinuxOS			       #
# Release: 2023 - 2025 		               #
# License: GPL2   						   #
# Requires: dialog,	sed		           	   #
############################################
#
export TEXTDOMAIN=mynvidia-cli
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
DIALOG="dialog --backtitle PCLinuxOS --title MyNvidia-Installer --clear"
DRNM="/usr/share/mynvidia-installer"
## If installed a nvidia driver, may it must first removed dialog
if [ `rpm -qa | grep -c  driver-video-nvidia ` -ge 1 ];then
	STS=`rpm -qa | grep   driver-video-nvidia | cut -d "." -f1`
	RMV=`rpm -qa | grep   driver-video-nvidia | cut -d "." -f1 | sed  "s/-470//;s/-525//;s/-340//"`
	$DIALOG --msgbox $"\n\nThe $STS driver is installed!\n\nIf you want to install a new driver, you must remove first the current driver." 15 40
	$DIALOG --yesno $"\n\nDo you want remove the Nvidia driver?" 15 40
	RST=${?}
	if [ "$RST" -eq "0" ];then
		if [ -f /usr/bin/apt ];then
			apt-get remove $RMV
		else
			dnf5 rm $RMV
		fi	
	$DIALOG --msgbox $"\n\nRestart mynvidia-cli ..." 10 40
	exit 0
	else
	exit 1
	fi
fi	

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`
fi

## main NVIDIA Installer cli
SET=$(dialog --stdout --backtitle PCLinuxOS --title MyNvidia-Installer --clear --radiolist $"Choose Your Nvidia Driver\n$CRDNM" 16 80 5 \
     "Nvidia340" "Legacy Driver" off \
     "Nvidia470" "Current Driver" off \
     "Nvidia525" "Latest Driver" off)


case "$SET" in
  Nvidia340) ##if set the 340
    $DIALOG --textbox "$DRNM/nvidia340.txt" 0 0
    $DIALOG --yesno $"\n\nDo you want install the Nvidia340 driver?" 15 40
    RST=${?}
    if [ "$RST" -eq "0" ];then
     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
		apt-get update && apt-get install dkms-nvidia340 -y && XFdrake --auto
	else
		dnf5 config-manager setopt  nvidia340.enabled=1  nvidia470.enabled=0 nvidia525.enabled=0 && dnf5 in dkms-nvidia340 -y && XFdrake --auto
	fi	
    $DIALOG --yesno $"\n\nInstallation finished.\n\nPlease now reboot to use the Nvidia driver.\nDo you want to restart now?" 12 50
    SST=${?}
    if [ "$SST" -eq "0" ];then
		reboot
		else
		exit 1
	fi	
    else
    $DIALOG --msgbox $"\n\nTry again, or reboot..." 10 40
    fi
    exit 0
    ;;
  Nvidia470) ##if set the 470
    $DIALOG --textbox "$DRNM/nvidia470.txt" 0 0
    $DIALOG --yesno $"\n\nDo you want install the Nvidia470 driver?" 15 40
    RST=${?}
    if [ "$RST" -eq "0" ];then
		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
			apt-get update && apt-get install dkms-nvidia-current -y && XFdrake --auto
		else
			dnf5 config-manager setopt  nvidia340.enabled=0  nvidia470.enabled=1 nvidia525.enabled=0 && dnf5 in dkms-nvidia-current -y && XFdrake --auto
		fi	
    $DIALOG --yesno $"\n\nInstallation finished.\n\nPlease now reboot to use the Nvidia driver.\nDo you want to restart now?" 12 50
    SST=${?}
    if [ "$SST" -eq "0" ];then
		reboot
		else
		exit 1
	fi	
    else
    $DIALOG --msgbox $"\n\nTry again, or reboot..." 10 40
    fi
    exit 0
    ;;
  Nvidia525) ##if set the 525
    $DIALOG --textbox "$DRNM/nvidia525.txt" 0 0
    $DIALOG --yesno $"\n\nDo you want install the Nvidia525 driver?" 15 40
     RST=${?}
    if [ "$RST" -eq "0" ];then
		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
			apt-get update && apt-get install dkms-nvidia-current -y && XFdrake --auto
		else
			dnf5 config-manager setopt  nvidia340.enabled=0  nvidia470.enabled=0 nvidia525.enabled=1 && dnf5 in dkms-nvidia-current -y && XFdrake --auto
		fi	
    $DIALOG --yesno $"\n\nInstallation finished.\n\nPlease now reboot to use the Nvidia driver.\nDo you want to restart now?" 12 50
    SST=${?}
    if [ "$SST" -eq "0" ]; then
		reboot
		else
		exit 1
	fi	
    else
    $DIALOG --msgbox $"\n\nTry again, or reboot..." 10 40
    fi
    exit 0
    ;;
esac

