#!/bin/sh
#-------------------------------------------------------------------------------
# restoreUpgradeFiles.sh
# 
# This shell script is invoked to perform a restore of previously saved upgrade
# data files.  It will be executed every time the system reboots.  If no
# "indicator" file is present, the script exits and no file restoration is done.
# If the indicator file is detected, a Java environment is established, and the
# actual class file to process the prior saved data is executed.
# 
# Usage: restoreUpgradeFiles
#
# Return Codes:
# 1 - Error accessing required save update data logging directory
# 2 - Media is write-protected
# 4 - Error mounting the media
# 6 - Error unmounting the media
# 7 - Generic other error(?)

#---------------------------------------------------------------------------------
# directory and filename which records the save upgrade data actions.
#---------------------------------------------------------------------------------
LOGDIR=/var/hsc/log
LOG=$LOGDIR/restoreUpgradeFiles.log

LOG_ERROR_LOG=/tmp/restoreUpgradeFiles.log

# STDIO directed to /tmp/mount_output + PID
MOUNT_OUTPUT=/tmp/mount_output$$

      
# common exit point for script
exit_cleanup() {
    rm -f $MOUNT_OUTPUT

    # 472655: Restore/merge the cim.properties file for restore operation.
    if [ "$1" = "0" ]; then
        sed -e '/^ *SocketProvider *=/d' /opt/hsc/data/cimomcfg.bak > /tmp/cimomcfg_bak
        grep -e '^ *SocketProvider *=' /opt/hsc/data/cim.properties > /tmp/cimcfg_socket
        rm -f /opt/hsc/data/cim.properties
        cat /tmp/cimomcfg_bak /tmp/cimcfg_socket > /opt/hsc/data/cim.properties
        rm -f /tmp/cimomcfg_bak /tmp/cimcfg_socket /opt/hsc/data/cimomcfg.bak
        echo "Merged cim.properties from files before and after restoration." >> $LOG
    else
        echo "Restore data has error ($1)." >> $LOG
        mv -f /opt/hsc/data/cimomcfg.bak /opt/hsc/data/cim.properties
        echo "Rolled back the cim.properties to before restoring data." >> $LOG
    fi
    # end 472655.

    exit $1
}  


#-------------------------------------------------------------------------------
# the (hard drive) media mount point to which the save upgrade data is to be
# stored.  There should be an entry for this mountpoint in /etc/fstab
#-------------------------------------------------------------------------------
UPGRADE_MOUNTPOINT=/mnt/upgrade

#-------------------------------------------------------------------------------
# the restore indicator file
#-------------------------------------------------------------------------------
INDICATOR_FILE=$UPGRADE_MOUNTPOINT/doRestore
#-------------------------------------------------------------------------------
# the upgrade indicator file
#-------------------------------------------------------------------------------
UPGRADE_INDICATOR_FILE=$UPGRADE_MOUNTPOINT/upgrade.dat


#-------------------------------------------------------------------------------
# the mount point for the media to which the save upgrade data is to be stored.
# A corresponding device entry should exist for this mount point in /etc/fstab
# Note: even though we're using the DVD, the 'auto' option in /etc/fstab will
#       figure out that it's not a CDROM device, but actually a DVD
#-------------------------------------------------------------------------------
#REMOVABLE_MOUNTPOINT=/media/cdrom



#-------------------------------------------------------------------------------
# Check if the directory for the log file exists.
#-------------------------------------------------------------------------------
if [ ! -d $LOGDIR ] ; then
   echo "=================================================================" > $LOG_ERROR_LOG
   echo -e "Restore Upgrade Data log for `date`." >> $LOG_ERROR_LOG
   echo "Restore upgrade data log directory, <$LOGDIR>, does not exist. Program exiting." >> $LOG_ERROR_LOG
   exit_cleanup 1
fi


#-------------------------------------------------------------------------------
# Start a new log to record save upgrade data actions.
#-------------------------------------------------------------------------------
echo "=================================================================" > $LOG
echo -e "Restore Upgrade Data log for `date`." >> $LOG
echo >> $LOG

# 472655: save the cim.properties file before restore.
cp -pf /opt/hsc/data/cim.properties /opt/hsc/data/cimomcfg.bak
echo -e "Save the cim.properties file to cimomcfg.bak before restoring." >> $LOG
# end 472655

#-------------------------------------------------------------------------------
#  Set the output media mount point variable and process accordingly
#   Initially only restore from hard disk, later possibly, restore from DVD
#-------------------------------------------------------------------------------
MEDIA_TYPE=0
if test "$MEDIA_TYPE" = "0" ; then

   MOUNTPOINT=$UPGRADE_MOUNTPOINT

   #----------------------------------------------------------------------------
   # Mount the media. Fixed disk in this case, /dev/hda2 possibly.
   #----------------------------------------------------------------------------
   mount -v $UPGRADE_MOUNTPOINT > $MOUNT_OUTPUT 2>&1
   if [ $? -ne 0 ] ; then

      #-------------------------------------------------------------------------
      # write protect errors?  This shouldn't happen if restore fom hard disk
      #-------------------------------------------------------------------------
      if grep "write-protected" $MOUNT_OUTPUT; then
         echo "The mountpoint, $UPGRADE_MOUNTPOINT, is write protected" >> $LOG
         exit_cleanup 2
      fi

      #-------------------------------------------------------------------------
      # already mounted?  Not an error, but a non-zero return code.  Continue
      #-------------------------------------------------------------------------
      if grep "already mounted" $MOUNT_OUTPUT; then
         echo "The mountpoint, $UPGRADE_MOUNTPOINT, is already mounted.  Continuing..." >> $LOG

      #-------------------------------------------------------------------------
      # other error?  Game over.
      #-------------------------------------------------------------------------
      else
         echo "The mountpoint, $UPGRADE_MOUNTPOINT, cannot be mounted.  Error is $?" >> $LOG
         exit_cleanup 4
      fi
      
   fi

   #----------------------------------------------------------------------------
   # good mount of output device, continue
   #----------------------------------------------------------------------------
   echo "Upgrade partition mounted at $UPGRADE_MOUNTPOINT." >> $LOG
   #----------------------------------------------------------------------------
   # Test for the existence of the "upgrade.dat' indicator file. If this file is
   # present, continue on with the restore process.
   #----------------------------------------------------------------------------

   if [ ! -e $UPGRADE_INDICATOR_FILE ]; then
   #----------------------------------------------------------------------------
   #  Unmount and exit.
   #----------------------------------------------------------------------------
      umount -v $UPGRADE_MOUNTPOINT > $MOUNT_OUTPUT 2>&1
      if [ $? -ne 0 ] ; then

         echo "Can't unmount the $UPGRADE_MOUNTPOINT volume, rc = $?." >> $LOG
         exit_cleanup 6

      else
         echo "$UPGRADE_MOUNTPOINT volume unmounted successfully." >> $LOG
         echo "No prior Upgrade performed, upgrade data not restored." >> $LOG
         exit_cleanup 0
      fi
   fi

   #----------------------------------------------------------------------------
   # Test for the existence of the "doRestore' indicator file.  If this file is
   # present, continue on with the restore process.
   #----------------------------------------------------------------------------
   if [ -e $INDICATOR_FILE ]; then
   
      SAVED_CLASSPATH=$CLASSPATH
      
      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

      CLASSPATH=${DEBUG_JARS_DIRECTORY}:${debug_jars}:/usr/websm/codebase/pluginjars/hsc.jar:/usr/websm/codebase/pluginjars/hsc_bundles.jar:/usr/websm/codebase/wsm.jar:/usr/websm/codebase/pluginjars/sniacimom.jar:/opt/ccfw/ccfw.jar
      
    # Any pre-configuration tasks to be executed first?

      if [ -f /opt/hsc/bin/preUpgrade ]; then
          /opt/hsc/bin/preUpgrade 2>/dev/null
      fi

      #-------------------------------------------------------------------------
      # account for ALL svcagent jar files
      #-------------------------------------------------------------------------
      for f in /usr/svcagent/lib/*.jar
      do
         if [ -f $f ]; then
            CLASSPATH=$CLASSPATH:$f
         fi
      done
      CLASSPATH=$CLASSPATH:$SAVED_CLASSPATH
      
      JAVAPATH="/opt/IBMJava/jre/bin"
      export CLASSPATH
      export PATH=$JAVAPATH:$PATH

      #-------------------------------------------------------------------------
      # Launch the Java program which iterates through the list of restore
      # scripts
      #-------------------------------------------------------------------------
      echo "Issuing command to restore the saved files" >> $LOG
      
      # New hack. Despite saving the /etc/httpd/conf/http.conf file, we
      # now never want to restore it.  Stash the new version file just re-installed
      # from corrective service install away, then restore it after restore upgrade
      # processing is complete. 8/7/03 - SLF
      if [ -f /etc/httpd/conf/httpd.conf ]; then
         cp -pf /etc/httpd/conf/httpd.conf /tmp/httpd.conf
      fi
      
      javaw -DCONSOLE_PATH=/opt/ccfw/ com.ibm.hmc.common.upgrade.RestoreUpgradeData
      RESTORE_RC=$?
      
      # 8/7/03 hack from above, continued.  Now restore the httpd.conf file
      if [ -f /tmp/httpd.conf ]; then
         mv -f /tmp/httpd.conf /etc/httpd/conf
      fi
      
      
      echo "Restore file processing completed with rc = $RESTORE_RC." >> $LOG

      CLASSPATH=$SAVED_CLASSPATH
      export CLASSPATH
      
      #-------------------------------------------------------------------------
      # Remove the old saveUpgradeLog file and the indicator file only if there
      # was a successful completion
      #-------------------------------------------------------------------------
      if [ $RESTORE_RC -eq 0 ]; then
      
         rm -f $LOGDIR/saveUpgradeData.log
         rm -f $INDICATOR_FILE
	 rm -f $UPGRADE_INDICATOR_FILE
      
         #
         # Additional cleanup - there is the possibility that the directories under /home
         # (all the users' dirs) may have been re-generated via this restore process.
         # Unfortunately, if the directory itself was not part of the archive, it will
         # have been created with uid/gid of root/root as part of un-taring the actual
         # files in those directories. This is not good since the users will not be able
         # to access their home dirs! Change ownership of those subdirectories to the
         # corresponding userID.
         #
         for user in `echo /home/*`
         do
             chown ${user##/home/} $user
             
             # skip .ssh subdirectory since root ownership is the way it is originally installed as
             find $user -name "*" -type d ! -path $user/.ssh -exec chown ${user##/home/} {} \;
         done
	 /usr/bin/chage -M 99999 hscroot 2>&1 >/dev/null
	 if [ -f /etc/dhcpd.conf ]
	 then
	 # If the lease time to see if it is set to 7200. If so
	 # set to infinite.
	     /usr/bin/grep -q "max-lease-time infinite" /etc/dhcpd.conf
	     if [ $? -ne 0 ]
	     then
		/usr/bin/sed -e 's/max-lease-time[ ]*7200/max-lease-time infinite/g' /etc/dhcpd.conf > /tmp/_new_dhcpd.conf
		mv /tmp/_new_dhcpd.conf /etc/dhcpd.conf
	     fi
	fi
	# Incompatible xinetd.conf defect 515818
	grep -q "RECORD" /etc/xinetd.conf
	if [ $? -eq 0 ]; then
	   /usr/bin/sed -e 's/RECORD//g' /etc/xinetd.conf > /tmp/_xinetd_conf_
           mv /tmp/_xinetd_conf_ /etc/xinetd.conf
	fi

	# Fix /etc/dhclient.conf issue defect 526725

	grep -q "require subnet-mask, domain-name-servers;" /etc/dhclient.conf
	if [ $? -eq 0 ]; then
	   /usr/bin/sed -e 's/require subnet-mask, domain-name-servers;/require subnet-mask;/g' /etc/dhclient.conf > /tmp/_dhclient_conf
           mv /tmp/_dhclient_conf /etc/dhclient.conf
	fi
	# Migration to new restricted shell setup

	/opt/hsc/bin/migrateToHmcBash
	# Make sure keyboard mapping is restored correctly
	/opt/hsc/bin/restoreKbdCfg

	# Post configuration processing
        if [ -f /opt/hsc/bin/postUpgrade ]; then
           /opt/hsc/bin/postUpgrade 2>/dev/null
        fi

	
      fi

   else
      echo "Indicator to restore saved upgrade data not found. No files restored." >> $LOG
      
   fi
   

   /opt/hsc/bin/httpClientUpdate
   #----------------------------------------------------------------------------
   #  Completed the restore.  Unmount and exit.
   #----------------------------------------------------------------------------
   umount -v $UPGRADE_MOUNTPOINT > $MOUNT_OUTPUT 2>&1
   if [ $? -ne 0 ] ; then

      echo "Can't unmount the $UPGRADE_MOUNTPOINT volume, rc = $?." >> $LOG
      exit_cleanup 6

   else
      echo "$UPGRADE_MOUNTPOINT volume unmounted successfully." >> $LOG
      echo "Successful completion of restore of upgrade data." >> $LOG
      exit_cleanup 0
   fi

fi
