#!/bin/bash
# Based on http://wiki.debian.org/NvidiaGraphicsDrivers?action=AttachFile&do=view&target=nvidia-versions.sh
# Based on nvidia-detect, originall for debian by:
# Copyright © 2008-2011 Filipus Klutiero <chealer@gmail.com>
# Copyright © 2011-2016 Andreas Beckmann <anbe@debian.org>
#
# Changes made for MX Linux by Dolphin Oracle (dolphinoracle@mgail.com)
# Depends on mx version of nvidia-detect
#
# This package is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This package is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>
#

#translation support
source gettext.sh
export TEXTDOMAINDIR=/usr/share/locale 
export TEXTDOMAIN="ddm-mx"

LOG="/var/log/ddm.log"

if [ "$1" = "-h" -o "$1" = "--help" ]; then
	echo "Usage: nvidia-detect-mx [PCIID]..."
	echo "       "$(gettext "Reports the Debian packages supporting the NVIDIA GPU that is installed on the local system (or given as a PCIID parameter).")
	exit 0
fi

shopt -s compat31 nocasematch 2>/dev/null || { echo "Error: this script only works with bash." && exit; } # Avoid cryptic failure when running dash on this script

# last time the PCI IDs were updated

LATEST="470.141.03"
NVIDIA_DETECT_PRESENT="/usr/bin/nvidia-detect"
# -------------------------------------------------------------------------
#get release info
source /etc/lsb-release
echo 
echo "Distribution:  " $DISTRIB_ID
echo  

# -------------------------------------------------------------------------

ARCH=$(dpkg --print-architecture)

#check for existing nvidia-detect

if [ -e "$NVIDIA_DETECT_PRESENT" ]; then
    LATEST=$(cat /usr/bin/nvidia-detect|grep LATEST= |cut -d '"' -f2)
fi

echo $(gettext "Latest") " = $LATEST" 
PACKAGE=

NV_DETECT()
{

NVGA=$1
IDLISTDIR=/usr/share/ddm-mx

if [ -d "/usr/share/nvidia" ]; then
    IDLISTDIR=/usr/share/nvidia
fi
local VERSIONS

if grep -q -i $NVGA $IDLISTDIR/nvidia-legacy-71xx.ids 2>/dev/null
then
	VERSIONS[71]=71.86
fi

if grep -q -i $NVGA $IDLISTDIR/nvidia-legacy-96xx.ids 2>/dev/null
then
	VERSIONS[96]=96.43
fi

if grep -q -i $NVGA $IDLISTDIR/nvidia-legacy-173xx.ids 2>/dev/null
then
	VERSIONS[173]=173.14
fi

if grep -q -i $NVGA $IDLISTDIR/nvidia-legacy-304xx.ids 2>/dev/null
then
	VERSIONS[304]=304.123
fi

if grep -q -i $NVGA $IDLISTDIR/nvidia-legacy-340xx.ids 2>/dev/null
then
	VERSIONS[340]=340.76
fi

if grep -q -i $NVGA $IDLISTDIR/nvidia-legacy-390xx.ids 2>/dev/null
then
	VERSIONS[390]=390.87
fi

if [ "$ARCH" = "amd64" ]; then
    if grep -q -i $NVGA $IDLISTDIR/nvidia-legacy-390xx-amd64.ids 2>/dev/null
    then
        VERSIONS[391]=390.87
    fi
    
    if grep -q -i $NVGA $IDLISTDIR/nvidia-418.ids 2>/dev/null
	then
		VERSIONS[418]=418.74
	fi
	
	if grep -q -i $NVGA $IDLISTDIR/nvidia-tesla-418.ids 2>/dev/null
	then
		VERSIONS[419]=418.87.01
	fi

	if grep -q -i $NVGA $IDLISTDIR/nvidia-tesla-440.ids 2>/dev/null
	then
		VERSIONS[441]=440.64.00
	fi

	if grep -q -i $NVGA $IDLISTDIR/nvidia-tesla-450.ids 2>/dev/null
	then
		VERSIONS[451]=450.51.05
	fi

	if grep -q -i $NVGA $IDLISTDIR/nvidia-tesla-460.ids 2>/dev/null
	then
		VERSIONS[461]=460.32.03
	fi
	
	if grep -q -i $NVGA $IDLISTDIR/nvidia-470.ids 2>/dev/null
	then
	VERSIONS[470]=470.103.01
	fi

	if grep -q -i $NVGA $IDLISTDIR/nvidia-tesla-470.ids 2>/dev/null
	then
	VERSIONS[471]=470.57.02
	fi
	
	if grep -q -i $NVGA $IDLISTDIR/nvidia-tesla.ids 2>/dev/null
	then
	VERSIONS[510]=510.47.03
	fi
	
	if grep -q -i $NVGA $IDLISTDIR/nvidia.ids 2>/dev/null
	then
	# 999 means current
		VERSIONS[999]=$LATEST
	fi
	
fi

if [ "$ARCH" = "amd64" ]; then
	if grep -q -i $NVGA $IDLISTDIR/nvidia.ids 2>/dev/null
	then
	# 999 means current
	VERSIONS[999]=$LATEST
	fi
fi

if [[ ${#VERSIONS[*]} == 0 ]]; then
       	echo $(eval_gettext "Uh oh. Your card is not supported by any driver version up to $LATEST.") 
	echo $(eval_gettext "A newer driver may add support for your card.") 
	echo $(eval_gettext "Newer driver releases may be available in the MX testing repository or debian-backports.") 
	exit 3
fi

if [[ -n ${VERSIONS[340]} ]]; then
	PACKAGECHECK="nvidia-legacy-340xx-driver"
	if [ -n "$(apt-cache madison $PACKAGECHECK 2>/dev/null |grep $PACKAGECHECK)" ]; then
	    #inform which driver series works
	    PACKAGE=$PACKAGECHECK
		printf "$(eval_gettext "Your card is supported by the nvidia %s drivers series. %s")" "340"  "$PACKAGE" 
		echo "" 
	fi
fi
if [[ -n ${VERSIONS[390]} ]]; then
	PACKAGECHECK="nvidia-legacy-390xx-driver" #; changes for mx
	if [ -n "$(apt-cache madison $PACKAGECHECK 2>/dev/null |grep $PACKAGECHECK)" ]; then
		PACKAGE=$PACKAGECHECK
		printf "$(eval_gettext "Your card is supported by the nvidia %s drivers series. %s")" "390"  "$PACKAGE" 
		echo "" 
	fi
fi
if [[ -n ${VERSIONS[391]} ]]; then
	PACKAGECHECK="nvidia-legacy-390xx-driver" #; changes for mx	
	if [ -n "$(apt-cache madison $PACKAGECHECK 2>/dev/null |grep $PACKAGECHECK)" ]; then
		PACKAGE=$PACKAGECHECK
	    printf "$(eval_gettext "Your card is supported by the nvidia %s drivers series. %s")" "390"  "$PACKAGE" 
	    echo "" 
	fi
fi
if [[ -n ${VERSIONS[419]} ]]; then
	PACKAGECHECK="nvidia-tesla-418-driver"
	if [ -n "$(apt-cache madison $PACKAGECHECK 2>/dev/null |grep $PACKAGECHECK)" ]; then
		PACKAGE=$PACKAGECHECK
	    printf "$(eval_gettext "Your card is supported by the nvidia %s drivers series. %s")" "tesla 418"  "$PACKAGE" 
	    echo "" 
	fi
fi
if [[ -n ${VERSIONS[441]} ]]; then
	PACKAGECHECK="nvidia-tesla-440-driver"
	if [ -n "$(apt-cache madison $PACKAGECHECK 2>/dev/null |grep $PACKAGECHECK)" ]; then
		PACKAGE=$PACKAGECHECK
		printf "$(eval_gettext "Your card is supported by the nvidia %s drivers series. %s")" "tesla 440"  "$PACKAGE" 
		echo "" 
	fi
fi
if [[ -n ${VERSIONS[451]} ]]; then
	PACKAGECHECK="nvidia-tesla-450-driver"
	if [ -n "$(apt-cache madison $PACKAGECHECK 2>/dev/null |grep $PACKAGECHECK)" ]; then
		PACKAGE=$PACKAGECHECK
		printf "$(eval_gettext "Your card is supported by the nvidia %s drivers series. %s")" "tesla 450"  "$PACKAGE" 
		echo "" 
	fi
fi
if [[ -n ${VERSIONS[461]} ]]; then
	PACKAGECHECK="nvidia-tesla-460-driver1"
	if [ -n "$(apt-cache madison $PACKAGECHECK 2>/dev/null |grep $PACKAGECHECK)" ]; then
		PACKAGE=$PACKAGECHECK
		printf "$(eval_gettext "Your card is supported by the nvidia %s drivers series. %s")" "tesla 460"  "$PACKAGE" 
		echo "" 
	fi
fi
if [[ -n ${VERSIONS[471]} ]]; then
	PACKAGECHECK="nvidia-tesla-470-driver"
	if [ -n "$(apt-cache madison $PACKAGECHECK 2>/dev/null |grep $PACKAGECHECK)" ]; then
		PACKAGE=$PACKAGECHECK
	    printf "$(eval_gettext "Your card is supported by the nvidia %s drivers series. %s")" "tesla 470"  "$PACKAGE" 
	    echo "" 
	fi
fi
if [[ -n ${VERSIONS[510]} ]]; then
	PACKAGECHECK="nvidia-tesla-510-driver"
	if [ -n "$(apt-cache madison $PACKAGECHECK 2>/dev/null |grep $PACKAGECHECK)" ]; then
		PACKAGE=$PACKAGECHECK
		printf "$(eval_gettext "Your card is supported by the nvidia %s drivers series. %s")" "tesla 510"  "$PACKAGE" 
		echo "" 
	fi
fi
if [[ -n ${VERSIONS[999]} ]]; then
	PACKAGECHECK="nvidia-driver" #; changes for mx
	if [ -n "$(apt-cache madison $PACKAGECHECK 2>/dev/null |grep $PACKAGECHECK)" ]; then
		PACKAGE=$PACKAGECHECK
		printf "$(eval_gettext "Your card is supported by the nvidia %s drivers series. %s")" "$LATEST"  "$PACKAGE" 
		echo ""
	fi
fi

if [[ -z $PACKAGE ]]; then
	if [[ -n ${VERSIONS[304]} ]]; then
	#. inform which driver series are totally unsupported
	    printf "$(eval_gettext "Uh oh. Your card is only supported by the %s legacy drivers series, which is not in any current Debian suite.")" "304" 
	    echo "" 
	elif [[ -n ${VERSIONS[173]} ]]; then
		printf "$(eval_gettext "Uh oh. Your card is only supported by the %s legacy drivers series, which is not in any current Debian suite.")" "173" 
		echo "" 
	elif [[ -n ${VERSIONS[96]} ]]; then
		printf "$(eval_gettext "Uh oh. Your card is only supported by the %s legacy drivers series, which is not in any current Debian suite.")" "96" 
		echo "" 
	elif [[ -n ${VERSIONS[71]} ]]; then
		printf "$(eval_gettext "Uh oh. Your card is only supported by the %s legacy drivers series, which is not in any current Debian suite.")" "71" 
		echo "" 
	else
		echo $(eval_gettext "Oops. Supported driver not found") "($NVGA)" 
		echo ""
	fi
fi

echo ""

if [ -n "$PACKAGE" ]; then
	echo $(eval_gettext "It is recommended to install the following:") 
	echo "INSTALL: "    $PACKAGE  
	echo "" 
	echo "$(eval_gettext "To install alternate driver packages, use") sudo ddm-mx -i nvidia -f [driver package name]" 
	echo "" 
fi

}


if [ -z "$1" ]; then

	if ! (lspci --version) > /dev/null 2>&1; then
		echo "ERROR: The 'lspci' command was not found. Please install the 'pciutils' package." >&2
		exit 1
	fi

	NV_DEVICES=$(lspci -mn | awk '{ gsub("\"",""); if (($2 ~ "030[0-2]") && ($3 == "10de" || $3 == "12d2")) { print $1 } }')

	if [ -z "$NV_DEVICES" ]; then
		echo "No NVIDIA GPU detected."
		exit 0
	fi

	echo "Detected NVIDIA GPUs:"
	for d in $NV_DEVICES ; do
		lspci -nn -s $d
	done

	for d in $NV_DEVICES ; do
		echo -e "\nChecking card: $(lspci -s $d | awk -F: '{print $3}')"
		NV_DETECT "$(lspci -mn -s "$d" | awk '{ gsub("\"",""); print $3 $4 }')"
	done

else

	for id in "$@" ; do
		PCIID=$(echo "$id" | sed -rn 's/^(10de)?:?([0-9a-fA-F]{4})$/10de\2/ip')
		if [ -z "$PCIID" ]; then
			echo "Error parsing PCI ID '$id'."
			exit 1
		fi

		echo "Checking driver support for PCI ID [$(echo $PCIID | sed -r 's/(....)(....)/\1:\2/')]" 
		NV_DETECT "$PCIID"
	done

fi
