#!/bin/bash
#
#**************************************************************************
#   Copyright (C) 2008 Jan Mette                                          *
#   Copyright (C) 2009 Jan Mette and Phil Miller                          *
#   <jan[dot]mette[at]berlin[dot]de>                                      *
#   <philm[at]chakra-project[dot]org>                                     *
#                                                                         *
#   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.          *
#**************************************************************************


# param 1: modules to load
# param 2: blacklisted modules
set_modules()
{
	for module in $2
	do
		# Unload module if X is not running
		if [ ! "$(pgrep X)" ];	then
			rmmod -f ${module}
		fi
	done

	for module in $1
	do
		# Load module if X is not running
		if [ ! "$(pgrep X)" ];	then
			modprobe ${module}
		fi
	done
}


# config is in /etc/chakra-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

	# check how many graphic cards exist; TODO: this is not
	# robust enough; it will also detect multiple Nvidia/AMD cards, which
	# gamers might use 
	# it is needed to disable proprietary drivers when having
	# hybrid graphics; this is a WORKROUND and should be removed in further
	# releases

	CARD_NUMBER=$(lspci -v | grep VGA | wc -l) # number of graphic cards
	[ $CARD_NUMBER -gt 1 ] && NONFREE="no"


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

	case "$NONFREE" in

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

			# do we have one?
			if [ "$CARD_NVIDIA" != "" ]
			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."
			
					pacman -Rdd --noconfirm xf86-video-nouveau &>/dev/null
					pacman -Udd --force --noconfirm ${HW_DRIVER_PATH}/nvidia-4*.pkg.tar.xz &>/dev/null
			
                                        set_modules "nvidia" "nouveau ttm drm_kms_helper drm"
					# add a status file in /tmp
					touch /tmp/nvidia

                                # 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."
				
					pacman -Rdd --noconfirm xf86-video-nouveau &>/dev/null
					pacman -Udd --force --noconfirm ${HW_DRIVER_PATH}/nvidia-390xx* &>/dev/null
				
					set_modules "nvidia" "nouveau ttm drm_kms_helper drm"
					# add a status file in /tmp
					touch /tmp/nvidia-390xx

				# or maybe a legacy card
				elif [ $(grep -i "$CARD_NVIDIA" ${HW_DB_PATH}/${NV340XX_DB}) ]
				then
				
					printhl "NVIDIA hardware detected"
					printhl "Installing driver: nvidia-340xx. This can take a few seconds."
				
					pacman -Rdd --noconfirm xf86-video-nouveau &>/dev/null
					pacman -Udd --force --noconfirm ${HW_DRIVER_PATH}/nvidia-340xx* &>/dev/null
				
					set_modules "nvidia" "nouveau ttm drm_kms_helper drm"
					# add a status file in /tmp
					touch /tmp/nvidia-340xx
				else
					printhl "No non-free drivers available for this hardware"
				fi


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

			fi
		;;

		no)
                        PRODUCT_NAME=$(dmidecode -s system-product-name)

                        if [ "$PRODUCT_NAME" == "VirtualBox" ]
                        then
                                printhl "VirtualBox detected"
                                printhl "Installing video driver: VirtualBox. This can take a few seconds."

                                pacman -Udd --force --noconfirm ${HW_DRIVER_PATH}/virtualbox-guest-*

                                # starts the vbox service and load all the required modules
                                systemctl start vboxservice.service

                                # add a status file in /tmp
                                touch /tmp/virtualbox
                        else

                                printhl "Non-free graphics drivers disabled"

                        fi
                ;;

		*)

				printhl "Non-free graphics drivers disabled"

		;;
	esac
}
