#!/bin/sh


#
# Make sure the base code is already installed.
rpm -q ibm-commserver-6.2.2.0-1 > /dev/null 2> /dev/null
IS6220=$?
rpm -q ibm-commserver-6.2.2.1-1 > /dev/null 2> /dev/null
IS6221=$?
if [[ ( $IS6220 != 0 ) && ( $IS6221 != 0 ) ]]
then
  echo This update must be applied on top of either
  echo ibm-commserver-6.2.2.0-1 or ibm-commserver-6.2.2.1-1
  exit 1
fi


#
# Check to make sure that have the update RPM available
if [ ! -f RPMS/ibm-commserver-6.2.2.1-1.`uname -m`.rpm ]
then
  echo "Error: Update RPM not available, did you download the right package?"
  exit 2
fi


#
# Check to make sure that have the update RPM available
if [ ! -f RPMS/ibm-commserver-ptf-6.2.2.2-1.noarch.rpm ]
then
  echo "Error: ibm-commserver-ptf RPM not available, are you in the right directory?"
  exit 3
fi


#
# Check to make sure that sna daemons are already stopped.
ACTIVE=`ps -ef | egrep "snadaemon|snaadmin" | grep -v grep`
if [ "x$ACTIVE" != "x" ]
then
  echo "Error: SNA must be stopped before running this script"
  exit 4
fi


#
# Check to make sure that we are running from the root userid
WHOAMI=`whoami`
if [ $WHOAMI != 'root' ]
then
  echo This update process must be run from the \'root\' userid.
  exit 5
fi


#
# Check for the LiS patch
LiS=`/usr/bin/strings /usr/src/LiS/head/linux/version.o | grep '23 Mar 07'`
LiSRC=$?
if [[ ${LiSRC} != 0 ]]
then
  echo Please apply the LiS patch first.
  exit 6
fi


#
# Update from 6.2.2.0 to 6.2.2.1 first
RC6221=0
if [[ $IS6220 = 0 ]]
then
  ./scripts/installibmcs_ptf6221.modified
  RC6221=$?
fi


#
# Unload the current drivers, if they are loaded
/opt/ibm/sna/bin/snaulmod 2> /dev/null
sleep 1
/opt/ibm/sna/bin/snarmdrv 2> /dev/null
sleep 1
/sbin/rmmod streams 2> /dev/null
sleep 1


#
# Save files which are about to be replaced
for I in i n q r s t y
do
  mv /opt/ibm/sna/drivers/snapix${I}/Driver.o \
     /opt/ibm/sna/drivers/snapix${I}/Driver.o.6221 2> /dev/null
done
if [[ ( `uname -m` = 's390' ) || ( `uname -m` = 's390x') ]]
then
  mv /opt/ibm/sna/drivers/snapixc/Driver.o \
     /opt/ibm/sna/drivers/snapixc/Driver.o.6221 2> /dev/null
fi


# Copy over the new Isolation Layer driver (before 'rpm -F')
rm -rf /etc/opt/ibm/sna/snapix0 2> /dev/null
rm /etc/opt/ibm/sna/isolationdrivers/Driver.o* 2> /dev/null
rm -rf /etc/opt/ibm/sna/drivers/snapix0/* 2> /dev/null
mkdir -p /etc/opt/ibm/sna/drivers
mkdir -p /etc/opt/ibm/sna/isolationdrivers
cp drivers/* /etc/opt/ibm/sna/isolationdrivers


#
# Apply the 6.2.2.2 ptf
rpm -ihv RPMS/ibm-commserver-ptf-6.2.2.2-1.noarch.rpm

#
# Copy over the new Driver.o files
for I in i n q r s t y
do
  cp 6222_Driver.o_files/`uname -m`/snapix${I}/Driver.o \
     /opt/ibm/sna/drivers/snapix${I}/Driver.o 2> /dev/null
  chown root.root /opt/ibm/sna/drivers/snapix${I}/Driver.o 2> /dev/null
  chmod 644 /opt/ibm/sna/drivers/snapix${I}/Driver.o 2> /dev/null
done
if [[ ( `uname -m` = 's390' ) || ( `uname -m` = 's390x') ]]
then
  cp 6222_Driver.o_files/`uname -m`/snapixc/Driver.o \
     /opt/ibm/sna/drivers/snapixc/Driver.o 2> /dev/null
  chown root.root /opt/ibm/sna/drivers/snapixc/Driver.o 2> /dev/null
  chmod 644 /opt/ibm/sna/drivers/snapixc/Driver.o 2> /dev/null
fi


# Unload the current drivers, if they are loaded
/opt/ibm/sna/bin/snaulmod 2> /dev/null
sleep 1
/opt/ibm/sna/bin/snarmdrv 2> /dev/null
sleep 1


#
# We will do the 'sna start' so the new isolation layer
# and 2.6 .ko files are built, but we will do it quietly.
sync;sync;sync
cd /
/opt/ibm/sna/bin/sna start > /dev/null 2> /dev/null


# Remind user that a reboot is needed to load the new kernel modules.
if [[ ${RC6221} = 0 ]]
then
  echo 'You should now reboot the Linux system.'
fi


exit $RC6221
