#!/bin/sh

base_dir="/opt/hp/graphics/nvidia"
version=`uname -r`
hw=`uname -i`

# Since I removed the dependency check for kernel source from the rpm package
# itself, I need to ensure the kernel source is installed.  This little bit
# of logic checks for the variety of kernel source packages that currently
# exist between RH and Novell...(4/12/05 - JWB)

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

   # The necessary kernel source package exists so we can proceed with
   # installation...

   # Install driver, silently (no ftp, and install over itself if already there)
   echo "Configuring NVIDIA OEM driver..."
   bash ${base_dir}/driver/NVIDIA-Linux-x86-1.0-*.run --silent --no-network > /dev/null

   # Check for errors: 
   if grep -q "ERROR" /var/log/nvidia-installer.log; 
   then
	echo "INCOMPLETE: See /var/log/nvidia-installer.log"
	echo "Please check your installation for completeness."
	echo "The error just encountered may have left your"
	echo "system incompletely configured."
	exit 0
   fi

   # Make sure 'nvconfig' is active
   /sbin/chkconfig --add nvconfig > /dev/null

   if rpm -q XFree86 >/dev/null;
   then

       # Make sure /etc/X11/X soft link is present 
       ln -s -f /usr/X11R6/bin/XFree86 /etc/X11/X

       # Check for presence of /etc/X11/XF86Config-4
       ## If present, save it aside. 
       if [ -L "/etc/X11/XF86Config-4" ]; then
            rm -f /etc/X11/XF86Config-4
       fi
     
       if [ -L "/etc/X11/XF86Config" ]; then
            rm -f /etc/X11/XF86Config
       fi
 
       if [ -f "/etc/X11/XF86Config-4" ]; then
            mv -f /etc/X11/XF86Config-4 /etc/X11/XF86Config-4.save
       fi
 
       if [ -f "/etc/X11/XF86Config" ]; then
            mv -f /etc/X11/XF86Config /etc/X11/XF86Config.save
       fi

       # Copy standard config in place and create symlink
       cp ${base_dir}/example_configs/XF86Config-4.nvidiaStandard /etc/X11/
       ln -s /etc/X11/XF86Config-4.nvidiaStandard /etc/X11/XF86Config-4
       ln -s /etc/X11/XF86Config-4.nvidiaStandard /etc/X11/XF86Config
    else
       # Xorg install

       # Make sure /etc/X11/X soft link is present 
       if [ ! -L "/etc/X11/X" ]; then
          ln -s -f /usr/X11R6/bin/Xorg /etc/X11/X
       fi

       # Check for presence of the Xorg configuration file 
       ## If present, save it aside. 

       # If it's a link, remove the link first
       if [ -L "/etc/X11/xorg.conf" ]; then
          rm -f /etc/X11/xorg.conf
       fi
     
       # If the xorg.conf files exists, save it aside
       if [ -f "/etc/X11/xorg.conf" ]; then
          mv -f /etc/X11/xorg.conf /etc/X11/xorg.conf.save
       fi
 
       # Copy standard config in place and create symlink
       cp ${base_dir}/example_configs/xorg.nvidiaStandard /etc/X11/

       ln -s /etc/X11/xorg.nvidiaStandard /etc/X11/xorg.conf
    fi
       
    # Make soft link to nvidia-settings program
    ln -s -f /usr/bin/nvidia-settings ${base_dir}/nvidia-settings

    # Make soft link to nvidia-xconfig program
    ln -s -f /usr/bin/nvidia-xconfig ${base_dir}/nvidia-xconfig

    echo ""
    echo "COMPLETE: Check /var/log/nvidia-installer.log for installation messages." 
    echo "NOTE: A basic nVIDIA X configuration file has been configured in /etc/X11/."
else
    echo ""
    echo "This system does not have the required kernel source package installed."
    echo ""
    echo "Please install the appropriate "$kernel_package" rpm for "$version" & "$hw" from"
    echo "your distribution before attempting to install this graphics driver again." 
    echo ""
fi

exit 0
