#!/bin/bash
# chkconfig: 12345 07 91
# description: This calls the HMC restore scripts (prior to network set up)
#

### BEGIN INIT INFO
# Provides: hmcRestore
# Required-Start: $local_fs
# Required-Stop:
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
# Short-Description: Provide HMC Restore
# Description: Restore Upgrade and Critical Data
### END INIT INFO
# Source function library.
. /etc/rc.status
rc_reset
# Critical error messages. Keep them under 80 chars per line
ARCHIVE_ERR_MSG_0="A critical error occurred during the recovery of archive data."
ARCHIVE_ERR_MSG_1="The system may be unstable. It is recommended the HMC be"
ARCHIVE_ERR_MSG_2="re-installed and manually reconfigured.\n"
ARCHIVE_ERR_MSG_3="Contact your service representative to determine the severity"
ARCHIVE_ERR_MSG_4="of this condition if you choose to proceed. Press <Enter> to"
ARCHIVE_ERR_MSG_5="continue the boot sequence or press 'R', then <Enter> to"
ARCHIVE_ERR_MSG_6="re-install the HMC.\n"

REINSTALL_MSG="Insert the HMC recovery media and press <Enter> to continue with re-installation."

NO_RESTORE_MSG_0="A non-critical error was encountered while recovering archive data."
NO_RESTORE_MSG_1="No archive files have been restored. Contact your service"
NO_RESTORE_MSG_2="representative for further analysis. Press <Enter> to continue"
NO_RESTORE_MSG_3="the boot sequence."

case "$1" in
    start)
        # remove any prior backup task lock
        lockfilename="/opt/hsc/data/backuphdr.lck"
        if [ -d $lockfilename ]; then
            rmdir $lockfilename
        fi

        # Logging information
        LOGDIR=/var/hsc/log
        FINALLOG=$LOGDIR/hmcRestore.log
        LOG=$LOGDIR/hmcRestore.tmp
        LOG_ERROR_LOG=/tmp/hmcRestore.log

        # Check if the directory for the log file exists.
        if [ ! -d $LOGDIR ]; then
            echo "=================================================================" > $LOG_ERROR_LOG
            echo "HMC restore script processing log for `date`." >> $LOG_ERROR_LOG
            echo "HMC log directory, <$LOGDIR>, does not exist. Program exiting" >> $LOG_ERROR_LOG
            exit 1
        fi


        #-----------------------------------------------------------------------
        # Something to the screen while booting
        #-----------------------------------------------------------------------
        echo "Processing HMC archive files ..."

        # Disable power saving for now
        /usr/bin/setterm -powersave off
        /usr/bin/setterm -blank 0

        #-----------------------------------------------------------------------
        # Start log to HMC restore actions.
        #-----------------------------------------------------------------------
        if [ ! -f $LOG ]; then
           echo "--------- start log --------" > $LOG
        else
           echo "--------- log resumed after reboot --------" >> $LOG
        fi
        echo -e "HMC restore processing log for `date`.\n" >> $LOG


        #-----------------------------------------------------------------------
        # Call the actual data restore processes
        #-----------------------------------------------------------------------
        /bin/sh /opt/hsc/bin/restore 2>/dev/null
        restoreRC=$?
        echo "system restore of backup critical data completed, rc=$restoreRC." >> $LOG

        case "$restoreRC" in 
            0) 
                 # Successful restore of backup data was done.  Force a warm 
                 # reboot since we do not know what, if any, critical files 
                 # may have been copied back onto the system that was just 
                 # re-loaded.
                 #
                 # An rc=0 indicated that an actual restore was done. Rc=12 
                 # states that we attampted to do a restore, but none was 
                 # necessary. In between rc's are various errors.
                 #
                 # Use 'reboot' since it's unclear whether or not doing a 
                 # shutdown will attempt a level switch and execution of 'kill' 
                 # scripts.
                 #
                 /bin/sync

                 #
                 # Do any post-configuration tasks have to be executed after the (DVD) restore task has run?
                 #
                 if [ -f /opt/hsc/bin/postRestore ]; then
                     /opt/hsc/bin/postRestore 2>/dev/null
                 fi

                 /sbin/reboot -f

                 rc_exit
                 ;;

            99) 
                 # This is bad. It indicates that during the restore of the 
                 # backup data there was a 'tar' error during processing.  The 
                 # condition *may* now exist on the system where we have 
                 # incompatible executables and libraries, but maybe not - no 
                 # way to tell.  Prompt user with continuation options.
                 /opt/hsc/sbin/UsbConfig
                 /etc/init.d/hotplug start
                 /etc/init.d/kbd start

                 echo -e "\n\n\n"
                 echo -e $ARCHIVE_ERR_MSG_0
                 echo -e $ARCHIVE_ERR_MSG_1
                 echo -e $ARCHIVE_ERR_MSG_2
                 echo -e $ARCHIVE_ERR_MSG_3
                 echo -e $ARCHIVE_ERR_MSG_4
                 echo -e $ARCHIVE_ERR_MSG_5
                 echo -e $ARCHIVE_ERR_MSG_6

                 # Hold processing until user acknowledges msg
                 # line is /dev/pts/2
                 line=$(tty)
                 while read input;
                 do
                      # <Enter> key appears to get us out of loop
                      break
                 done

                 #
                 # Users assumes all risks with continuing with the boot
                 # process. They have been warned of potential instability.
                 #
                 if [[ "$input" == "R" ||  "$input" == "r" ]]; then
                      #
                      # Prompt with additional message to re-install product
                      #
                      echo -e $REINSTALL_MSG

                      # Hold processing until user acknowledges msg
                      line=$(tty)
                      while read input;
                      do
                           # <Enter> key appears to get us out of loop
                           break
                      done

                      # Reboot assuming the recovery CD has been re-inserted
                      /sbin/reboot -f

                      rc_exit
                 else
                      # User has been warned.  Force the reboot
                      /bin/sync

                      #
                      # Do any post-configuration tasks have to be executed after the (DVD) restore task has run?
                      #
                      if [ -f /opt/hsc/bin/postRestore ]; then
                          /opt/hsc/bin/postRestore 2>/dev/null
                      fi

                      /sbin/reboot -f

                      rc_exit
                 fi
                 ;;

            1|2|4|5|6|8|9|10|11|13|14|15) 
                 # Come to think of it now, we should indicate a non-critcial 
                 # error occurred ('mount' failure, media error, no data, etc.) 
                 # and no restore was done. Again, direct them to the error log 
                 # and present continuation options.

                 /etc/init.d/kbd start
                 echo -e "\n\n\n"
                 echo -e $NO_RESTORE_MSG_0
                 echo -e $NO_RESTORE_MSG_1
                 echo -e $NO_RESTORE_MSG_2
                 echo -e $NO_RESTORE_MSG_3

                 # Hold processing until user acknowledges msg
                 line=$(tty)
                 while read input;
                 do
                      # <Enter> key appears to get us out of loop
                      break
                 done
		  rc_status -v
                 ;;
            *) 
                 ;;
        esac


        #
        # Now do the restore of upgrade data files, if *both* indicator files re present
        #
        /bin/sh /opt/hsc/bin/restoreUpgradeFiles 2>/dev/null
        echo "system restore of upgrade files completed, rc = $?." >> $LOG


        # defect 573290 - removed recursive chown of ccfw subdirs 10/9/06, SLF 
#        #-----------------------------------------------------------------------
#        # Regardless if the 'ccfw' userid existed before/after either restore,
#        # there is the possibility that the opt/ccfw directory ownership will
#        # be corrupt (different GID).
#        #-----------------------------------------------------------------------
#        chown -R root.ccfw /opt/ccfw

        # defect 532659 - special case the permissions/ownership of the 'vr' directory. Note that
        # this directory is dynamically created and is not part of the product installation
#        if [ -d /opt/ccfw/data/vr ]; then
#            chmod 775 /opt/ccfw/data/vr
#            chown ccfw.ccfw /opt/ccfw/data/vr
#        fi
#        if [ -d /opt/ccfw/data/pa/dumps ]; then
#            chmod 775 /opt/ccfw/data/pa/dumps
#            chown ccfw.ccfw /opt/ccfw/data/pa/dumps
#        fi
#        if [ -d /opt/ccfw/data/pa/fedc ]; then
#            chmod 775 /opt/ccfw/data/pa/fedc
#            chown ccfw.ccfw /opt/ccfw/data/pa/fedc
#        fi
#        if [ -d /opt/ccfw/data/pa/fedc/eed ]; then
#            chmod 775 /opt/ccfw/data/pa/fedc/eed
#            chown ccfw.ccfw /opt/ccfw/data/pa/fedc/eed
#        fi

        # more thorough way of accomplishing the above permission mods
        if [ -f /opt/ccfw/iqzddfc.trm ]; then
            x=/opt/ccfw/iqzddfc.trm
            while read line
            do
                # first, operate against only non-comment lines (#) in the framework file...
                if echo "$line" | grep -qv "^#"; then
                    # if not a comment line and line ends with a 'C' char, this is a potential
                    # candidate for directory permission modification after restore of data...
                    if echo "$line" | grep -q " C$"; then

                        # 2nd column of data in the file is directory for ccfw to dynamically create if needed
                        y=`echo "$line" | cut -f2 -d ' '`

                        # starting wildcard char "*" is indication of relative (to /opt/ccfw) path.
                        # Bypass the non-relative directory entries
                        if echo "$y" | grep -q "^*"; then
                            # change the "*" char to the actual /opt/ccfw (non-relative, installed) directory
                            z=`echo "$y" | sed -e 's/\*/\/opt\/ccfw/g'`

                            # now change the directory permisssions
                            if [ -d $z ]; then
                                chmod 775 $z
                                chown ccfw.ccfw $z

                                echo "directory permissions updated for $z" >> $LOG
                            fi
                        fi
                    fi
                fi
            done < $x
        fi

        # This is for MM use only....
        if [ -f /opt/hsc/data/hmcType.properties ]; then
            y=`cat /opt/hsc/data/hmcType.properties`
            if [ $y = 'HMCTYPE=essHMC' ]; then
                if [ -d /extra/data/vr ]; then
                    chmod 775 /extra/data/vr
                    chown ccfw.ccfw /extra/data/vr
                fi
                if [ -d /extra/data/pa/dumps ]; then
                    chmod 775 /extra/data/pa/dumps
                    chown ccfw.ccfw /extra/data/pa/dumps
                fi
                if [ -d /extra/data/pa/fedc ]; then
                    chmod 775 /extra/data/pa/fedc
                    chown ccfw.ccfw /extra/data/pa/fedc
                fi
                if [ -d /extra/data/pa/fedc/eed ]; then
                    chmod 775 /extra/data/pa/fedc/eed
                    chown ccfw.ccfw /extra/data/pa/fedc/eed
                fi
            fi
        fi

        # This is for data utilization ....
        if [ -d /opt/hsc/data/utilization ]; then
            chmod 775 /opt/hsc/data/utilization/*
            chown ccfw.ccfw /opt/hsc/data/utilization/*
        fi

        # defect 573290 - removed password migration 10/9/06, SLF 
#        #-----------------------------------------------------------------------
#        # Migrate password
#        #-----------------------------------------------------------------------
#        JAVAPATH="/opt/IBMJava/jre/bin"
#        export LD_LIBRARY_PATH=/usr/sbin/rsct/lib:/usr/lib:/opt/hsc/lib:$LD_LIBRARY_PATH
#        export PATH=$JAVAPATH:$PATH
#
#        x=sslite-us.zip:sslite-ex.zip:jcb.jar:images.jar:wsmextra.jar:jcchart.jar:jcswingsuite.jar:/usr/websm/codebase/taskguide:mm_snmp.jar:wsm.jar:habeans.jar:log.jar:
#        for i in /usr/websm/codebase/pluginjars/*.jar
#        do
#           x=${x}pluginjars/`basename $i`:
#        done
#
#        if [ "${DEBUG_JARS_DIRECTORY}" != "" ] ; then
#          if [ -d ${DEBUG_JARS_DIRECTORY} ] ; then
#            for i in ${DEBUG_JARS_DIRECTORY}/*.jar
#            do
#               debug_jars=${debug_jars}:$i
#            done
#          fi
#        fi
#
#        export CUSTOM_CLASSPATH=${DEBUG_JARS_DIRECTORY}:${debug_jars}:/usr/websm/codebase/pluginjars/aca.jar:/usr/websm/codebase/images:/usr/websm/codebase/pluginjars/hsc_${LANG}.jar:/usr/websm/codebase/pluginjars/wsmhscsecurity.jar:/usr/websm/codebase/pluginjars/hsc.jar:/usr/websm/codebase/pluginjars/hsc_bundles.jar:/opt/hsc/auiml/properties:/usr/websm/codebase:/opt/hsc/com/ibm/hsc/auiml/databeans/images:$x
#        $JAVAPATH/java -cp $CUSTOM_CLASSPATH com.ibm.hsc.common.util.HscPasswordMgr hscroot
#        if [ $? -eq 0 ]
#        then
#           rm -f /opt/hsc/data/passwords
#        fi
#
#        echo "completed hscroot password migration." >> $LOG


        #-----------------------------------------------------------------------
        # Reset the hostname since a change could have occurred after doing
        # a restore or restore of upgrade data
        #-----------------------------------------------------------------------
        HOSTNAME=`/bin/hostname`
        if [ -f /etc/HOSTNAME ]; then
            HOSTNAME=`cat /etc/HOSTNAME`
        fi
        echo "current hostname is: $HOSTNAME." >> $LOG

        if [ -z "$HOSTNAME" -o "$HOSTNAME" = "(none)" ]; then
           HOSTNAME=localhost
        fi

        # Reset the hostname
        echo "Resetting hostname to ${HOSTNAME}"
        hostname ${HOSTNAME}
        echo "new hostname is: $HOSTNAME." >> $LOG


        chmod 644 $LOG
        mv -f $LOG $FINALLOG

        # re-enable power saving
        /usr/bin/setterm -powersave on
        /usr/bin/setterm -blank 60
        ;;

    stop)
        ;;
    *)

    echo "Usage: hmcRestore {start|stop}"
    exit 1
    ;;
esac

rc_exit

