#!/bin/sh
#!
#! 20240121 how to install nvidia-settings
#!
#! 1. Get the version matching your current driver at https://download.nvidia.com/XFree86/nvidia-settings/
#!    EXAMPLE:  wget https://download.nvidia.com/XFree86/nvidia-settings/nvidia-settings-545.29.06.tar.bz2
#!
#! 2. Extract the archive
#!    EXAMPLE:  tar -xf nvidia-settings-545.29.06.tar
#!
#! 3. Build and install
#!    EXAMPLE:  sudo make && sudo make install
#!
#! 4. If you lack files install the following packages:
#!    sudo apt-get install build-essential libgtk-3-dev libxxf86vm-dev libxv-dev libvdpau-dev
#!

#! Make a temporary directory to download everything to
TMPDIR=`mktemp -d`
cd $TMPDIR

#! Get required packages
sudo apt-get install build-essential libgtk-3-dev libxxf86vm-dev libxv-dev libvdpau-dev

#! Find out which version of the Nvidia driver we're using
VER=`dmesg | grep NVIDIA.*Kernel.*Module | awk -F"Module" '{print $2}' | awk '{print $1}'`
if [ -z "$VER" ]; then
	VER=`nvidia-smi | grep Driver\ Version | awk -F"Driver Version: " '{print $2}' | awk '{print $1}'`
fi
if [ -z "$VER" ]; then
	echo "Unable to get Nvidia driver version from either kernel messages or nvidia-smi.  Exiting."
	exit 1
fi

#! Get the nvidia-settings package
wget https://download.nvidia.com/XFree86/nvidia-settings/nvidia-settings-$VER.tar.bz2

#! Extract
tar -xf nvidia-settings-$VER.tar.bz2

#! Build
cd nvidia-settings-$VER
sudo make && sudo make install

#! Copy the library to the main location avoiding having to change PATH or PKG_PATH or anything.
sudo cp /usr/local/lib/libnvidia-* /usr/lib/

#! Remind us if we're happy with this we can delete the directory
echo "If nvidia-settings works as you like, feel free to     sudo rm -rf $TMP."

exit 0
