#!/usr/bin/env bash
# http://developer.download.nvidia.com/compute/DevZone/docs/html/C/doc/CUDA_C_Getting_Started_Linux.pdf

# https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#package-manager-metas

# http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1704/x86_64/

# Meta Package 	        	Purpose
# cuda 	                	Installs all CUDA Toolkit and Driver packages. Handles upgrading to the next version of the cuda package when it's released.
# cuda-9-1 	        	Installs all CUDA Toolkit and Driver packages. Remains at version 9.1 until an additional version of CUDA is installed.
# cuda-toolkit-9-1 		Installs all CUDA Toolkit packages required to develop CUDA applications. Does not include the driver.
# cuda-tools-9-1 	        Installs all CUDA command line and visual tools.
# cuda-runtime-9-1 		Installs all CUDA Toolkit packages required to run CUDA applications, as well as the Driver packages.
# cuda-compiler-9-1 		Installs all CUDA compiler packages.
# cuda-libraries-9-1 		Installs all runtime CUDA Library packages.
# cuda-libraries-dev-9-1 	Installs all development CUDA Library packages.
# cuda-drivers 	        	Installs all Driver packages. Handles upgrading to the next version of the Driver packages when they're released.

#curl http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1704/x86_64/cuda-repo-ubuntu1704_9.1.85-1_amd64.deb | dpkg -i -

# nvidia-detect
#Detected NVIDIA GPUs:
#01:00.0 VGA compatible controller [0300]: NVIDIA Corporation G86GLM [Quadro FX 360M] [10de:042d] (rev a1)
#
#Checking card:  NVIDIA Corporation G86GLM [Quadro FX 360M] (rev a1)
#Your card is only supported by the 340 legacy drivers series, which is only available up to buster.

# debian
## taken from the nvidia site:
# legacy-304.137 supports # Quadro FX 360M 	0x042D 	A
# legacy-340.108 supports # Quadro FX 360M 	0x042D 	A
# legacy-390.12  supports # Quadro FX 360M 	042D 	A

## So we should be able to install the latest legacy directly.

apt install -y \
	nvidia-kernel-common \
	nvidia-modprobe \
	nvidia-opencl-common \
	vdpauinfo \
	nvidia-detect \
	nvidia-cuda-toolkit \
	nvidia-cuda-mps \
	nvidia-visual-profiler \
	nvidia-persistenced \
	nvidia-legacy-390xx-smi \
	nvidia-legacy-390xx-opencl-icd \
	nvidia-legacy-390xx-driver \
	nvidia-legacy-390xx-kernel-support \
	nvidia-legacy-390xx-kernel-dkms \
	nvidia-legacy-390xx-vdpau-driver \
	xserver-xorg-video-nvidia-legacy-390xx
#	nvidia-cuda-dev \
#	libnvidia-legacy-390xx-compile \
#	libnvidia-legacy-390xx-cuda1 \
#	libnvidia-legacy-390xx-fatbinaryloader \
#	libnvidia-legacy-390xx-nvcuvid1 \
#	libnvidia-legacy-390xx-ptxjitcompiler1 \

#/var/tmp/automate/build_deb_from_dsc.sh nvidia_build http://deb.debian.org/debian/pool/non-free/n/nvidia-graphics-drivers-legacy-340xx/nvidia-graphics-drivers-legacy-340xx_340.108-11.dsc
#Suggested packages:
#  nvidia-driver | nvidia-driver-any nvidia-kernel-dkms | nvidia-kernel-source nvidia-driver | nvidia-tesla-470-driver | nvidia-tesla-460-driver
#  opencl-clhpp-headers-doc
#Recommended packages:
#  nvidia-kernel-dkms | nvidia-kernel-460.91.03 nvidia-smi libnvidia-cfg1 nvidia-persistenced libcuda1:i386 libcupti-doc libvdpau-dev libnvcuvid1
#  nvidia-cuda-toolkit-doc nvidia-cuda-gdb nsight-compute nsight-systems nvidia-opencl-icd libgl1-nvidia-glvnd-glx nvidia-vdpau-driver

#bash /var/tmp/automate/build_deb_from_dsc.sh /var/tmp/nvidia-legacy-340xx http://deb.debian.org/debian/pool/non-free/n/nvidia-graphics-drivers-legacy-340xx/nvidia-graphics-drivers-legacy-340xx_340.108-11.dsc

#cat > /etc/X11/xorg.conf << 'EOF'
#
#Section "Device"
#	Identifier "My GPU"
#	Driver "nvidia"
#EndSection
#EOF
#
#mkdir -p  /etc/X11/xorg.conf.d/
#cat > /etc/X11/xorg.conf.d/20-nvidia.conf <<'EOF'
#
#Section "Device"
#	Identifier "My GPU"
#	Driver "nvidia"
#EndSection
#EOF

#cat > /etc/modprobe.d/blacklist-nouveau.conf << 'EOF'
#blacklist nouveau
#options nouveau modeset=0
#EOF

# To enable FastWrites and Sidebus addressing, uncomment these lines
OPTION1="# options nvidia NVreg_EnableAGPSBA=1"
OPTION2="# options nvidia NVreg_EnableAGPFW=1"
CONFIG_FILE=/etc/modprobe.d/nvidia-kernel-common.conf
#sed -i "/${OPTION1}/s/^#.//g" ${CONFIG_FILE}
#sed -i "/${OPTION2}/s/^#.//g" ${CONFIG_FILE}

#sed -i 's,^\(GRUB_CMDLINE_LINUX=\).*,\1'\"nomodeset\ modeset.nouveau=0\"',' /etc/default/grub

#update-grub2

#cat > /etc/grub.d/40_custom << EOF
#menuentry 'BL - NVIDIA' {
#	set root=(hdX,Y)
#	linux	/ROOT/devuan-1@/boot/vmlinuz-4.19.0-8-amd64 root=ZFS=rpool/ROOT/devuan-1 ro  quiet resume=ZFS=rpool/ROOT/devuan-1/swap root=ZFS=rpool/ROOT/devuan-1 cgroup_enable=memory net.ifnames=0 i915.modeset=1 radeon.modeset=1 nvidia_drm.modeset=1 noplymouth nosplash  nomodeset nouveau.modeset=0
#	initrd /initrd.img
#}
#EOF
