#
#  This script installs the CS Linux on zSeries Client
#

############################################################
#
# Do some basic sanity checks to ensure the script can run.
#

#  First check we are running from the subdirectory of the CD, else no
#  messages can be produced.  Set NLSPATH environment variable so that the
#  correct language variant of message catalog file is used.  Set C to be
#  the default.  The client installation script is 2 directories down from
#  the top level of the CD.
#

MACH=`uname -m`
if [ $MACH != s390 -a $MACH != s390x ]
then
  # Can't use readinstcat as it will be for the wrong machine type
  echo "Invalid machine type $MACH; expecting s390 or s390x"
  exit 5
fi

RUNFROM=`pwd`
NLSPATH=${RUNFROM}/install-message-catalog/%L/%N
export NLSPATH=${NLSPATH}:${RUNFROM}/install-message-catalog/C/%N

#
# Construct full path for snareadinstcat to protect against script changing
# directory.
export READINSTCAT=${RUNFROM}/install-message-catalog/snareadinstcat

if [ ! -f installibmcscli ]
then
  # echo "Error: must be run from home of installibmcscli"
  $READINSTCAT 14 2> /dev/null

  if [ $? != 0 ]
  then
    # Failed to write message from catalog, last resort is plain echo
    echo "Error: must be run from home of installibmcscli"
  fi
  exit 3
fi

#
# Allow a single parameter of license_accepted
#
if [ $# = 0 ]
then
  SILENT=0
elif [ $# = 1 ]
then
  if [ "$1" = "license_accepted" ]
  then
    # echo "License terms accepted, silent installation"
    $READINSTCAT 39
    SILENT=1
  else
    # echo "Invalid parameter"
    # echo "Usage $0 | $0 license_accepted"
    $READINSTCAT 37
    $READINSTCAT 51 $0 $0
    exit 1
  fi
else
  # echo "Too many parameters"
  # echo "Usage $0 | $0 license_accepted"
  $READINSTCAT 48
  $READINSTCAT 51 $0 $0
  exit 2
fi

#
#  Check that CS Linux daemon/appl is not running
#
ACTIVE=`ps -ef | egrep "snadaemon|snaadmin" | grep -v grep`
if [ "x$ACTIVE" != "x" ]
then
  # echo "Error: SNA must be stopped before running this script"
  $READINSTCAT 18
  exit 4
fi

############################################################
#
# Set up environment variables
#
LINUX_VENDOR=
LINUX_VERSION=
JAVA_VERSION=IBMJava2-s390-142

#
# Establish Linux version. We currently support the following.
# - RedHat Enterprise Linux 3 for S/390 and zSeries
# - RedHat Enterprise Linux 4 for S/390 and zSeries
# - SuSE Linux Enterprise Server 8 for IBM Mainframes and zSeries (SLES8)
# - SuSE Linux Enterprise Server 9 for IBM Mainframes and zSeries (SLES9)
#
# Bypass RHEL3 NPTL rpm hang
export LD_ASSUME_KERNEL=2.4.1
if [ "x$LINUX_VENDOR" = "x" ]
then
  rpm -q -a | grep -i redhat-release-3AS >/dev/null 2>&1
  if [ $? = 0 ]
  then
    LINUX_VENDOR=redhat
    LINUX_VERSION=EL3
    LAP_LD_ASSUME_KERNEL=2.4.19
  else
    export LD_ASSUME_KERNEL=
  fi
fi
if [ "x$LINUX_VENDOR" = "x" ]
then
  rpm -q -a | grep -i redhat-release-4 >/dev/null 2>&1
  if [ $? = 0 ]
  then
    LINUX_VENDOR=redhat
    LINUX_VERSION=EL4
    export LD_ASSUME_KERNEL=
  fi
fi
if [ "x$LINUX_VENDOR" = "x" ]
then
  rpm -q -a | grep -i suse-build-key-1.0-60 >/dev/null 2>&1
  if [ $? = 0 ]
  then
    LINUX_VENDOR=suse
    LINUX_VERSION=SLES8
  fi
fi
if [ "x$LINUX_VENDOR" = "x" ]
then
  rpm -q -a | grep -i suse-build-key-1.0-662.4 >/dev/null 2>&1
  if [ $? = 0 ]
  then
    LINUX_VENDOR=suse
    LINUX_VERSION=SLES9
    export LD_ASSUME_KERNEL=
  fi
fi
if [ "x$LINUX_VENDOR" = "x" ]
then
  rpm -q -a | grep -i UnitedLinux-build-key-1.0 >/dev/null 2>&1
  if [ $? = 0 ]
  then
    LINUX_VENDOR=ul
    LINUX_VERSION=1x0
  fi
fi


if [ "x$LINUX_VENDOR" = "x" ]
then
  # echo  "Warning: Installed O/S version is not supported"
  # echo  "Refer to the README for further information"
  $READINSTCAT 58
  $READINSTCAT 47
fi


############################################################
#
# Establish whether there is an existing version of CS Linux installed.
# Check for ibm-commserver* and CS-LINUX* (the PRPQ release).
#
for oldproduct in ibm-commserver CS-LINUX
do
  EXISTING_CSLINUX=`rpm -q -a | grep -i $oldproduct`
  if [ "x$EXISTING_CSLINUX" != "x" ]
  then
    $READINSTCAT 55
    echo "$EXISTING_CSLINUX"
    $READINSTCAT 47
    exit 13
  fi
done

############################################################
#
# Check pre-requisites for main package are present.
#

#
# No pre-requisites for the main package
#


############################################################
#
# Verify that the user is happy with the licensing terms
#

#
#  Generate License using LAP code
#  (LAP code requires libXt.so.6 library, exit if not present)
#
if [ ! -f /usr/X11R6/lib/libXt.so.6 ]
then
  if [[ ( $LINUX_VENDOR = redhat ) && ( $LINUX_VERSION = EL4 ) ]]
  then
    # echo  "Error: xorg-x11-libs RPM package needs to be installed"
    $READINSTCAT 59 xorg-x11-libs
  elif [ $LINUX_VENDOR = redhat ]
  then
    # echo  "Error: XFree86-libs RPM package needs to be installed"
    $READINSTCAT 59 XFree86-libs
  elif [[ ( $LINUX_VENDOR = suse ) && ( $LINUX_VERSION = SLES9 ) ]]
  then
    # echo  "Error: XFree86-libs RPM package needs to be installed"
    $READINSTCAT 59 XFree86-libs
  elif [ "x$LINUX_VENDOR" != "x" ]
  then
    # echo  "Error: xshared RPM package needs to be installed"
    $READINSTCAT 59 xshared
  else
    # echo  "Error: libXt RPM package needs to be installed"
    $READINSTCAT 59 libXt
  fi
  # echo  "Refer to the README for further information"
  $READINSTCAT 47
  exit 10
fi

# save/set LD_ASSUME_KERNEL if needed
if [ "x$LAP_LD_ASSUME_KERNEL" != "x" ]
then
  OLD_LD_ASSUME_KERNEL=$LD_ASSUME_KERNEL
  export LD_ASSUME_KERNEL=$LAP_LD_ASSUME_KERNEL
fi
cd LAP/LAP_Home
if [ $SILENT = 0 ]
then
  ./runLAP $JAVA_VERSION "-text_only"
else
  ./runLAP $JAVA_VERSION "-t 5"
fi
LAPRC=$?
cd $RUNFROM
# restore LD_ASSUME_KERNEL if it was set
if [ "x$LAP_LD_ASSUME_KERNEL" != "x" ]
then
  LD_ASSUME_KERNEL=$OLD_LD_ASSUME_KERNEL
fi

#
#  Exit if license not accepted.
#
if [ x$LAPRC != x9 ]
then
  # echo "Install abandoned as license terms not accepted"
  $READINSTCAT 27
  exit 11
fi


############################################################
#
# We can now install CS Linux.
#

# echo "Installing CS Linux Client..."
$READINSTCAT 33

#
#  Install the CS Linux rpm package
#
cd RPMS
rpm -Uhv ibm-commserver-cli-6*$MACH.rpm
if [ $? != 0 ]
then
  # echo "Possible error during install of ibm-commserver-cli package, will exit now."
  $READINSTCAT 43
  exit 12
fi
if [ ! -f /etc/opt/ibm/sna/sna_clnt.net ]
then
  cp /opt/ibm/sna/samples/sna_clnt.net /etc/opt/ibm/sna
fi

# echo "installibmcscli is done."
$READINSTCAT 31

exit 0
