#!/bin/bash

LogFile=/tmp/HmcInstall.log

#-------------------------------------------------------------------------------
# Common exit point
#-------------------------------------------------------------------------------
function ExitCleanup {
    # keep the log file, but ensure a different user can overwrite it next time
    cd /
    chmod 666 $LogFile

    if [ $1 -ne 0 ]; then
        exit $1
    else
        exit 0
    fi
}
#-------------------------------------------------------------------------------
# This function Updates if already installed, else it installs
#-------------------------------------------------------------------------------
function RsctInstall {
    rpm -q $1 1>&2 2>/dev/null
    if [ $? -eq 0 ]; then
        InstallRpm -Uvh $2
    else
        InstallRpm -ivh $2
    fi
}

#-------------------------------------------------------------------------------
# This function does the RPM installation task.
#-------------------------------------------------------------------------------
function InstallRpm {
    if [ -f /opt/hsc/data/config/NO_UPDATE_RPMS ]; then
        x="$2"
        # Strip version then leading directory name
        f=`echo ${x%%-[0-9]*}`
        r=`echo ${f##*/}`
        for i in `cat /opt/hsc/data/config/NO_UPDATE_RPMS`; do
            if [ "$r" == "$i" ]; then
                return
            fi
        done
    fi

    CMD=`echo $1`; shift;
    RPM=`echo $1`; shift;
    OPT=`echo $*`
    OPT="$OPT --force --nodeps"

    # Log the test install output. The format for the "normal" RPM install
    # processing inside this script is 'rpm -i <file spec> --force --nodeps'
    if [ -f $RPM ]; then
        echo "=====================================================" >> $LogFile
        echo "***** Executing rpm -vv $CMD $RPM $OPT *****         " >> $LogFile
        echo "=====================================================" >> $LogFile
        rpm -vv $CMD $RPM $OPT >> $LogFile 2>&1
        if [ $? -ne 0 ]; then
            echo "Error installing rpm fileset named $2" >> $LogFile
            if [ "$Update" == "true" ]; then
                echo "Error installing rpm fileset named $2"
                ExitCleanup 9
            fi
        fi
    fi
}

#-------------------------------------------------------------------------------
# Start the product install...
#-------------------------------------------------------------------------------
cd /
image=$1

PATH=$PATH:/opt/IBMJava/jre/bin:
LD_LIBRARY_PATH=/opt/hsc/lib:/opt/hsc/lib/hcmjni:/lib:/usr/lib:$LD_LIBRARY_PATH
export PATH LD_LIBRARY_PATH

if [ "$image" == "" ]; then
    echo "Please specify directory containing installable packages"
    echo "usage: installImages  <directory>"
    ExitCleanup 1
fi

# Check if directory exists
if [ ! -d $image ]; then
    echo "The directory $image doesn't exist"
    echo "Please specify directory containing the installable packages."
    ExitCleanup 2
fi

# requirement for installing Update images
# Only allow update to install if R3.3.x or R3.4.x is on the HMC
CR=`/opt/hsc/bin/hsc version | grep "Release:" | cut -d':' -f2 | cut -d'.' -f2`
if [[ "$CR" != 4 ]]; then
   echo "Installation of this update image requires HMC Version 7 Release 3.4.0"
   echo "Unable to install this update image."
   ExitCleanup 6
fi

# check and install ptf-req rpm
if [ -f $image/IBMhmc.MH0*rpm ]; then
    cd $image
    PTF=`ls IBMhmc.MH0*rpm | cut -d '.' -f 2 | cut -d '_' -f 1`
    PTF_d=`rpm -qpl IBMhmc.MH0*rpm | grep MH0`
    if [ -f /opt/hsc/data/ptf/${PTF}_obs ]; then
        echo "--- Install error: This fix is now obsolete.  "
        ExitCleanup 3
    else
        echo "--- Installing ptf-req ...."
        rpm --force -Uvh IBMhmc.${PTF}*rpm >> $LogFile 2>&1
        if [ $? -ne 0 ]; then
            echo "--- Install error: failed dependencies"
            ExitCleanup 3
#        else
            # remove ptf-req if it's 1st part(s) of an Update "_z1", "_d1", or "_d2"
#            PTFREQ=`rpm -qpR IBMhmc.${PTF}*.rpm | grep ${PTF} | cut -d '=' -f 1`
#            PTFupd=`echo ${PTFREQ} | cut -d '_' -f 2`
#            if [[ "$PTFupd" == "z1" || "$PTFupd" == "d1" || "$PTFupd" == "d2" ]]; then
#                rpm -e --nodeps ${PTFREQ}
#            fi
        fi
    fi
fi

if [ -f $image/hmcConfig ]; then
   cp -p /etc/init.d/hmcConfig /tmp/hmcConfig.tmp
   cp -f $image/hmcConfig /tmp/hmcConfig
   mv /tmp/hmcConfig /etc/init.d/hmcConfig
   chown root.root /etc/init.d/hmcConfig
   chmod 555 /etc/init.d/hmcConfig
fi

# baseOS update
if [ -d $image/baseOS ]; then
   cd $image/baseOS
   echo "--- Updating base ...."
#   for UPD in `cat index`
#   do
#      echo "***** Updating $UPD *****" >> $LogFile
#      $UPD --nox11 >> $LogFile 2>&1
#      case $? in
#	   0 | 66 ) ;;
#	        * ) ExitCleanup 4 ;;
#      esac
#   done
#
# kernel ipforwarding fix 
   rpm --force -Uvh kernel-hmc51_1-2.6.16.46-199.i686.rpm >> $LogFile 2>&1

   cd /boot
   ln -sf bzImage-*smp bzImage

   KERNEL=`ls bzImage-*smp | cut -c9-`
   cd /
   mv /lib/modules/$KERNEL/kernel/extra_drivers/open/sk98lin_8_30/sk98lin_8_30.ko /lib/modules/$KERNEL/kernel/extra_drivers/open/sk98lin_8_30/sk98lin.ko >> $LogFile 2>&1
   mv /lib/modules/$KERNEL/kernel/extra_drivers/open/e1000_7_6_15_5/e1000_7_6_15_5.ko /lib/modules/$KERNEL/kernel/extra_drivers/open/e1000_7_6_15_5/e1000.ko >> $LogFile 2>&1
   /sbin/depmod -a $KERNEL >> $LogFile 2>&1


  # defect 691996:
  # from:
  #   ftp://javaserv.hursley.ibm.com/pmrs/44939,001,866/pxi3260sr1ifx-20090225_01-sdk.jar
  # patch:
  #   sdk/jre/lib/logging.jar
  # to:
  #   /opt/IBMJava/jre/lib/logging.jar
  echo "=======================================================================" >> $LogFile
  echo "Applying updated /opt/IBMJava/jre/lib/logging.jar"                       >> $LogFile
  echo "=======================================================================" >> $LogFile
  echo "Before update:"                              >> $LogFile
  ls -la /opt/IBMJava/jre/lib/logging.jar            >> $LogFile
  cp -p $image/baseOS/logging.jar /opt/IBMJava/jre/lib/logging.jar
  echo "After update:"                               >> $LogFile
  ls -la /opt/IBMJava/jre/lib/logging.jar            >> $LogFile


  # defect 700220:
  echo "=======================================================================" >> $LogFile
  echo "Applying updated openssh"                                                >> $LogFile
  echo "=======================================================================" >> $LogFile
  cd $image/baseOS
  rpm --force -Uvh openssh-4.2p1-36.i686.rpm >> $LogFile 2>&1
  rpm --force -Uvh openssh-askpass-4.2p1-36.i686.rpm >> $LogFile 2>&1
  
fi

cd $image
mkdir -p /console/HSC
rm -rf /console/HSC/*
if [ -f /console/HSC/.VERSION ]; then
   rm /console/HSC/.VERSION
fi
if [ -f /console/HSC/.dev ]; then
   rm /console/HSC/.dev
fi

touch /opt/hsc/data/pendingUpdate

cp -p finishUpdate /console/HSC/
cp -p postinstall /console/HSC/
cp -p .VERSION /console/HSC/.VERSION
if [ -f .dev ]; then
  cp -p .dev /console/HSC/.dev
fi

#cp -p java6*.rpm /console/HSC/
cp -p -r baseHMC /console/HSC/
cp -p IBMhmc.MH*_d?-*.i386.rpm /console/HSC/

cp -p .image.updates /console/HSC/
cp -p .signature /console/HSC/

if [ -f hsc.jar ]; then
   cp -p hsc.jar /console/HSC/
fi
if [ -f hdwr_svr ]; then
   cp -p hdwr_svr /console/HSC/
fi

if [ -f $image/boot.install ]; then
   cp -p /etc/init.d/boot.install /tmp/boot.install.tmp
   cp -f $image/boot.install /tmp/boot.install
   mv /tmp/boot.install /etc/init.d/boot.install
   chown root.root /etc/init.d/boot.install
   chmod 555 /etc/init.d/boot.install
fi


echo "==========================================================" >> $LogFile
echo "********* Install/Update complete at `date` *********"      >> $LogFile
echo "********* A system reboot is REQUIRED now...*********"      >> $LogFile
echo "==========================================================" >> $LogFile
ExitCleanup 0
