#!/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 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
}

#-------------------------------------------------------------------------------
# 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 removal task.
#-------------------------------------------------------------------------------
function EraseRpm {
    # If rpm is part of no update list then do not do anything with it
    if [ -f /opt/hsc/data/config/NO_UPDATE_RPMS ]; then
        x="$2"
        for i in `cat /opt/hsc/data/config/NO_UPDATE_RPMS`; do
            if [ "$x" == "$i" ]; then
                return
            fi
        done
    fi
    echo "=====================================================" >> $LogFile
    echo "***** Executing rpm -evv $* *****                    " >> $LogFile
    echo "=====================================================" >> $LogFile
    rpm -evv $* --nodeps --allmatches >> $LogFile 2>&1
    if [ $? -ne 0 ]; then
        echo "Error removing rpm fileset named $2" >> $LogFile
    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 is on the HMC
CR=`/opt/hsc/bin/hsc version | grep "Release:" | cut -d':' -f2 | cut -d'.' -f2`
if [ "$CR" != 3 ]; then
   echo "Installation of this update image requires HMC Version 7 Release 3.3.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

# Install Pegasus
if [ -d $image/pegasus ]; then
    cd $image/pegasus
    if [ -f IBMhmc.pegasus*rpm ]; then
        echo "--- Installing Pegasus ...."
        InstallRpm -Uvh IBMhmc.pegasus*rpm
    fi
fi
#
if [ -d $image/rmc ]; then
    cd $image/rmc
    echo "--- Installing RSCT ...."
    RsctInstall rsct.core.utils rsct.core.utils-*rpm
    RsctInstall rsct.core       rsct.core-*rpm
fi

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

touch /opt/hsc/data/pendingUpdate

cp -p finishUpdate /console/HSC/
cp -p postinstall /console/HSC/

#cp -p .VERSION /console/HSC/

cp -p -r baseHMC /console/HSC/

cp -p IBMhmc.MH01150_d1-7.0-3.3.0.i386.rpm /console/HSC/

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

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