#!/bin/bash
#
# atimodconfig:	Install FireGL accelerated driver upon first boot
#
# chkconfig: 012345 06 99
#
# description: 	atimodconfig builds and installs the ATI FireGL \
#		accelerated driver for the installed graphics card. 
#
export LANG=C

# 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="atimodconfig"
RETVAL=0

current_kernel=$(uname -r)
mod_dir="/lib/modules/${current_kernel}/kernel/drivers/char/drm"
homedir="/opt/hp/graphics/ati"

case "$1" in 
 start)
        echo -n $"Starting $prog: "
        runlevel=$(set -- $(runlevel); eval "echo \$$#")

        # If this is the first time this script has run, remove old
        # incompatible modules so fresh ones will be built. 
        if [ -f ${homedir}/notconfig ]   
        then
          rm -rf /lib/modules/*/kernel/drivers/char/drm/fglrx*.*o 2> /dev/null
          rm -rf ${homedir}/notconfig   
        fi  

	# If a FireGL card is installed, configure it...
	if [ $( lspci | grep -qi ATI && echo 1 || echo 0 ) = 1  ]
	then
           if grep -i "release 4" /etc/redhat-release > /dev/null; then
              if [ ! -f ${mod_dir}/fglrx.ko ]
              then
                   # Module is not in place for current kernel 
	   	echo $"Running /opt/hp/graphics/ati/configure for release 4."
	   	/opt/hp/graphics/ati/configure
              fi
              if [ ! -f ${mod_dir}/fglrx.ko ]
              then
                   #Kernel build or install problems
                   echo $"${mod_dir}/fglrx.ko not found." 
                   echo $"Update to the latest driver from ati.com to get"
                   echo $"accelerated 3D graphics performance."  
                   FAIL
              fi
           fi
           if grep -i "release 3" /etc/redhat-release > /dev/null; then
              if [ ! -f ${mod_dir}/fglrx.o ]
              then
                   # Module is not in place for current kernel
	   	echo $"Running /opt/hp/graphics/ati/configure for release 3."
                /opt/hp/graphics/ati/configure
              fi
              if [ ! -f ${mod_dir}/fglrx.o ]
              then
                   #Kernel build or install problems
                   echo $"${mod_dir}/fglrx.o not found."
                   echo $"Update to the latest driver from ati.com to get"
                   echo $"accelerated 3D graphics performance."
                   FAIL
              fi
           fi
        else 
        # If no ATI card, turn this script off:
        /sbin/chkconfig --del $prog > /dev/null
        fi

        ;;

esac
PASS
exit $RETVAL
