#!/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 {
    cd /boot
    KERNEL=`ls bzImage-*default | cut -c9-`
    cd /
    KERN_DIR="/lib/modules/$KERNEL/kernel"
    mv $KERN_DIR/drivers/net/bcm/bcm5700.ko $KERN_DIR/drivers/net/bcm/bcm5700_8_1_11.ko 
    touch $KERN_DIR/drivers/net/bcm/bcm5700_8_1_11.ko 

    mv $KERN_DIR/extra_drivers/open/bcm5700_8_2_7/bcm5700_8_2_7.ko $KERN_DIR/extra_drivers/open/bcm5700_8_2_7/bcm5700.ko
    touch $KERN_DIR/extra_drivers/open/bcm5700_8_2_7/bcm5700.ko

    mv $KERN_DIR/extra_drivers/open/e1000_7_3_15/e1000_7_3_15.ko $KERN_DIR/extra_drivers/open/e1000_7_3_15/e1000.ko
    touch $KERN_DIR/extra_drivers/open/e1000_7_3_15/e1000.ko
    mv $KERN_DIR/drivers/net/e1000/e1000.ko $KERN_DIR/drivers/net/e1000/e1000_6_0_52.ko
    touch $KERN_DIR/drivers/net/e1000/e1000_6_0_52.ko

    mv $KERN_DIR/extra_drivers/open/bnx2_1_3/bnx2_1_3.ko $KERN_DIR/extra_drivers/open/bnx2_1_3/bnx2.ko
    touch $KERN_DIR/extra_drivers/open/bnx2_1_3/bnx2.ko

    /sbin/depmod -a -v $KERNEL
}

#-------------------------------------------------------------------------------
# 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 --nox11 >> $LogFile 2>&1
                case $? in
                    0 | 66 ) ;;
                         * ) ExitCleanup 4 ;;
                esac
            done
	    ln -sf /boot/bzImage-* /boot/bzImage
        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

# 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


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.deploy      csm.deploy*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

rpm -qa | grep -q sysmgt.websm.framework
if [ $? -eq 0 ]; then
   EraseRpm sysmgt.websm.framework
fi
# Install websm if websm directory exists
#if [ -d $image/websm ]; then
#    cd $image/websm
#    echo "--- Installing WebSM ....."
#    InstallRpm -ivh sysmgt.websm.framework*.rpm
#fi


# Install the base HMC rpms if baseHMC directory exists
if [ -d $image/baseHMC ]; then
    cd $image/baseHMC
    
    if [ -f $image/baseHMC/apache-tomcat-5.5.20-embed.tar.gz ]; then
       cd /opt
       tar -xzf $image/baseHMC/apache-tomcat-5.5.20-embed.tar.gz
       cd $image/baseHMC
    fi
    if [ -d $image/bobcat ]; then
        echo "--- Installing Websphere Express package...."
        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 -Uvh IBMhsc.corepe*.rpm
    InstallRpm -Uvh IBMhsc.auiml_en_US*.rpm
#    EraseRpm IBMhsc.cimprovider
    InstallRpm -Uvh IBMhsc.cimprovider*.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
    InstallRpm -Uvh osinstall*rpm

    echo "--- Installing NLS..."
    for i in IBMhsc.bundles_*.rpm
    do
        RPM_NAME=`echo $i|cut -f1 -d "-"`
        EraseRpm $RPM_NAME
        InstallRpm -Uvh $i
    done

    echo "Creating asmproxy and sysplanviewer directory under /opt/ccfw/tomcat/webapps"
    mkdir -p /opt/ccfw/tomcat/webapps/asmproxy
    mkdir -p /opt/ccfw/tomcat/webapps/sysplanviewer

    echo "Copying extension webapps config to /opt/ccfw/tomcat/extensions"
    cp -f /opt/hsc/data/asmproxyExtension.xml   /opt/ccfw/tomcat/extensions
    cp -f /opt/hsc/data/sysplanviewerExtension.xml   /opt/ccfw/tomcat/extensions

    echo "Extracting asmproxy.war to /opt/ccfw/tomcat/webapps/asmproxy"
    cd /opt/ccfw/tomcat/webapps/asmproxy
    /opt/IBMJava2-142/bin/jar  -xvf /opt/hsc/data/asmproxy.war >> $LogFile 2>&1

#    echo "Importing ASM server signed public key to JVM keystore"
#    /opt/IBMJava2-142/bin/keytool -import -v -noprompt -trustcacerts -alias asm -file /opt/hsc/data/asm.cer -keystore /opt/IBMJava2-142/jre/lib/security/cacerts -storepass changeit -storetype jks

    #System Plan Viewer: uninstall it if it exists, install via jar xvf
    if [ "$Update" == "true" ]; then
        echo "--- Removing old System Plan Viewer ...."
        rm -rf /opt/ccfw/tomcat/webapps/sysplanviewer/*
    fi
    echo -n "--- Installing System Plan Viewer ......."
    unzip /usr/websm/codebase/pluginjars/sysplanviewer.war -d /opt/ccfw/tomcat/webapps/sysplanviewer/ 2>&1 >/dev/null >> $LogFile 2>&1
    echo " done"
    #System Plan Viewer: done
fi

# Install InfoCenter if info directory exists
if [ -d $image/info ]; then
    cd $image/info
    echo "--- Installing service documentation ...."
    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.NM_Common-*rpm
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_img*rpm ]; then
       InstallRpm -Uvh IBMhsc.service_img*.rpm
    fi
    if [ -f IBMhsc.service_content*rpm ]; then
       InstallRpm -Uvh IBMhsc.service_content*.rpm
    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
    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 $image/hsc.jar ]; then
   cp -p /usr/websm/codebase/pluginjars/hsc.jar /tmp/hsc.jar.orig
   cp -f $image/hsc.jar /tmp/hsc.jar
   mv /tmp/hsc.jar /usr/websm/codebase/pluginjars/hsc.jar
   chown bin.bin /usr/websm/codebase/pluginjars/hsc.jar
   chmod 555 /usr/websm/codebase/pluginjars/hsc.jar
fi

if [ -f $image/startConsole ]; then
   cp -p /opt/hsc/bin/startConsole /tmp/startConsole.orig
   cp -f $image/startConsole /tmp/startConsole
   mv /tmp/startConsole /opt/hsc/bin/startConsole
   chown bin.bin /opt/hsc/bin/startConsole
   chmod 555 /opt/hsc/bin/startConsole
fi

if [ -f $image/startccfw ]; then
   cp -p /opt/ccfw/startccfw /tmp/startccfw.orig
   cp -f $image/startccfw /tmp/startccfw
   mv /tmp/startccfw /opt/ccfw/startccfw
   chown root.ccfw /opt/ccfw/startccfw
   chmod 775 /opt/ccfw/startccfw
fi

if [ -f $image/ccfw.jar ]; then
   cp -p /opt/ccfw/ccfw.jar /tmp/ccfw.jar.orig
   cp -f $image/ccfw.jar /tmp/ccfw.jar
   mv /tmp/ccfw.jar /opt/ccfw/ccfw.jar
   chown root.ccfw /opt/ccfw/ccfw.jar
   chmod 664 /opt/ccfw/ccfw.jar
fi

if [ -f $image/appletssl.jar ]; then
   cp -p /opt/ccfw/www/applets/appletssl.jar /tmp/appletssl.jar.orig
   cp -f $image/appletssl.jar /tmp/appletssl.jar
   mv /tmp/appletssl.jar /opt/ccfw/www/applets/appletssl.jar
   chown root.ccfw /opt/ccfw/www/applets/appletssl.jar
   chmod 775 /opt/ccfw/www/applets/appletssl.jar
fi

if [ -f $image/pseries_vterm.jar ]; then
   cp -f $image/pseries_vterm.jar /tmp/pseries_vterm.jar
   mv /tmp/pseries_vterm.jar /opt/ccfw/www/applets/pseries_vterm.jar
   chown root.ccfw /opt/ccfw/www/applets/pseries_vterm.jar
   chmod 775 /opt/ccfw/www/applets/pseries_vterm.jar
fi

if [ -f $image/network_config.xml ]; then
   cp -p /opt/ccfw/data/network/network_config.xml /tmp/network_config.xml.orig
   cp -f $image/network_config.xml /tmp/network_config.xml
   mv /tmp/network_config.xml /opt/ccfw/data/network/network_config.xml
   chown root.ccfw /opt/ccfw/data/network/network_config.xml
   chmod 664 /opt/ccfw/data/network/network_config.xml
fi
mkdir -p /var/hsc/vterm
#touch /var/hsc/vterm/vtermon

if [ ! -f /usr/lib/firefox/plugins/libjavaplugin_oji.so ]; then
   cd /usr/lib/firefox/plugins
   ln -s /opt/IBMJava2-142/jre/bin/libjavaplugin_ojigcc3.so libjavaplugin_oji.so
fi

if [ -f $image/HSCUserConf ]; then
   cp -f $image/HSCUserConf /tmp/HSCUserConf
   mv /tmp/HSCUserConf /opt/hsc/bin/HSCUserConf
   chown root.root /opt/hsc/bin/HSCUserConf
   chmod 4755 /opt/hsc/bin/HSCUserConf
fi

#if [ ! -f /lib/modules/2.6.5-355-default/kernel/drivers/net/bcm/bcm5700.ko ]; then
#   cp -p /lib/modules/2.6.5-355-default/kernel/drivers/net/bcm/bcm5700-8_1_11.ko /lib/modules/2.6.5-355-default/kernel/drivers/net/bcm/bcm5700.ko
#fi
#if [ ! -f /lib/modules/2.6.5-355-default/kernel/drivers/net/e1000/e1000.ko ]; then
#   cp -p /lib/modules/2.6.5-355-default/kernel/drivers/net/e1000/e1000_6_0_52.ko /lib/modules/2.6.5-355-default/kernel/drivers/net/e1000/e1000.ko
#fi

if [ -f $image/statusUpdate.jar ]; then
   cp -f $image/statusUpdate.jar /tmp/statusUpdate.jar
   mv /tmp/statusUpdate.jar /opt/ccfw/jars/statusUpdate.jar
   chown root.ccfw /opt/ccfw/jars/statusUpdate.jar
   chmod 664 /opt/ccfw/jars/statusUpdate.jar
fi


if [ -f $image/wui-config.xml ]; then
   cp -p /opt/ccfw/data/ui/bonsai/wui-config.xml /tmp/wui-config.xml.orig
   cp -f $image/wui-config.xml /tmp/wui-config.xml
   mv /tmp/wui-config.xml /opt/ccfw/data/ui/bonsai/wui-config.xml
   chown root.ccfw /opt/ccfw/data/ui/bonsai/wui-config.xml
   chmod 664 /opt/ccfw/data/ui/bonsai/wui-config.xml
fi

if [ -f $image/PIHSC_Model.mof ]; then
   cp -p /opt/hsc/data/mof/CIM26/PIHSC_Model.mof /tmp/PIHSC_Model.mof.orig
   cp -f $image/PIHSC_Model.mof /tmp/PIHSC_Model.mof
   mv /tmp/PIHSC_Model.mof /opt/hsc/data/mof/CIM26/PIHSC_Model.mof
   chown bin.bin /opt/hsc/data/mof/CIM26/PIHSC_Model.mof
   chmod 555 /opt/hsc/data/mof/CIM26/PIHSC_Model.mof
   su -c "/opt/hsc/bin/compileMof" ccfw
fi

if [ -f $image/runAccessmgr ]; then
   cp -f $image/runAccessmgr /tmp/runAccessmgr
   mv /tmp/runAccessmgr /opt/hsc/sbin/runAccessmgr
   chown root.root /opt/hsc/sbin/runAccessmgr
   chmod 554 /opt/hsc/sbin/runAccessmgr
fi

if [ -f $image/1.PSeriesTower ]; then
   cp -p /opt/ccfw/data/plugins/tower/1.PSeriesTower /tmp/1.PSeriesTower.orig
   cp -f $image/1.PSeriesTower /tmp/1.PSeriesTower
   mv /tmp/1.PSeriesTower /opt/ccfw/data/plugins/tower/1.PSeriesTower
   chown root.ccfw /opt/ccfw/data/plugins/tower/1.PSeriesTower
   chmod 664 /opt/ccfw/data/plugins/tower/1.PSeriesTower
fi

if [ -f $image/PIHSCResponsePropFile ]; then
   cp -p /opt/hsc/data/PIHSCResponsePropFile /tmp/PIHSCResponsePropFile.orig
   cp -f $image/PIHSCResponsePropFile /tmp/PIHSCResponsePropFile
   mv /tmp/PIHSCResponsePropFile /opt/hsc/data/PIHSCResponsePropFile
   chown bin.bin /opt/hsc/data/PIHSCResponsePropFile
   chmod 555 /opt/hsc/data/PIHSCResponsePropFile
fi

if [ -f $image/libgettid.so ]; then
   cp -f $image/libgettid.so /tmp/libgettid.so
   mv /tmp/libgettid.so /usr/lib/libgettid.so
   chown root.root /usr/lib/libgettid.so
   chmod 555 /usr/lib/libgettid.so
   ln -s /usr/lib/libgettid.so /opt/hsc/lib/libgettid.so
fi

if [ -f $image/xinitrc ]; then
   cp -p /opt/hsc/data/xinitrc /tmp/xinitrc.orig
   cp -f $image/xinitrc /tmp/xinitrc
   mv /tmp/xinitrc /opt/hsc/data/xinitrc
   chown root.root /opt/hsc/data/xinitrc
   chmod 644 /opt/hsc/data/xinitrc
fi


if [ -f $image/mgrstart.ini ]; then
   cp -p /opt/ccfw/data/mgrstart.ini /tmp/mgrstart.ini.orig
   cp -f $image/mgrstart.ini /tmp/mgrstart.ini
   mv /tmp/mgrstart.ini /opt/ccfw/data/mgrstart.ini
   chown root.ccfw /opt/ccfw/data/mgrstart.ini
   chmod 664 /opt/ccfw/data/mgrstart.ini
fi

if [ -f $image/inittab ]; then
   cp -p /etc/inittab /tmp/inittab.orig
   cp -f $image/inittab /tmp/inittab
   mv /tmp/inittab /etc/inittab
   chown root.root /etc/inittab
   chmod 644 /etc/inittab
fi

if [ -f $image/hsccredmgr ]; then
   cp -p /opt/hsc/sbin/hsccredmgr /tmp/hsccredmgr.orig
   cp -f $image/hsccredmgr /tmp/hsccredmgr
   mv /tmp/hsccredmgr /opt/hsc/sbin/hsccredmgr
   chown root.root /opt/hsc/sbin/hsccredmgr
   chmod 755 /opt/hsc/sbin/hsccredmgr
fi

if [ -f $image/browserjail ]; then
   cp -p /opt/ccfw/native/runAsRoot/browserjail /tmp/browserjail.orig
   cp -f $image/browserjail /tmp/browserjail
   mv /tmp/browserjail /opt/ccfw/native/runAsRoot/browserjail
   chown root.ccfw /opt/ccfw/native/runAsRoot/browserjail
   chmod 775 /opt/ccfw/native/runAsRoot/browserjail
fi

if [ -f $image/menu ]; then
   cp -p /opt/hsc/data/fluxbox/en_US/menu /tmp/menu.orig
   cp -f $image/menu /tmp/menu
   mv /tmp/menu /opt/hsc/data/fluxbox/en_US/menu
   chown root.root /opt/hsc/data/fluxbox/en_US/menu
   chmod 644 /opt/hsc/data/fluxbox/en_US/menu
fi

if [ -f $image/restoreUpgradeFiles ]; then
   cp -p /opt/hsc/bin/restoreUpgradeFiles /tmp/restoreUpgradeFiles.orig
   cp -f $image/restoreUpgradeFiles /tmp/restoreUpgradeFiles
   mv /tmp/restoreUpgradeFiles /opt/hsc/bin/restoreUpgradeFiles
   chown bin.bin /opt/hsc/bin/restoreUpgradeFiles
   chmod 555 /opt/hsc/bin/restoreUpgradeFiles
fi

if [ -f $image/ibm5250 ]; then
   cp -p /opt/hsc/bin/ibm5250 /tmp/ibm5250.orig
   cp -f $image/ibm5250 /tmp/ibm5250
   mv /tmp/ibm5250 /opt/hsc/bin/ibm5250
   chown bin.bin /opt/hsc/bin/ibm5250
   chmod 555 /opt/hsc/bin/ibm5250
fi

if [ -f $image/rdist-6.1.5-1.i586.rpm ]; then
   rpm --force -Uvh $image/rdist-6.1.5-1.i586.rpm >> $LogFile 2>&1
fi

if [ -f $image/phmc.xml ]; then
   cp -p /opt/ccfw/data/deployment/phmc.xml /tmp/phmc.xml.orig
   cp -f $image/phmc.xml /tmp/phmc.xml
   mv /tmp/phmc.xml /opt/ccfw/data/deployment/phmc.xml
   chown root.ccfw /opt/ccfw/data/deployment/phmc.xml
   chmod 664 /opt/ccfw/data/deployment/phmc.xml
fi

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

if [ -f $image/libhsclogin.so ]; then
   cp -f $image/libhsclogin.so /tmp/libhsclogin.so
   mv /tmp/libhsclogin.so /usr/lib/libhsclogin.so
   chown bin.bin /usr/lib/libhsclogin.so
   chmod 555 /usr/lib/libhsclogin.so
fi

if [ -f $image/commandcontrol ]; then
   cp -p /opt/hsc/bin/commandcontrol /tmp/commandcontrol.orig
   cp -f $image/commandcontrol /tmp/commandcontrol
   mv /tmp/commandcontrol /opt/hsc/bin/commandcontrol
   chown root.root /opt/hsc/bin/commandcontrol
   chmod 4755 /opt/hsc/bin/commandcontrol
fi

if [ -f $image/hsc_bundles.jar ]; then
   cp -p /usr/websm/codebase/pluginjars/hsc_bundles.jar /tmp/hsc_bundles.jar.orig
   cp -f $image/hsc_bundles.jar /tmp/hsc_bundles.jar
   mv /tmp/hsc_bundles.jar /usr/websm/codebase/pluginjars/hsc_bundles.jar
   chown bin.bin /usr/websm/codebase/pluginjars/hsc_bundles.jar
   chmod 555 /usr/websm/codebase/pluginjars/hsc_bundles.jar
fi

cp -f /opt/hsc/data/fluxbox/menu /opt/hsc/data/fluxbox/en_US/menu

if [ -f $image/postUpgrade ]; then
   cp -p /opt/hsc/bin/postUpgrade /tmp/postUpgrade.orig
   cp -f $image/postUpgrade /tmp/postUpgrade
   mv /tmp/postUpgrade /opt/hsc/bin/postUpgrade
   chown bin.bin /opt/hsc/bin/postUpgrade
   chmod 555 /opt/hsc/bin/postUpgrade
fi

# remove setting to CST 
#if [ -f /etc/localtime ]; then
#   rm -f /etc/localtime
#fi

# /tmp/hmc permission
mkdir -p /tmp/hmc
chown -R ccfw.ccfw /tmp/hmc
 
# browser failed to come up
if [ -f $image/hscSystemInfo ]; then
   cp -p /opt/hsc/sbin/hscSystemInfo /tmp/hscSystemInfo.orig
   cp -f $image/hscSystemInfo /tmp/hscSystemInfo
   mv /tmp/hscSystemInfo /opt/hsc/sbin/hscSystemInfo
   chown root.root /opt/hsc/sbin/hscSystemInfo
   chmod 755 /opt/hsc/sbin/hscSystemInfo
fi

if [ -f $image/taskorder.xml ]; then
   cp -p /opt/ccfw/data/ui/bonsai/taskorder.xml /tmp/taskorder.xml.orig
   cp -f $image/taskorder.xml /tmp/taskorder.xml
   mv /tmp/taskorder.xml /opt/ccfw/data/ui/bonsai/taskorder.xml
   chown root.ccfw /opt/ccfw/data/ui/bonsai/taskorder.xml
   chmod 664 /opt/ccfw/data/ui/bonsai/taskorder.xml
fi

if [ -f $image/uireader.jar ]; then
   cp -f $image/uireader.jar /tmp/uireader.jar
   mv /tmp/uireader.jar /opt/ccfw/jars/uireader.jar
   chown root.ccfw /opt/ccfw/jars/uireader.jar
   chmod 664 /opt/ccfw/jars/uireader.jar
fi

/usr/sbin/usermod -G ccfw,root,hmc ccfw 2>/dev/null

# remove /opt/ccfw world write
chmod 775 /opt/ccfw

if [ -f $image/man ]; then
   cp -p /usr/hmcrbin/man /tmp/man.orig
   cp -f $image/man /tmp/man
   mv /tmp/man /usr/hmcrbin/man
   chown bin.bin /usr/hmcrbin/man
   chmod 555 /usr/hmcrbin/man
fi

if [ -f $image/firewall ]; then
   cp -p /opt/ccfw/firewall /tmp/firewall.orig
   cp -f $image/firewall /tmp/firewall
   mv /tmp/firewall /opt/ccfw/firewall
   chown root.ccfw /opt/ccfw/firewall
   chmod 775 /opt/ccfw/firewall
fi

if [ -f $image/updateNetworkSettings.sh ]; then
   cp -p /opt/ccfw/native/runAsRoot/updateNetworkSettings.sh /tmp/updateNetworkSettings.sh.orig
   cp -f $image/updateNetworkSettings.sh /tmp/updateNetworkSettings.sh
   mv /tmp/updateNetworkSettings.sh /opt/ccfw/native/runAsRoot/updateNetworkSettings.sh
   chown root.ccfw /opt/ccfw/native/runAsRoot/updateNetworkSettings.sh
   chmod 775 /opt/ccfw/native/runAsRoot/updateNetworkSettings.sh
fi

if [ -f $image/resourcetables.xml ]; then
   cp -f $image/resourcetables.xml /tmp/resourcetables.xml
   mv /tmp/resourcetables.xml /opt/ccfw/data/ui/bonsai/resourcetables.xml
   chown root.ccfw /opt/ccfw/data/ui/bonsai/resourcetables.xml
   chmod 664 /opt/ccfw/data/ui/bonsai/resourcetables.xml
fi

#ln -s /opt/hsc/bin/commandcontrol /opt/hsc/bin/migrlpar
#ln -s /opt/hsc/bin/commandcontrol /opt/hsc/bin/lslparmigr

#touch /opt/ccfw/unified
#touch /opt/ccfw/noBrowser

# resolve ssh id files installed from CSM rpms
rm -f /home/hscroot/.ssh/id*

#
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

# Temporary patch to install PAM KRB5
if [ -f $image/pam_krb5-*rpm ]; then
  /bin/rpm -ivh $image/pam_krb5-*rpm --force
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

# 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

# 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
# Special install code to update java time zone
if [ -f $image/jtzu.zip ]; then
   echo "Updating Java Timezone" >> $LogFile
   if [ -d /tmp/jtzu ]; then
      rm -rf /tmp/jtzu
   fi
   mkdir -p /tmp/jtzu
   cd /tmp/jtzu
   unzip -o -q $image/jtzu.zip
   sh /tmp/jtzu/runjtzu.sh 2>&1 >> $LogFile
   rm -rf /tmp/jtzu
fi

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