#!/bin/bash
#
# nvconfig:	Install nVIDIA accelerated driver upon first boot
#
# chkconfig: 345 06 99
#
# description: 	nvconfig builds and installs the nvidia \
#		accelerated driver for the installed graphics card. 
#

export LANG=C

# Quirk for init/output behavior: 
# 'action '

# Determine Source function library (RH vs. SUSE)
if [ -f /etc/rc.status ] ; then 
	. /etc/rc.status
	rc_reset
	function PASS()
	{
		rc_status -v
	}
	function FAIL()
	{
		rc_failed
		rc_status -v
		exit -1
	}
else	# Assume RH distro
	. /etc/init.d/functions
	function PASS()
	{
		action "" /bin/true
	}
	function FAIL()
	{
		action "" /bin/false 
		exit -1
	}
fi

prog=nvconfig
RETVAL=0

current_kernel=$(uname -r)
mod_dir="/lib/modules/${current_kernel}/kernel/drivers/video"
homedir="/opt/hp/graphics/nvidia"
nv_log="/var/log/nvidia-installer.log"

case "$1" in 
 start)
	echo -n $"Starting $prog: "

	# If a nVIDIA card is installed:
	if [ $( lspci | grep -qi nvidia && echo 1 || echo 0 ) = 1  ] 
	then
	   # >=4G mem check:	   
	   tot=$(grep MemTotal </proc/meminfo  \
	         | sed -e's/MemTotal: *//' -e's/ kB//')
	   
	   if $(grep -qi intel /proc/cpuinfo); then
	      # Only do this for Intel EM64T systems
	      if [ $tot -ge 3899999 ]; then
	   
	         # Check for kernel param: 'swiotlb=#'
	         if [ $( grep -q swiotlb /var/log/dmesg && echo 1 || echo 0 ) = 0 ]; then
                    # Check to see if we need manually apply the 'swiotlb'
                    # kernel boot option.  If RHEL 3 && update 4 or older,
                    # yes - otherwise no If RHEL 4 base, then yes, otherwise no
                    release=`cut -d" "  -f7 /etc/redhat-release`
                    update=`cut -d" " -f10 /etc/redhat-release|cut -c1`

		    # If update is null, update=0
		    if [ -z "$update" ]; then
		       # Need to do this if it's the base release with no update
		       # string.  Compare later on blows up otherwise...
		       update="0"
		    fi
			
		    # We check for anything earlier then RHEL4_U1 and REHL_U5
		    # because it wasn't until then that RedHat made the kernel 
		    # swiotlb interface default to 16384 (64mb). 
		    execute_swiotlb=0
		    if  [[ "$release" = "4" ]]; then
		        if [[ "$update" < "1" ]]; then
		            execute_swiotlb=1
		        fi
		    elif [[ "$release" = "3" ]]; then
		        if [[ "$update" < "5" ]]; then
		            execute_swiotlb=1
		        fi
		    fi

                    if (( $execute_swiotlb )); then
		       echo $" "
		       echo $" "
		       echo $"   ERROR: This OEM NVIDIA driver will not function"
		       echo $"   properly on Intel systems with 4GB or more memory"
		       echo $"   without the kernel parameter 'swiotlb=16384'."
	               echo $" "
		       echo $"   ACTION: Please see 'nv_swiotlb.README' provided in"
		       echo $"   $homedir to address this issue."
		       echo $" "
		       echo $"   INCOMPLETE: Check /var/log/nvidia-installer.log"
			# write also to the log file...
	      
		       echo $" " > $nv_log
		       echo $"   ERROR: This OEM NVIDIA driver will not function" \
			      >> $nv_log
		       echo $"   properly on Intel systems with 4GB or more memory" \
			      >> $nv_log
		       echo $"   without the kernel parameter 'swiotlb=16384'." \
			      >> $nv_log
		       echo $" " \
			      >> $nv_log
		       echo $"   ACTION: Please see 'nv_swiotlb.README' provided in" \
			      >> $nv_log
		       echo $"   $homedir to address this issue." \
			      >> $nv_log
		       echo $" " >> $nv_log

	      	       action "" /bin/false
		       exit -1 	 
                    fi
	         fi  
	      fi
           fi
	   # If driver is not installed:
           if [ ! -f /usr/bin/nvidia-installer ]
	   then
	      ${homedir}/configure
	   else
	      # If driver installed but no kernel mod in place:
	      if [ ! -f ${mod_dir}/nvidia.o -a ! -f ${mod_dir}/nvidia.ko ]
	      then		
		
		 # Build additional kernel module:
		 echo $"Adding module..."
	         ${homedir}/driver/NVIDIA-Linux-x86_64-1.0*.run \
                        --silent --kernel-module-only --no-network > /dev/null

	         # NOTE: I'm assuming that with driver already installed,
		 # there is no need to update or muck with the XF86Config files.
		
		 # Check for errors: 
		 if grep -q "ERROR" /var/log/nvidia-installer.log;
		 then
		 	echo $"INCOMPLETE: Check /var/log/nvidia-installer.log"
			echo $"NOTE: X may not start or function properly."
			FAIL 
		 else
		 	echo "COMPLETE: Check /var/log/nvidia-installer.log for installation messages."
		 fi
	      fi
	   fi
	else
	   # If not a nvidia card, turn this script off:
	   /sbin/chkconfig --del $prog > /dev/null
	fi

	;;
	
esac
PASS
exit $RETVAL
