#!/bin/sh


#
# Check for the demo code, which we cannot install on top of.
rpm -q ibm-commserver | grep 99 > /dev/null 2> /dev/null
if [ $? == 0 ]
then
  echo This update cannot be installed on top of the 90-day demo download.
  exit 1
fi


#
# Make sure the base code is already installed.
# We can install 6.2.3.0 on top of 6.2.2.0, 6.2.2.1
# or 6.2.2.2 (which is just 6.2.2.1+).
rpm -q ibm-commserver-6.2.2.0-1 ibm-commserver-6.2.2.1-1 > /dev/null 2> /dev/null
if [ $? == 2 ]
then
  echo This update must be applied on top of ibm-commserver-6.2.2.x.
  exit 2
fi


#
# Check to make sure that have the update RPM available
if [ ! -f RPMS/ibm-commserver-6.2.3.0-1.`uname -m`.rpm ]
then
  echo "Error: Update RPM not available, did you download the right package?"
  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


############################################################
#
# Set up environment variables
# (Changes to this section come from the installibmcs and installgskit scripts)
#
LINUX_VENDOR=
LINUX_VERSION=
LINUX_ZH_CN_FONTSET=
JAVA_VERSION=IBMJava2-142
if [[ $MACH = ppc64 ]]
then
  JAVA_VERSION=IBMJava2-ppc-142
fi

#
# Establish Linux version. We currently support the following.
# - RedHat Enterprise Linux 3 (i686 only)
# - RedHat Enterprise Linux 4 (i686, ppc64, x86_64)
# - RedHat Enterprise Linux 5 (i686, x86_64, ppc64)
# - SuSE Linux Enterprise Server 8 (i686 only)
# - SuSE Linux Enterprise Server 9 (i686, ppc64, x86_64)
# - SUSE Linux Enterprise Server 10 (i686, ppc64, x86_64)
#
# Bypass RHEL3 NPTL rpm hang
fgrep "S release 3" /etc/redhat-release > /dev/null 2>&1
if [ $? = 0 ]
then
  export LD_ASSUME_KERNEL=2.4.1
fi
if [ "x$LINUX_VENDOR" = "x" ]
then
  rpm -q -a | grep -i redhat-release-3 >/dev/null 2>&1
  if [ $? = 0 ]
  then
    LINUX_VENDOR=redhat
    LINUX_VERSION=EL3
    LINUX_ZH_CN_FONTSET=redhat
    LAP_LD_ASSUME_KERNEL=2.4.19
  fi
fi
if [ "x$LINUX_VENDOR" = "x" ]
then
  RHEL4=`grep "Red Hat Enterprise Linux" /etc/redhat-release 2>/dev/null | grep "release 4 " | wc -l`
  if [ $RHEL4 != 0 ]
  then
    LINUX_VENDOR=redhat
    LINUX_VERSION=EL4
# For RHEL4 we use the same zh_CN fonts as for SLES
    LINUX_ZH_CN_FONTSET=suse
  fi
fi 
if [ "x$LINUX_VENDOR" = "x" ]
then
  rpm -q -a | grep -i suse-build-key-1.0-198 >/dev/null 2>&1
  if [ $? = 0 ]
  then
    LINUX_VENDOR=suse
    LINUX_VERSION=SLES8
    LINUX_ZH_CN_FONTSET=suse
  fi
fi
if [ "x$LINUX_VENDOR" = "x" ]
then
  rpm -q suse-build-key | grep 1.0-662 >/dev/null 2>&1
  if [ $? = 0 ]
  then
    LINUX_VENDOR=suse
    LINUX_VERSION=SLES9
    LINUX_ZH_CN_FONTSET=suse
  fi
fi
if [ "x$LINUX_VENDOR" = "x" ]
then
  SLES10=`grep "Enterprise Server 10" /etc/SuSE-release 2>/dev/null | wc -l`
  if [ $SLES10 != 0 ]
  then
    LINUX_VENDOR=suse
    LINUX_VERSION=SLES10
    LINUX_ZH_CN_FONTSET=suse
  fi
fi
if [ "x$LINUX_VENDOR" = "x" ]
then
  RHEL5=`grep "Red Hat Enterprise Linux Server release 5" /etc/redhat-release 2>/dev/null | wc -l`
  if [ $RHEL5 != 0 ]
  then 
    LINUX_VENDOR=redhat
    LINUX_VERSION=EL5
# For RHEL5 we use the same zh_CN fonts as for SLES                     ?
    LINUX_ZH_CN_FONTSET=suse
  fi
fi


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


#
# Save files which are about to be replaced
#
# Save copy of snastart someplace other than /etc/init.d
# This is done by the %post scriptlet of the 6.2.2.1 RPM


# 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


#
# Check for the latest LiS patch being applied
strings /lib/modules/`uname -r`/misc/streams.*o 2> /dev/null | egrep '2.19.0cs623' >/dev/null 2>&1
if [ $? != 0 ]
then
  echo "The correct LiS level and patch are not applied"
  exit 6
fi


#
# Remove the (empty) 6.2.2.2 ptf package if it is there
rpm -e ibm-commserver-ptf > /dev/null 2> /dev/null


#
# Apply the update
echo "Updating Communications Server for Linux v6.2.3.0"
rpm -Fhv RPMS/ibm-commserver-6.2.3.0-1.`uname -m`.rpm
RPMRC=$?


#
# Need to re-do what is un-done by the %postun scriptlet of the 6.2.2.0 RPM.
#
mkdir -p /etc/opt/ibm/sna/drivers 2> /dev/null
mkdir -p /etc/opt/ibm/sna/drivers/snapix0 2> /dev/null
#
ln -fs /usr/lib/libgsk7ssl.so /opt/ibm/sna/lib/libgskssl.so
ln -fs /usr/lib/libgsk7km.so  /opt/ibm/sna/lib/libgskkm.so
#
# Preprocess the DBCS XSnaadmin resource files to select the correct fonts
# depending on whether we are installing on RedHat or SuSE.
#
export LINUX_ZH_CN_FONTSET
export LINUX_VENDOR

cd /usr/X11R6/lib/X11/ko_KR/app-defaults
awk '{gsub (("!{" ENVIRON["LINUX_VENDOR"] "}"), "", $0); print $0}' XSnaadmin.gen >XSnaadmin

cd /usr/X11R6/lib/X11/ja_JP/app-defaults
awk '{gsub (("!{" ENVIRON["LINUX_VENDOR"] "}"), "", $0); print $0}' XSnaadmin.gen >XSnaadmin

cd /usr/X11R6/lib/X11/zh_TW/app-defaults
awk '{gsub (("!{" ENVIRON["LINUX_VENDOR"] "}"), "", $0); print $0}' XSnaadmin.gen >XSnaadmin

cd /usr/X11R6/lib/X11/zh_CN/app-defaults
awk '{gsub (("!{" ENVIRON["LINUX_ZH_CN_FONTSET"] "}"), "", $0); print $0}' XSnaadmin.gen >XSnaadmin

if [[ ( $LINUX_VENDOR = redhat ) && ( $LINUX_VERSION = EL5 ) ]]
then
  # X.Org 7.0 uses different directories for the resource files
  mkdir -p /usr/share/X11/app-defaults  2> /dev/null
  chmod ugo+rx /usr/share/X11 /usr/share/X11/app-defaults 2> /dev/null
  ln -s /usr/X11R6/lib/X11/app-defaults/XSnaadmin /usr/share/X11/app-defaults 2> /dev/null
  for LNG in de_DE en_US es_ES fr_FR ja_JP ko_KR pt_BR zh_CN zh_TW
  do
    mkdir -p /usr/share/X11/$LNG/app-defaults 2> /dev/null
    chmod ugo+rx /usr/share/X11/$LNG /usr/share/X11/$LNG/app-defaults 2> /dev/null
    ln -s /usr/X11R6/lib/X11/$LNG/app-defaults/XSnaadmin /usr/share/X11/$LNG/app-defaults 2> /dev/null
  done
fi


#
# Need to re-do some of what is done by the %post scriptlet of the 6.2.2.1 RPM
# Copy in the new snastart script (which was erased by the 6.2.2.0 %postun)
cp -p /etc/opt/ibm/sna/snastart-6221 /etc/init.d/snastart > /dev/null 2>&1
# Set up the soft-links to the start up scripts, if the O/S supports it
# we use install_initd (insserv), otherwise we set up the links manually
if [ -f /sbin/insserv ]
then
  /usr/lib/lsb/install_initd /etc/init.d/snastart 2> /dev/null
else
  ln -s /etc/init.d/snastart /etc/rc.d/rc0.d/K05snastart 2> /dev/null
  ln -s /etc/init.d/snastart /etc/rc.d/rc1.d/K05snastart 2> /dev/null
  ln -s /etc/init.d/snastart /etc/rc.d/rc2.d/S95snastart 2> /dev/null
  ln -s /etc/init.d/snastart /etc/rc.d/rc3.d/S95snastart 2> /dev/null
  ln -s /etc/init.d/snastart /etc/rc.d/rc4.d/S95snastart 2> /dev/null
  ln -s /etc/init.d/snastart /etc/rc.d/rc5.d/S95snastart 2> /dev/null
  ln -s /etc/init.d/snastart /etc/rc.d/rc6.d/K05snastart 2> /dev/null
fi


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


#
# We will do the 'sna start' so the new isolation layer is built.
cd /
/opt/ibm/sna/bin/sna start


exit $RPMRC
