#!/bin/bash
#
#**************************************************************************
#   Copyright (C) 2008 Jan Mette                                          *
#   Copyright (C) 2013-2025 Anke Boersma                                  *
#                                                                         *
#   This script 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 program 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, write to the                         *
#   Free Software Foundation, Inc.,                                       *
#   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.          *
#**************************************************************************

# config is in /etc/kdeos-hwdetect.conf & /etc/nvidia-drv.conf

hwdetect_graphics()
{
	#get variables
	NONFREE=`get_nonfree`
	XDRIVER=`get_xdriver`

	[ -n "$XDRIVER" ] || XDRIVER="vesa"

	case "$XDRIVER" in

		vesa)

			#force vesa driver
			printhl "Forcing driver: vesa."
			# add a status file in /tmp
			touch /tmp/vesa
			#disable nonfree if any
			NONFREE="no"

		;;

		*)

			printhl "no vesa driver forced."

		;;
	esac

	[ -n "$NONFREE" ] || NONFREE="yes"

	case "$NONFREE" in

		yes)
			# check for vendors
			CARD_NVIDIA=$(lspci -n | sed -n "s/.* 03.*: 10de:\(....\).*/\1/p")
			CARD_ATI=$(lspci -n | sed -n "s/.* 0300: 1002:\(....\).*/\1/p")
			CARD_INTEL=$(lspci -n | sed -n "s/.* 0300: 8086:\(....\).*/\1/p")

			# do we have one?
			if [ "$CARD_NVIDIA" != "" ] && [ "$CARD_INTEL" == "" ]
			then
			
				# check if its a card supported by the latest driver
				if [ $(grep -i "$CARD_NVIDIA" ${HW_DB_PATH}/${NV_DB}) ] 
				then

					printhl "NVIDIA hardware detected"
					printhl "Installing driver: nvidia. This can take a few seconds."
			
					rmmod -f nouveau
					rmmod -f ttm
					rmmod -f drm_kms_helper
					rmmod -f drm
					pacman -Rdd --noconfirm xf86-video-nouveau &>/dev/null
					pacman -Ud --noconfirm ${HW_DRIVER_PATH}/nvidia-utils-2:59* &>/dev/null
					pacman -Ud --noconfirm ${HW_DRIVER_PATH}/nvidia-2:59* &>/dev/null
			
					# add a status file in /tmp
					touch /tmp/nvidia
					# add status file for calamares
					touch /var/log/nvidia
					# no wayland session for non-free
					sed -i 's|Session=plasma|Session=plasmax11|' /etc/sddm.conf
					sed -i 's|DisplayServer=wayland|DisplayServer=x11|' /etc/sddm.conf
					
                # or maybe a legacy card
				elif [ $(grep -i "$CARD_NVIDIA" ${HW_DB_PATH}/${NV390XX_DB}) ] 
				then
				
					printhl "NVIDIA hardware detected"
					printhl "Installing driver: nvidia-390xx. This can take a few seconds."
				
					rmmod -f nouveau
					rmmod -f ttm
					rmmod -f drm_kms_helper
					rmmod -f drm
					pacman -Rdd --noconfirm xf86-video-nouveau &>/dev/null
					pacman -Ud --noconfirm ${HW_DRIVER_PATH}/nvidia-390xx-utils-390* &>/dev/null
					pacman -Ud --noconfirm ${HW_DRIVER_PATH}/nvidia-390xx-390* &>/dev/null
				
					# add a status file in /tmp
					touch /tmp/nvidia-390xx
					# add status file for calamares
					touch /var/log/nvidia-390xx
					# no wayland session for non-free
					sed -i 's|Session=plasma|Session=plasmax11|' /etc/sddm.conf
					sed -i 's|DisplayServer=wayland|DisplayServer=x11|' /etc/sddm.conf
				
				# or a newer legacy card
				elif [ $(grep -i "$CARD_NVIDIA" ${HW_DB_PATH}/${NV470XX_DB}) ] 
				then
				
					printhl "NVIDIA hardware detected"
					printhl "Installing driver: nvidia-470xx. This can take a few seconds."
				
					rmmod -f nouveau
					rmmod -f ttm
					rmmod -f drm_kms_helper
					rmmod -f drm
					pacman -Rdd --noconfirm xf86-video-nouveau &>/dev/null
					pacman -Ud --noconfirm ${HW_DRIVER_PATH}/nvidia-470xx-utils-470* &>/dev/null
					pacman -Ud --noconfirm ${HW_DRIVER_PATH}/nvidia-470xx-470* &>/dev/null
				
					# add a status file in /tmp
					touch /tmp/nvidia-470xx
					# add status file for calamares
					touch /var/log/nvidia-470xx
					# no wayland session for non-free
					sed -i 's|Session=plasma|Session=plasmax11|' /etc/sddm.conf
					sed -i 's|DisplayServer=wayland|DisplayServer=x11|' /etc/sddm.conf

				else
					printhl "No non-free drivers available for this hardware"
				fi
			
			elif [ "$CARD_NVIDIA" != "" ] && [ "$CARD_INTEL" != "" ]
			then
			
				# check if its a card supported by the latest driver
				if [ $(grep -i "$CARD_NVIDIA" ${HW_DB_PATH}/${NV_DB}) ] 
				then

					printhl "NVIDIA hybrid hardware detected"
					printhl "Installing driver: nvidia. This can take a few seconds."
			
					rmmod -f nouveau
					rmmod -f ttm
					rmmod -f drm_kms_helper
					rmmod -f drm
					pacman -Rdd --noconfirm xf86-video-intel &>/dev/null
					pacman -Rdd --noconfirm xf86-video-nouveau &>/dev/null
					pacman -Ud --noconfirm ${HW_DRIVER_PATH}/nvidia-utils-2:59* &>/dev/null
					pacman -Ud --noconfirm ${HW_DRIVER_PATH}/nvidia-2:59* &>/dev/null
					pacman -Ud --noconfirm ${HW_DRIVER_PATH}/prime* &>/dev/null
			
					# add a status file in /tmp
					touch /tmp/nvidia-prime
					# add status file for calamares
					touch /var/log/nvidia-prime
					# no wayland session for non-free
					sed -i 's|Session=plasma|Session=plasmax11|' /etc/sddm.conf
					sed -i 's|DisplayServer=wayland|DisplayServer=x11|' /etc/sddm.conf
					
                else
					printhl "No non-free drivers available for this hardware"
				fi
			
			
			elif [ "$CARD_ATI" != "" ]
			then

   			         # check if its a card supported by the latest driver			
				if [ $(grep -i "$CARD_ATI" ${HW_DB_PATH}/${ATI_DB}) ]
				then
				
					printhl "ATI hardware detected"
					printhl "Installing driver: ATI catalyst. This can take a few seconds."
				
					pacman -Rdd --noconfirm libgl &>/dev/null
					pacman -Rdd --noconfirm xf86-video-ati &>/dev/null
					pacman -Ud --noconfirm ${HW_DRIVER_PATH}/catalyst-utils* &>/dev/null
					pacman -Ud --noconfirm ${HW_DRIVER_PATH}/catalyst-1* &>/dev/null
				
					# add a status file in /tmp
					touch /tmp/catalyst

				# or maybe a legacy card
				elif [ $(grep -i "$CARD_ATI" ${HW_DB_PATH}/${ATILEGACY_DB}) ]
				then
 
					printhl "ATI hardware detected"
					printhl "Installing driver: ATI catalyst-legacy. This can take a few seconds."
				
					pacman -Rdd --noconfirm libgl &>/dev/null
					pacman -Rdd --noconfirm xf86-video-ati &>/dev/null
					pacman -Ud --noconfirm ${HW_DRIVER_PATH}/catalyst-legacy-utils* &>/dev/null
					pacman -Ud --noconfirm ${HW_DRIVER_PATH}/catalyst-legacy-1* &>/dev/null
				
					# add a status file in /tmp
					touch /tmp/catalyst-legacy
				else

					printhl "No non-free drivers available for this hardware"
				fi
			else
			
					printhl "No non-free drivers available for this hardware"

			fi
		;;

		*)

					printhl "Non-free graphics drivers disabled"

		;;
	esac
}
