#!/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"

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
	     # 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-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
