#!/bin/sh
### BEGIN INIT INFO
# Provides: uxsp
# Required-Start: $syslog $network cron
# Required-Stop:
# Default-Start: 3 5
# Default-Stop: 0 1 6
# Description: 0 1 6
### END INIT INFO
##################################################
# UpdateXpress System Pack for Linux
##################################################
echo UpdateXpress Install Beginning
sgtk_dir=/opt/ibm/sgtk/log
log=$sgtk_dir/uxsp.inst.log
if [ ! -d  $sgtk_dir ]; then
 mkdir -p $sgtk_dir 
fi
echo UpdateXpress Install Beginning    >>$log
if [ ! -d  /mnt/uxsp ]; then
  mkdir -p /mnt/uxsp
fi
if [ ! -f  /tmp/tkvars ]; then
  echo "Not Found /tmp/tkvars"
  mv -f /etc/rc.d/*uxsp.sh /tmp/S98uxsp.sh.ran
  rm -f /etc/rc.d/rc3.d/*uxsp.sh
  rm -f /etc/rc.d/rc5.d/*uxsp.sh
  exit 1
fi
. /tmp/tkvars
# NOTE: The following line assumes that the Altiris share is configured with
# access enabled for samba
# Credentials for access are saved in /tmp/tkvars during deployment
umount /mnt/uxsp
mount -o username=$LOGON,password=$PASSWORD //$SERVERIP/$UXSPSHARE /mnt/uxsp
if [ $? -ne 0 ]; then
	echo "Mounting Failed" >>$log
	echo "echo mount -o username=$LOGON,password=$PASSWORD //$SERVERIP/$UXSPSHARE /mnt/uxsp" >>$log
	mv -f /etc/rc.d/*uxsp.sh /tmp/S98uxsp.sh.ran
	rm -f /etc/rc.d/rc3.d/*uxsp.sh
	rm -f /etc/rc.d/rc5.d/*uxsp.sh
	umount /mnt/uxsp
	exit 1
fi


#Determine OS Release
rel=rhel3
suse=/etc/SuSE-release
redhat=/etc/redhat-release
vmware=/etc/vmware-release
if [ -f $suse ]; then
  echo "Running on SuSE" >>$log
  rel=sles9
  cat $suse | grep -q "VERSION = 9"
  if [ $? -eq 0 ]; then
	rel=sles9
  fi
  cat $suse | grep -q "VERSION = 10"
  if [ $? -eq 0 ]; then
	rel=sles10
  fi
fi
if [ -f $redhat ]; then
  echo "Running on RedHat" >>$log
  rel=rhel4
  cat $redhat | grep -q "release 3"
  if [ $? -eq 0 ]; then
	rel=rhel3
  fi  
  cat $redhat | grep -q "release 4"
  if [ $? -eq 0 ]; then
	rel=rhel4
  fi
  cat $redhat | grep -q "release 5"
  if [ $? -eq 0 ]; then
	rel=rhel5
  fi
fi
if [ -f $vmware ]; then
  echo "Running on VMware" >>$log
  rel=rhel3
  vmware_flag="-f"
fi
echo "Searching for UpdateXpress System Pack Installer for OS=$rel" >>$log
cd /mnt/uxsp/$UXSPPATH
ls install*.$rel 1>>$log 2>/dev/null
if [ $? -eq 0 ]; then
  for uxspi in install*.$rel
  do
     echo "Searching for latest UpdateXpress System Pack Installer: $uxspi"   >>$log
  done
else
  echo "UpdateXpress Sytem Pack Installer install*.$rel not found" >>$log
  mv -f /etc/rc.d/*uxsp.sh /tmp/S98uxsp.sh.ran
  rm -f /etc/rc.d/rc3.d/*uxsp.sh
  rm -f /etc/rc.d/rc5.d/*uxsp.sh
  umount /mnt/uxsp
  exit 1
fi
echo "Found UpdateXpress System Pack Installer $uxspi " >>$log
echo "Executing ./$uxspi update -u " >>$log
#nic firmware update requires for all interfaces to be up
ifup eth0
ifup eth1
#lsi controller updates needs module mptctl for detection
modprobe mptctl
#add /usr/local/bin to remote environment so ABI breakage does not occur (rhel3)
export PATH=$PATH:/usr/local/bin
#executing uxspi
./$uxspi update -u $vmware_flag  
rc=$?
if [ $rc -eq 0 ]; then
  echo "UpdateXpress System Pack finished successfully" >>$log
else
  echo "UpdateXpress System Pack finished with Error code=$? " >>$log
fi
echo "Check /var/log/IBM_Support/uxsp.log for more information" >>$log
cd /

if [ -f  $sgtk_dir/second -o $rc -eq 0 ]; then
  mv -f /etc/rc.d/*uxsp.sh /tmp/S98uxsp.sh.ran
  rm -f /etc/rc.d/rc3.d/*uxsp.sh
  rm -f /etc/rc.d/rc5.d/*uxsp.sh
else
  touch $sgtk_dir/second
  reboot
fi
umount /mnt/uxsp



