#!/bin/bash

version=`uname -r`
base_dir="/opt/hp/graphics/ati"
build_dir="/lib/modules/fglrx"
mod_dir="/lib/modules/${version}/kernel/drivers/char/drm"
hw=`uname -i`

# Initialize atimodconfig "smart" script to check for new kernels at boot
/sbin/chkconfig --add atimodconfig


# Verify kernel source is installed.
if [ -a /etc/redhat-release ];
   then
      if grep -i "release 4" /etc/redhat-release >/dev/null;
      then
         kernel_package="kernel-devel"
      else
         kernel_package="kernel-source"
      fi
else
   kernel_package="kernel-source"
fi

if rpm -q $kernel_package >/dev/null; then
   if test -d /lib/modules/`uname -r`/build; then
      # The necessary kernel build env exists so we can proceed with
      # installation...

      # Build a new kernel module
       echo "Checking for module..."
       if grep -i "release 3" /etc/redhat-release > /dev/null; then
         if [ ! -f ${mod_dir}/fglrx.o ]
         then
            echo "Cleaning build directory..."
            rm -rf ${build_dir}/fglrx.o
            rm -rf ${build_dir}/fglrx.${version}.o
            rm -rf ${build_dir}/build_mod/*.o
            cd ${build_dir}/build_mod/2.6.x
            make clean 2> /dev/null
            echo "Building module..."
            cd ${build_dir}/build_mod
            sh make.sh
            cd ${build_dir}
            sh make_install.sh
            echo "Module created..."
         else
            echo "Module found..."
         fi
         ls ${mod_dir}/fglrx.*
       fi
       if grep -i "release 4" /etc/redhat-release > /dev/null; then
         if [ ! -f ${mod_dir}/fglrx.ko ]
         then
            echo "Cleaning build directory..." 
            rm -rf ${build_dir}/fglrx.ko
            rm -rf ${build_dir}/fglrx.${version}.ko
            rm -rf ${build_dir}/build_mod/*.ko
            cd ${build_dir}/build_mod/2.6.x
            make clean 2> /dev/null
            echo "Building module..."
            cd ${build_dir}/build_mod
            sh make.sh
            cd ${build_dir}
            sh make_install.sh
            echo "Module created..."
         else
            echo "Module found..."
         fi
         ls ${mod_dir}/fglrx.*
       fi
   fi
fi

      # Create link to documentation
      cd ${base_dir}         
      rm -rf ati-docs
      echo "Creating ati-docs link..."
      ln -s /usr/share/doc/fglrx ati-docs

      #Install template X file. Required for aticonfig.
      if grep -i "release 4" /etc/redhat-release > /dev/null; then
         ## If config file is present, save it aside. 
         echo "RedHat EL4 detected..."
         if [ -L "/etc/X11/xorg.conf" ]; then
            rm -f /etc/X11/xorg.conf
         fi
         if [ -f "/etc/X11/xorg.conf" ]; then
            mv -f /etc/X11/xorg.conf /etc/X11/xorg.conf.save
         fi
         ##Copy template into place 
         cp -f ${base_dir}/example_configs/xorg.conf.atiStandard /etc/X11/
         cp -f ${base_dir}/example_configs/xorg.conf.atiStandard /etc/X11/xorg.conf
         ##Run ati script to intitialize. 
         ##Note: Dont confuse this with the HP atimodconfig script.
         /usr/X11R6/bin/aticonfig --initial -i /etc/X11/xorg.conf
      fi

      if grep -i "release 3" /etc/redhat-release > /dev/null; then
         echo "RedHat EL3 detected..."
         ##Not RHEL4, so use legacy name and format
         ## If XF86Config file is present, save it aside. 
         if [ -L "/etc/X11/XF86Config" ]; then
            rm -f /etc/X11/XF86Config
         fi
         if [ -L "/etc/X11/XF86Config-4" ]; then
            rm -f /etc/X11/XF86Config-4
         fi
         if [ -f "/etc/X11/XF86Config" ]; then
            mv -f /etc/X11/XF86Config /etc/X11/XF86Config.save
         fi
         if [ -f "/etc/X11/XF86Config-4" ]; then
            mv -f /etc/X11/XF86Config-4 /etc/X11/XF86Config-4.save
         fi
         ##Copy template into place 
         cp -f ${base_dir}/example_configs/XF86Config.atiStandard /etc/X11/
         cp -f ${base_dir}/example_configs/XF86Config.atiStandard /etc/X11/XF86Config
         ##Run ati script to intitialize
         ##Note: Dont confuse this with the HP atimodconfig script.
         /usr/X11R6/bin/aticonfig --initial -i /etc/X11/XF86Config
      fi
      modprobe fglrx

      echo "A standard X configuration file was installed under /etc/X11."
      echo "Run /usr/X11R6/bin/aticonfig --help for customization details."
      echo "A reboot is recommended."
      echo "** DONE **"
exit 0

