#!/bin/bash
# This script configures XGL on a few types of machines on which XGL is supported.

## First we check whether the machine being installed is one out of the supported list
# List of machines that XGL works on. IBM type numbers, and dell fammily names. 
#The IBM type numbers must be postfixed with a _ for the script to work. 
#(bonus points if you can tell me why, double bonus poitns if you can fix it so it's not needed)


xgl_machines=( "23868UW" "0C4708_" "2669VLM_" "2669VDH_" "0FH884_" "17068GG_" "1707W9L_" "2373P1G_" "2373GGU_" "2373GEU_" "2373KXU_" "200883G_" "Latitude_D610" "Latitude_D620" "Latitude_D400" "Latitude_D820" "Latitude_D810" "8742C4U_" "0FT292_")

# Next check what we have compared to the list

#SYSTEMCHECK=`hwinfo --bios | grep Product:|grep -v Latitude|awk '{ print $2 }'|uniq|tr -d /\"/ `

for SYSTEMCHECK in $(hwinfo --bios | grep Product: | awk '{print $2 "_" $3} '|tr -d /\"/) ; do
echo "Trying $SYSTEMCHECK"
  	for XGL in ${xgl_machines[@]}; do
#echo $XGL
		if [ "$SYSTEMCHECK" = "$XGL" ] ; then
			doXGL=1
                        SYSTEMCHECK=$XGL
#echo "Found XGL."
			#break out of the nested looks
			break 2
		fi
	done
done
echo $doXGL $SYSTEMCHECK

if [ "$doXGL" != "1" ] ; then 
echo "not doing xgl"
	exit 0
fi

## Next we need to check what type of video card is in the machine. Depending on the video-card, we know what RPMS to install
# First the kernel type is checked

kversion=`uname -r | cut -d "-" -f 3`

# Next the video card is checked, if XGL is supported, the doXGL variable is set to 1.
# We also define what RPMS are needed

if [ `hwinfo --gfx | grep -c nVidia` -ne 0 ] ; then 
echo nvidia
  # System has a nVidia card
  rpms="nvidia-gfx-kmp-$kversion x11-video-nvidia"
  saxmod="nvidia"
  doXGL=1
elif [ `hwinfo --gfx | grep -c ATI` -ne 0 ]; then
echo ati
  # System has an ATI card
  # For ATI card we also set the variable disable_sleep to 1, so sleep mode will be disabled later
  rpms="ati-fglrx-kmp-$kversion x11-video-fglrx"
  saxmod="fglrx"
  doXGL=1
  disable_sleep=1
elif [ `hwinfo --gfx | grep -c Intel` -ne 0 ]; then
echo intel
  # System has an Intel card
  # With Intel system, we need a videobios to get the special resolutions. Also we have a pre-configured xorg configuration we use.
  # After the Intel configuration we exit thsi script

  #Remove the trailing _ from the config files
  SYSTEMCHECK=$(echo $SYSTEMCHECK | sed -e 's/_$//' )
  curl -f http://autoyast/autoconf-sled10/xorg.conf.$SYSTEMCHECK > /tmp/xorg.conf
	if [ $? -eq 0 ] ; then 
		cp /tmp/xorg.conf /etc/X11/xorg.conf
	fi
  curl -f /tmp http://autoyast/autoconf-sled10/videobios.$SYSTEMCHECK > /tmp/videobios
  	if [ $? -eq 0 ] ; then 
		cp /tmp/videobios /etc/sysconfig/videobios
  	fi
  /opt/gnome/bin/gnome-xgl-switch --enable-xgl
  echo -e "configuring X\n" >/dev/tty1
  echo -e  "quitting xglconf.sh now\n" >/dev/tty1
  exit
else 
  echo "No matches"
  exit 0
fi  

## We continu to do the Xgl install for ATI or Nvidia

# Stop the X server, so noone can login
init 3

# Installing the RPMS if doXGL is set (if the machines have a supported video card)
if [ "$doXGL" = "1" ]; then
	echo -e "Installing Xgl drivers"
	expect - << EOF 

set timeout 600 
spawn rug in $rpms
expect " Software End User License Agreement"
send "q"
expect "Do you accept the license?"
send "y\r"
expect " Software End User License Agreement"
send "q"
expect "Do you accept the license?"
send "y\r"
expect "Transaction Finished"
EOF

  echo -e "\nReturn from expect was $!\n"
  echo -e "video driver installed.\nConfiguring X with sax2"
  sax2 -a -r -m 0=$saxmod
  /opt/gnome/bin/gnome-xgl-switch --enable-xgl

  sleep 20;
fi


# Disable sleep in powersave, because it doesn't work with fglrx

if [ "$disable_sleep" = "1" ]; then
  sed -i -e 's/DISABLE_USER_SUSPEND2RAM=".*"/DISABLE_USER_SUSPEND2RAM="YES"/ ' /etc/sysconfig/powersave/sleep
fi


# Finish

echo -e "Quitting xglconf.sh now and end of script\n" >/dev/tty1
