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

#-------------------------------------------------------------------------------
# Cleanup script for BobCat
#-------------------------------------------------------------------------------
function BobCatCleanup {
    rm -rf /tmp/_bcat_
    if [ $1 -ne 0 ]; then
        rm -rf /opt/bobcat
    fi
    exit $1
}

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

#-------------------------------------------------------------------------------
# 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 installs patches that are not in rpms
#-------------------------------------------------------------------------------
function InstallPatch {
    FILE=`echo $1`; shift;
    MESG=`echo $*`
    if [ -f $FILE ]; then
        echo "--- Installing ${MESG}...." 
        cd /
        tar -xf $FILE
        if [ $? -ne 0 ]; then
            ExitCleanup 3
        fi
    fi
}

#-------------------------------------------------------------------------------
# This function renames the modules for hmc
#-------------------------------------------------------------------------------
function SetModules {
    KERNEL=`uname -r`
    KERN_DIR="/lib/modules/$KERNEL/kernel"
    mv $KERN_DIR/drivers/net/bcm/bcm5700.o    \
       $KERN_DIR/drivers/net/bcm/bcm5700-6.2.17.o 
    mv $KERN_DIR/extra_drivers/open/bcm5700-7.3.4/bcm5700-7.3.4.o \
       $KERN_DIR/extra_drivers/open/bcm5700-7.3.4/bcm5700.o
    mv $KERN_DIR/extra_drivers/open/e1000-5.2.30.1/e1000-5.2.30.1.o \
       $KERN_DIR/extra_drivers/open/e1000-5.2.30.1/e1000.o
    mv $KERN_DIR/extra_drivers/open/irqtune-0.6/irqtune-0.6.o \
       $KERN_DIR/extra_drivers/open/irqtune-0.6/irqtune.o
    mv $KERN_DIR/extra_drivers/closed/epca-1.51/epca-1.51.o \
       $KERN_DIR/extra_drivers/closed/epca-1.51/epca.o 
    mv $KERN_DIR/extra_drivers/closed/xcrypto-1.0/xcrypto-1.0.o \
       $KERN_DIR/extra_drivers/closed/xcrypto-1.0/xcrypto.o 
    mv $KERN_DIR/extra_drivers/closed/xcrypto-1.0/xcrypto_serial-1.0.o \
       $KERN_DIR/extra_drivers/closed/xcrypto-1.0/xcrypto_serial.o 
    /sbin/depmod -a
}

#-------------------------------------------------------------------------------
# This function Updates the Base Operating System
#-------------------------------------------------------------------------------
function UpdateOS {
    if [ -d $image/baseOS ]; then
        CURR_DIR=$PWD
        cd $image/baseOS
        echo "--- Updating base ...."
        dhcpd_conf=0
        if [ -f /etc/dhcpd.conf ]; then
            dhcpd_conf=1
        fi

        OldUpdates=`rpm -qa|grep hmc-update-GA`     # Locate the older updates
        for i in $OldUpdates                        # and remove them all
        do                                          # before we add the 
            rpm -e $i --nodeps 2>&1                 # latest updates
        done
        # MCP Update. return code of 0 indicates successful update
        # return code of 66 indicates update has already been applied
        TYPE=`file $image/baseOS/*update* | cut -f2 -d ":"|cut -f2 -d" "`
        if [ "$TYPE" == "RPM" ]; then
            InstallRpm -ivh hmc-update-GA-*.i386.rpm
            if [ $? -eq 0 ]; then
                cd /tmp/mcp-update
                ./post_install.*.sh
                case $? in
                    0 | 66 ) ;;
                         * ) EraseRpm hmc-update-GA
                             ExitCleanup 4 ;;
                esac
            fi
        else
            for UPD in `cat index`
            do
                echo "***** Updating $UPD *****" >> $LogFile
                $UPD >> $LogFile 2>&1
                case $? in
                    0 | 66 ) ;;
                         * ) ExitCleanup 4 ;;
                esac
            done
        fi

        rm -f /opt/IBMJava/src.jar 2>/dev/null

        sed -e 's/ENABLE_SUSECONFIG=no/ENABLE_CONFIG=yes/' \
            /etc/sysconfig/suseconfig > /tmp/suseconfig
        mv /etc/sysconfig/suseconfig /etc/sysconfig/suseconfig.orig
        cp /tmp/suseconfig /etc/sysconfig

        # We need to preserve /etc/hosts before runing SuSEconfig as it updates
        # localhost to point to 127.0.0.2 which access problems to users.

        mv /etc/hosts /etc/hosts.orig
        cp /etc/hosts.orig /etc/hosts
        /sbin/SuSEconfig >> $LogFile 2>&1
        mv /etc/sysconfig/suseconfig.orig /etc/sysconfig/suseconfig
        mv /etc/hosts.orig /etc/hosts

        cp /etc/opera6rc.fixed /tmp/saved_opera6rc.fixed
        InstallRpm -Uvh $image/baseOS/opera*rpm
        cp /tmp/saved_opera6rc.fixed /etc/opera6rc.fixed

        # If there was no dhcpd.conf file before installation, then remove 
        # the file created by installing dhcp-server

        if [ "$dhcpd_conf" == "0" ]; then
            if [ -f /etc/dhcpd.conf ]; then
                rm -f /etc/dhcpd.conf
            fi
        fi

        DISKINFO="/usr/sbin/hwinfo --disk 2>/dev/null"
        hdl=`$DISKINFO | grep "Device File" | cut -d'/' -f3 | sed -e 's/ //g'`
        for hd in $hdl
        do  
            if [ "$hd" = "" ]; then
                hd="hda"
            fi
            break
        done
        
        echo "========================================" >> $LogFile
        echo "******** Updating Module Names  ********" >> $LogFile
        echo "========================================" >> $LogFile
        SetModules >> $LogFile 2>&1
        echo "========================================" >> $LogFile
        echo "********* Updating Boot Record *********" >> $LogFile
        echo "========================================" >> $LogFile
        /usr/sbin/grub-install /dev/$hd>> $LogFile 2>&1
        
        rm /opt/IBMJava
        ln -sf /opt/IBMJava2-142 /opt/IBMJava
        cd $CURR_DIR
    fi
}

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

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 $patchdir doesn't exist"
    echo "Please specify directory containing the installable packages."
    ExitCleanup 2
fi

if [ -f /opt/hsc/data/config/NO_UPDATE_FILES ]; then
    rm -f /tmp/saved_files.tar
    cat /opt/hsc/data/config/NO_UPDATE_FILES | \
        xargs tar -cvf /tmp/saved_files.tar
fi

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
migration_needed=0

if [ ! -d /opt/IBMJava ]; then
    ln -sf /opt/IBMJava2-142 /opt/IBMJava
fi

# Remove the zip file to save space
rm -f /usr/local/hsc_install.images/*.zip

# Save old log files
if [ -f $LogFile ]; then
    mv -f ${LogFile}.3 ${LogFile}.4 2>/dev/null
    mv -f ${LogFile}.2 ${LogFile}.3 2>/dev/null
    mv -f ${LogFile}.1 ${LogFile}.2 2>/dev/null
    mv -f ${LogFile}   ${LogFile}.1 2>/dev/null
fi

# requirement for installing Update images
if [ -f $image/.require ]; then
    /opt/hsc/bin/hsc version > /tmp/.version
    INSTALL="true"
    VERSION=`cat $image/.require | cut -d '|' -f 1`
    RELEASE=`cat $image/.require | cut -d '|' -f 2`

    grep -q "$VERSION" /tmp/.version
    if [ $? -ne 0 ]; then
        INSTALL="false"
    else
        if [ "$RELEASE" != "" ]; then
            grep -q "$RELEASE" /tmp/.version
            if [ $? -ne 0 ]; then
                INSTALL="false"
            fi
        fi
    fi

    if [ "$INSTALL" == "false" ]; then
        echo "Installation of this update image requires HMC $VERSION $RELEASE"
        echo "Current HMC is not $VERSION $RELEASE"
        echo "Unable to install this update image."
        ExitCleanup 3
    fi
fi

# Save current hmc version file
cp /opt/hsc/data/version /tmp/.version.orig

# 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
            PTFdesc=`rpm -qpic IBMhmc.MH0*rpm | grep "$PTF:"`

            # 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


echo "=========================================================" >> $LogFile
disk=`cat $image/.signature | cut -d' ' -f 4`
if [[ "$disk" == "0" || -f $image/.image.updates ]]; then
    Update="true"
    echo "*********   Performing Update operation `date`  *********" >> $LogFile
else
    Update="false"
    echo "*********   Performing Install operation `date` *********" >> $LogFile
fi
if [ "$Update" == "true" ]; then
    cd /
    rm -f /tmp/WsmSaved.tar
    /bin/tar cf /tmp/WsmSaved.tar /var/websm/security /usr/websm/codebase/SM* \
       /var/websm/config/user_settings/websm.cfg 2>/dev/null
fi
echo "=========================================================" >> $LogFile

# Save security information This will get processed in next reboot
sp=`grep ^SocketProvider /opt/hsc/data/cim.properties`
if [[ $sp == *PKCS12SocketProvider ]]; then
    echo $sp > /var/hsc/tmp/cimomcfg.properties.reboot
fi

# Install RMC and CSM if rmc directory exists
if [ -d $image/rmc ]; then
    cd $image/rmc
    echo "--- Installing RSCT ...."
    RsctInstall src src-*rpm
    if [ "$Update" == "false" ]; then
        # Ensure src is started
        cd /
        /sbin/srcmstr -r &
	srcmstr_pid=$!
    fi
    cd $image/rmc
    RsctInstall rsct.core.utils rsct.core.utils-*rpm 
    RsctInstall rsct.core       rsct.core-*rpm 
    RsctInstall rsct.service    rsct.service-*rpm 
    RsctInstall rsct.basic      rsct.basic-*rpm 
    echo "--- Installing CSM ...."
    RsctInstall csm.core        csm.core-*rpm 
    RsctInstall csm_hmc.server  csm_hmc.server-*rpm 
    RsctInstall csm_hmc.hdwr_svr  csm_hmc.hdwr_svr-*rpm 
    RsctInstall csm_hmc.client  csm_hmc.client-*rpm 
    RsctInstall csm.server.hsc  csm.server.hsc-*rpm 
    echo "--- Installing LPARCMD ...."
    RsctInstall hsc.lparcmd     hsc.lparcmd*.rpm 
    if [ "$Update" == "false" ]; then
       if [ "$srcmstr_pid" != "" ]; then
	  kill -9 $srcmstr_pid
       fi
    fi
fi

# Install websm if websm directory exists
if [ -d $image/websm ]; then
    cd $image/websm
    echo "--- Installing WebSM ....."
    EraseRpm sysmgt.websm.framework
    InstallRpm -ivh sysmgt.websm.framework*.rpm
    EraseRpm sysmgt.websm.webaccess
    InstallRpm -ivh sysmgt.websm.webaccess*.rpm
    EraseRpm sysmgt.websm.apps
    InstallRpm -ivh sysmgt.websm.apps*.rpm
    EraseRpm sysmgt.websm.security
    InstallRpm -ivh sysmgt.websm.security-[0123456789]*.rpm
    if [ "$Update" == "false" ]; then
        EraseRpm sysmgt.websm.security-ex
        InstallRpm -ivh sysmgt.websm.security-ex*.rpm
        EraseRpm sysmgt.websm.security-us
        InstallRpm -ivh sysmgt.websm.security-us*.rpm
    fi
    EraseRpm sysmgtlib.framework.core
    InstallRpm -ivh sysmgtlib.framework.core*.rpm
    if [ ! -f /usr/websm/codebase/pluginjars/bundles.jar ]; then
        ln -sf /usr/websm/codebase/bundles.jar /usr/websm/codebase/pluginjars/
    fi
fi


# Install the base HMC rpms if baseHMC directory exists
if [ -d $image/baseHMC ]; then
    cd $image/baseHMC
    echo "--- Installing Websphere Express package...."
    
    if [ -d $image/bobcat ]; then
        if [ -f /opt/bobcat/bin/stopServer.sh ]; then
            /opt/bobcat/bin/stopServer.sh server1
        fi
        rm -rf /opt/bobcat
        rm -rf /tmp/_bcat_
        mkdir /tmp/_bcat_
        BOBCATZIP=$image/bobcat/EmbeddedExpress_*.zip
        unzip $BOBCATZIP -d /tmp/_bcat_ 2>&1 >/dev/null >> $LogFile 2>&1

        /tmp/_bcat_/install.sh        \
            -installroot /opt/bobcat  \
            -hostName localhost       \
            >> $LogFile 2>&1
        if [ $? -ne 0 ]; then
            echo " Websphere Express Installation Failed"
            rm -rf /opt/bobcat
        else
            /opt/bobcat/bin/wasprofile.sh                          \
                -create                                            \
                -profileName profile1                              \
                -profilePath /opt/bobcat/profiles/profile1         \
                -templatePath /opt/bobcat/profileTemplates/default \
                -nodeName DefaultNode                              \
                -hostName localhost                                \
                -cellName DefaultNode                              \
                -isDefault                                         \
                -portsFile $image/portdef.props                    \
                >> $LogFile 2>&1

            # Some migration stuff here

            /opt/bobcat/bin/startServer.sh server1                       \
                -script /opt/bobcat/bin/start_server1.sh 2>&1 >/dev/null \
                >> $LogFile 2>&1
            grep "^wasexpress" /etc/services 2>/dev/null
            if [ $? -ne 0 ]; then
                echo "wasexpress       4411/tcp" >>/etc/services
                echo "wasexpress       4411/udp" >>/etc/services
            fi

            chmod 555 /opt/bobcat/bin/rc.was
            INIT_ENTRY="was:2345:once:/opt/bobcat/bin/rc.was >/dev/console 2>&1"
            grep "^was" /etc/inittab 2>/dev/null
            if [ $? -ne 0 ];then
                echo "$INIT_ENTRY" >>/etc/inittab
            fi
        fi
        rm -rf /tmp/_bcat_

        if [ "$Update" == "true" ]; then
            if [ ! -f /opt/hsc/data/extbackupfile.list ]; then
                touch /opt/hsc/data/extbackupfile.list
            fi
            # remove all previous entries related to bobcat
            grep -v "/opt/bobcat/" /opt/hsc/data/extbackupfile.list >/tmp/_s_e_b
            mv /tmp/_s_e_b /opt/hsc/data/extbackupfile.list

            il=`find /opt/bobcat/ -type f -print`
            for i in $il
            do
                grep -q "$i" /opt/hsc/data/extbackupfile.list
                if [ $? -ne 0 ]; then
                    echo "$i" >> /opt/hsc/data/extbackupfile.list
                fi
            done
            il=`find /opt/bobcat/ -type l -print`
            for i in $il
            do
                grep -q "$i" /opt/hsc/data/extbackupfile.list
                if [ $? -ne 0 ]; then
                    echo "$i" >> /opt/hsc/data/extbackupfile.list
                fi
            done
            /opt/bobcat/bin/startServer.sh server1

        fi
    fi

    echo "--- Installing Common Console Framework...."
    /usr/sbin/groupadd -g 508 ccfw 2>/dev/null
    EraseRpm ccfw
    if [ "$Update" == "false" ]; then
        rm -rf /opt/ccfw
    fi
    InstallRpm -ivh ccfw*.rpm
    /usr/sbin/useradd -c "Common Console Framework" -M -g ccfw -d /opt/ccfw ccfw
    echo "--- Installing HMC ..."
    if [ "$Update" == "true" ]; then
	grep -q "/bin/hmcbash" /etc/shells
	if [ $? -ne 0 ]; then
	   migration_needed=1
	fi
    fi
    InstallRpm -ivh IBMhsc.corepe*.rpm
    for i in IBMhsc.bundles_*.rpm
    do
        RPM_NAME=`echo $i|cut -f1 -d "-"`
        EraseRpm $RPM_NAME
        InstallRpm -Uvh $i
    done
    InstallRpm -Uvh IBMhsc.lparmgt*.rpm
    InstallRpm -Uvh IBMhsc.auiml_en_US*.rpm
    InstallRpm -Uvh IBMhsc.plfmgt*.rpm
    EraseRpm IBMhsc.cimprovider
    InstallRpm -ivh IBMhsc.cimprovider*.rpm
    InstallRpm -Uvh IBMhsc.bidi-fnt*.rpm
    InstallRpm -Uvh IBMhsc.coreserver*.rpm
    echo "--- Installing HMC Help Software..."
    InstallRpm -Uvh IBMhsc.help*.rpm 
    echo "--- Installing Netboot ..."
    InstallRpm -Uvh IBMhsc.netboot*rpm
    InstallRpm -Uvh nimol*rpm

    if [ "$Update" == "true" ]; then
        echo "--- Removing old ASM Proxy...."
        /opt/bobcat/bin/wsadmin.sh -conntype NONE -c "\$AdminApp uninstall asmproxy" >> $LogFile 2>&1
    fi
    echo "--- Installing ASM Proxy...."
    cp /opt/hsc/data/asmproxy.war /opt/bobcat/profiles/profile1/installableApps/

    ADMIN_DATA="{/opt/bobcat/profiles/profile1/installableApps/asmproxy.war}"
    ADMIN_DATA="$ADMIN_DATA { -usedefaultbindings -nodeployejb -appname"
    ADMIN_DATA="$ADMIN_DATA  asmproxy -contextroot asmproxy} "
    /opt/bobcat/bin/wsadmin.sh -conntype NONE -c \
        "\$AdminApp install $ADMIN_DATA " >> $LogFile 2>&1

    echo "--- Importing ASM server signed certificate to bobcat JVM root CA..."
    /opt/bobcat/java/bin/keytool                            \
        -import                                             \
        -trustcacerts                                       \
        -alias asm                                          \
        -file /opt/hsc/data/asm.cer                         \
        -keystore /opt/bobcat/java/jre/lib/security/cacerts \
        -storetype jks                                      \
        -storepass changeit                                 \
        -noprompt >> $LogFile 2>&1

    if [ "$Update" == "true" ]; then
        echo "--- Removing old System Plan Viewer ...."
        /opt/bobcat/bin/wsadmin.sh -conntype NONE -c \
        "\$AdminApp uninstall sysplanviewer" >> $LogFile 2>&1
    fi

    echo "--- Installing System Plan Viewer Application...."
    cp /opt/hsc/data/sysplanviewer.war /opt/bobcat/profiles/profile1/installableApps/
    
    ADM_DATA="{/opt/bobcat/profiles/profile1/installableApps/sysplanviewer.war}"
    ADM_DATA="$ADM_DATA { -usedefaultbindings -nodeployejb -appname"
    ADM_DATA="$ADM_DATA  sysplanviewer -contextroot sysplanviewer} "
    /opt/bobcat/bin/wsadmin.sh -conntype NONE -c \
        "\$AdminApp install $ADM_DATA " >> $LogFile 2>&1

    echo "--- Linking csdml.jar to System Plan Viewer..."
    ln -sf /usr/websm/codebase/pluginjars/csdml.jar /opt/bobcat/profiles/profile1/installedApps/DefaultNode/sysplanviewer.ear/sysplanviewer.war/WEB-INF/lib/

    echo "--- Linking hsc.jar to System Plan Viewer..."
    ln -sf /usr/websm/codebase/pluginjars/hsc.jar /opt/bobcat/profiles/profile1/installedApps/DefaultNode/sysplanviewer.ear/sysplanviewer.war/WEB-INF/lib/
    ln -sf /usr/websm/codebase/pluginjars/aca.jar /opt/bobcat/profiles/profile1/installedApps/DefaultNode/sysplanviewer.ear/sysplanviewer.war/WEB-INF/lib/

    echo "--- Linking ldw_csdmlBeans.jar to System Plan Viewer..."
    ln -sf /usr/websm/codebase/pluginjars/ldw_csdmlBeans.jar /opt/bobcat/profiles/profile1/installedApps/DefaultNode/sysplanviewer.ear/sysplanviewer.war/WEB-INF/lib/    

fi

# Install InfoCenter if info directory exists
if [ -d $image/info ]; then
    cd $image/info
    echo "--- Installing InfoCenter ...."
#   InfoCenter rpm update from SP1 is a delta, do not erase the orig InfoCenter
#
#    if [ "$Update" == "true" ]; then
#        EraseRpm IBMhmc.InfoCenter
#        EraseRpm IBMhmc.InfoCenterBase
#        EraseRpm IBMhmc.InfoCenter.en_US
#    fi
    for RPM in `cat index`
    do
        InstallRpm -Uvh ${RPM}*.rpm
    done
fi

# Install SNM logging
if [ -d $image/snm ]; then
    cd $image/snm
    if [ -f IBMhsc.HPSNM_log*rpm ]; then
         echo "--- Installing HPSNM ...."
         InstallRpm -Uvh IBMhsc.HPSNM_log*rpm
    fi
fi

# Install Infiniband
if [ -d $image/infiniband ]; then
    cd $image/infiniband
    echo "--- Installing Infiniband ...."
    old_rpms=`rpm -qa | grep IBMhsc.IBNM`
    if [ "$old_rpms" != "" ]; then
        EraseRpm $old_rpms
    fi
    EraseRpm IBMhsc.NM_Common
    InstallRpm -Uvh IBMhsc.IBMNM-*rpm
    InstallRpm -Uvh IBMhsc.NM_Common-*rpm
    InstallRpm -Uvh IBMhsc.IBMNM_GUI*rpm
    if [ -f /usr/websm/config/factory_defaults/AppIBNM.db ]; then
        rm -f /usr/websm/config/factory_defaults/AppIBNM.db
    fi
fi

# Install Inventory Scout if service directory exists
if [ -d $image/service ]; then
    cd $image/service
    if [ -f IBMinvscout*rpm ]; then
        echo "--- Installing InventoryScout ...."
        InstallRpm -Uvh IBMinvscout*rpm
    fi
    if [ -f IBMhsc.service*rpm ]; then
       InstallRpm -Uvh IBMhsc.service*.rpm
    fi
fi

# Install Pegasus
if [ -d $image/pegasus ]; then
    cd $image/pegasus
    if [ -f tog-pegasus*rpm ]; then
        echo "--- Installing Open Pegasus ...."
        InstallRpm -Uvh tog-pegasus*rpm
    fi
    if [ -f oob-hw-provider-0*rpm ]; then
        InstallRpm -Uvh oob-hw-provider-0*rpm
    fi
fi

if [ ! -L /usr/lib/libnetchmcx.so ]; then
    ln -s /usr/lib/libnetc.so /usr/lib/libnetchmcx.so
fi

InstallPatch $image/hsc.tar   "Miscellaneous Updates"
InstallPatch $image/X.tar     "X Server Config"
InstallPatch $image/websm.tar "WebSM Updates"

if [ -f /opt/hsc/data/config/NO_UPDATE_FILES ]; then
    if [ -f /tmp/saved_files.tar ]; then
        cd /
        tar -xvf /tmp/saved_files.tar
        rm -f /tmp/saved_files.tar
    fi
fi
 
if [ "$Update" == "true" ]; then
    UpdateOS
    if [ -d $image/patch ]; then
        cd $image/patch
        rm -f /tmp/UpdateKernel
        for i in `ls`; do
            $i >> $LogFile 2>&1
            case $? in
                 0 ) touch /tmp/UpdateKernel ;;
                66 ) ;;
                *  ) ExitCleanup 4 ;;
            esac
        done
        if [ -f /tmp/UpdateKernel ]; then
            mount /mnt/dos
            mv /mnt/dos/linux/vmlinuz /mnt/dos/linux/vmlinuz.old
            cp /boot/vmlinuz /mnt/dos/linux/
            umount /mnt/dos
            rm -f /tmp/UpdateKernel
        fi
    fi
    cd /
    /bin/tar -xf /tmp/WsmSaved.tar
fi

if [ -f $image/postinstall ]; then
    $image/postinstall
fi

if [ -f $image/.VERSION ]; then
    if [ -f $image/.dev ]; then
        cat $image/.VERSION >> /opt/hsc/data/version
    fi
    cat $image/.VERSION > /root/.version
    cat $image/.VERSION > /home/hscroot/.version
    chmod 644 /home/hscroot/.version
    chown hscroot.hmc /home/hscroot/.version
fi

# /opt/hsc/data/version not changed for this update:
if [ "$PTF" != "" ]; then
   # if this PTF installed before, remove its desc
   grep -q "$PTF" /tmp/.version.orig
   if [ $? -eq 0 ]; then
      grep -v "$PTF" /tmp/.version.orig > /opt/hsc/data/version
   fi
fi

# keep other PTFs desc
#grep -q "MH0" /tmp/.version.orig
#if [ $? -eq 0 ]; then
#   grep "MH0" /tmp/.version.orig >> /opt/hsc/data/version
#fi

if [ "$PTFdesc" != "" ]; then
   echo "$PTFdesc" >> /opt/hsc/data/version
fi

# Remove /opt/hsc/data/disablefsplock from GA1 or pre SQ2_0424A_0610 driver
if [ -f /opt/hsc/data/disablefsplock ]; then 
    /bin/rm -f /opt/hsc/data/disablefsplock
fi

if [ ! -f /etc/resolv.conf ]; then 
    touch /etc/resolv.conf
fi

mkdir -p /extra 2>/dev/null

if [ "$Update" == "true" ]; then
    if [ ! -f /done.boothmc ]; then
        touch /done.boothmc
    fi
fi

# Some migration stuff here

if [ "$Update" == "true" ]; then
   if [ $migration_needed -eq 1 ]; then
      /opt/hsc/bin/migrateToHmcBash
   fi
fi

# indicate if this ptf requires another ptf
if [ "$PTF_d" != "" ]; then
    REQUIRE=`cat ${PTF_d} | grep REQUIRE | cut -d ':' -f 2`
    if [ "$REQUIRE" != "NONE" ]; then
        echo "*** The following package is a requisite of this installation:"
        echo "*** ${REQUIRE}"
        echo "*** Please apply the above requisite to complete this installation."
    fi
fi

echo "==========================================================" >> $LogFile
echo "********* Install/Update complete at `date` *********"      >> $LogFile
echo "==========================================================" >> $LogFile
ExitCleanup 0
