#!/bin/sh

#-------------------------------------------------------------------------------
# restore.sh
# 
# This shell script is invoked to perform a restore of the HSC backup data.
# This script should be executed every time the system reboots.  If no
# "indicator" file is present, the script exits and no file restoration is done.
# 
# Usage: 'restore'
#
# Return Codes:
# 1 - Error mounting the upgrade partition
# 2 - Error Copying from the media
# 3 - Archive not present
# 4 - Error unmounting the media
# 5 - Other Errors
# 6 - DVD drive failure
# 7 - No media inserted
# 8 - Unformatted media
# 9 - Incorrect label on media
# 10 - tar error extracting relative to working directory
# 11 - unable to create staging directory
# 12 - no need to run a restore
# 13 - unable to generate the excluded shared object file list
# 14 - tar error extracting non-*.so files relative to / dir
# 15 - staged file move/copy error
# 16 - HmcBase file miscompare (incompatible archive)
# 18 - unable to query the archive file (tar -tzvf failure)

#-------------------------------------------------------------------------------
# directory and filename which records the backup actions.
#-------------------------------------------------------------------------------
LOGDIR=/var/hsc/log
LOG=$LOGDIR/restore.log

LOG_ERROR_LOG=/tmp/restore.log
ARCHIVE_LOG=/tmp/archive.log

# stdout of 'mount' command directed to /tmp/mount_output+PID
MOUNT_OUTPUT=/tmp/mount_output$$

#-------------------------------------------------------------------------------
# mount point: the mount point where the backup data is contained
#-------------------------------------------------------------------------------
MOUNTPOINT=/media/cdrom

#-------------------------------------------------------------------------------
# location on the HMC where a remote archive file can be found
#-------------------------------------------------------------------------------
REM_ARCHIVE_LOCATION=/dump

#-------------------------------------------------------------------------------
# the filename of the backup archive
#-------------------------------------------------------------------------------
ARCHIVENAME=backuphdr.tgz
ARCHIVE=$MOUNTPOINT/$ARCHIVENAME

#-------------------------------------------------------------------------------
# the (hard drive) media mount point where the indicator file, created by the
# CD re-install process, is stored.  There should be an entry for this
# mountpoint in /etc/fstab.  Also set the Linux kernel mount point location
#-------------------------------------------------------------------------------
UPGRADE_MOUNTPOINT=/mnt/upgrade

#-------------------------------------------------------------------------------
# the indicator file. If it exists, execute 'tar' cmd to restore the backup data
#-------------------------------------------------------------------------------
INDFILE=iqybcrit.dat
REMOTE_INDFILE=rmtbcrit.dat
IQYBCRIT=$UPGRADE_MOUNTPOINT/$INDFILE
RMTBCRIT=$UPGRADE_MOUNTPOINT/$REMOTE_INDFILE

#-------------------------------------------------------------------------------
# Initialize internal script variables used for archive restore type
#-------------------------------------------------------------------------------
USE_REMOTE_BACKUP=0
USE_LOCAL_BACKUP=0

#-------------------------------------------------------------------------------
# Flag to indicate a critical error has occurred during 'tar' file processing
#-------------------------------------------------------------------------------
ARCHIVEFAIL=0

#-------------------------------------------------------------------------------
# Flags/variables used for error processing
#-------------------------------------------------------------------------------
let CP_INDICATOR=0
let MV_INDICATOR=0
 
#
# common exit point for script
#
exit_cleanup() {
    rm -f $MOUNT_OUTPUT

    # Cleanup any staged *.so/misc files in the working area
    rm -fr $WORKING_DIR

    
    if [ "$USE_REMOTE_BACKUP" = "1" ]; then
        rm -f $RMTBCRIT
        rm -f $REM_ARCHIVE_LOCATION/HMCBackup*
        echo "remote archive indicator file and archive file have been removed from this HMC" >> $LOG
    fi
    if [ "$USE_LOCAL_BACKUP" = "1" ]; then
        /sbin/hdparm -d0 $DVD_DEV >/dev/null 2>&1
    fi
    
    umount $UPGRADE_MOUNTPOINT

    if [ $ARCHIVEFAIL -ne 0 ]; then
        echo "possible critical archive restore failure!" >> $LOG
        echo "archive failure error code is: $ARCHIVEFAIL" >> $LOG
        echo "task completed on `date`, return status = 99" >> $LOG
        exit 99
    else
        echo "task completed on `date`, return status = $1" >> $LOG
    fi

    exit $1
}  



#-------------------------------------------------------------------------------
# calculate the checksum of archive file and current file
#-------------------------------------------------------------------------------
function doChecksum {

    funcRC=0
        
    echo "calculating checksum of archive file, $1 ..." >> $ARCHIVE_LOG
    x=`/usr/bin/sum $1`
    echo "...archive file checksum complete, value is --> <$x>, timestamp is `date`." >> $ARCHIVE_LOG
    
    echo "calculating checksum of current file, $2 ..." >> $ARCHIVE_LOG
    y=`/usr/bin/sum $2`
    echo "...current file checksum complete, value is --> <$y>, timestamp is `date`." >> $ARCHIVE_LOG
    
    if [ "$x" == "$y" ]; then
        # all is well, files match
        funcRC=0;
    else
        # miscompare
        funcRC=9;
    fi
    
    echo "Exiting verifyChecksum, funcRC = $funcRC." >> $ARCHIVE_LOG
    return $funcRC
}  



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


# Just in case we have NLS troubles reading system information...
LANG=en_US
export LANG



#-------------------------------------------------------------------------------
# Start log to record restore actions. Keep a few archive copies since multiple
# reboots may be done and this current log file will be lost
#-------------------------------------------------------------------------------
if [ -f $LOG.2 ]; then
    mv $LOG.2 $LOG.3
fi
if [ -f $LOG.1 ]; then
    mv $LOG.1 $LOG.2
fi
if [ -f $LOG ]; then
    mv $LOG $LOG.1
fi
echo -e "Restore script program start - log for `date`\n" > $LOG

#-------------------------------------------------------------------------------
# Mount the media where the indicator file should be located
#-------------------------------------------------------------------------------
mount -v $UPGRADE_MOUNTPOINT > $MOUNT_OUTPUT 2>&1
mountRC=$?
if [ $mountRC -ne 0 ]; then

    #----------------------------------------------------------------------------
    # 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 mount error?  Game over.
    #----------------------------------------------------------------------------
    else
        echo "The mountpoint, $UPGRADE_MOUNTPOINT, cannot be mounted. Error is $mountRC" >> $LOG
        cat $MOUNT_OUTPUT >> $LOG
        exit_cleanup 1
    fi
fi

#-------------------------------------------------------------------------------
# good mount of output device, continue
#-------------------------------------------------------------------------------
echo "partition with indicator file, $INDFILE, successfully mounted at $UPGRADE_MOUNTPOINT..." >> $LOG


#-------------------------------------------------------------------------------
# The initial check is for the indicator file that a remote restore was was
# initiated. If this flag is set, then process the "HMCBackup_yyyymmdd.hhmmss"
# file which should now be located in the /dump directory.  Otherwise, check
# next for the HMC indicator file which says that a restore from DVD is to be
# done.
#-------------------------------------------------------------------------------
if [ ! -e $RMTBCRIT ]; then
    echo "$RMTBCRIT indicator file does not exist. Remote data restoration not requested, continuing..." >> $LOG
    
    # See if a "DVD" restore is required.  This flag should only be set after an HMC
    # clean install (F8/F1).
    if [ ! -e $IQYBCRIT ]; then
        echo "$IQYBCRIT indicator file does not exist. Data restoration not required" >> $LOG
        echo -e "restore script exiting.\n" >> $LOG
        
        # excellent - no HMC restore required.  Exit program
        exit_cleanup 12
    else
        # ok, let's mark that we want to restore the system - the hard way...
        USE_LOCAL_BACKUP=1
    fi
else
    # Ah ha! We will be restoring the system from a remote archive file
    USE_REMOTE_BACKUP=1      
fi



#-------------------------------------------------------------------------------
# Mount the DVD media, if required
#-------------------------------------------------------------------------------
if [ "$USE_LOCAL_BACKUP" = "1" ]; then

    #-------------------------------------------------------------------------------
    # Check to see if the backup media (DVD) is already mounted
    #-------------------------------------------------------------------------------
    if grep -q "$MOUNTPOINT" /etc/mtab; then
        # Media is already mounted, unmount in preparation for (re-)'mount' cmd
        if ! umount -v $MOUNTPOINT >> $LOG 2>&1; then
            echo "Couldn't umount the media... exiting" >> $LOG
            exit_cleanup 4
        fi
    fi

    #-------------------------------------------------------------------------------
    # Mount and check for existence of archive
    #-------------------------------------------------------------------------------
    # specifically use UDF, do not rely on filesystem "guessing" or /etc/fstab content
    if mount -t udf -v /dev/cdrom /mnt/cdrom > $MOUNT_OUTPUT 2>&1; then
        echo "successfully mounted the backup media, continuing..." >> $LOG

#        # leave this test out since the HMC does not do a specific DVD format
#        if !(chkudf /dev/hdc | grep -q -i "ACTBKP" 2>&1); then # udf/dvd specific
        false=0
        if false; then
            echo "The media has an incorrect label... exiting" >> $LOG
            umount $MOUNTPOINT
            exit_cleanup 9
        elif [ ! -f $ARCHIVE ]; then
            echo "The backup archive is not present on the restore media... exiting" >> $LOG
            umount $MOUNTPOINT
            exit_cleanup 3
        fi
        
        # archive file on DVD media check passed
        echo "The backup archive file was detected on DVD media." >> $LOG
    else
        echo "Could NOT mount the DVD media. Resolving..." >> $LOG

        # Check for no media inserted. If this is the case, we're going to remove the
        # iqybcrit.dat file.  That file should never remain on the system unless the
        # product recovery CD was just used. The last thing the recovery CD process
        # does is prompt to insert the backup archive media.  Hence, if no media, no
        # need for recovery indicator flag.
        #
        # Using 'grep' here seems kinda unnecessary/risky. We should just key off
        # 'mount' cmd return codes instead...

        if grep -q -i "wrong major or minor number" $MOUNT_OUTPUT; then
            echo -e "Failure with major/minor signature on the DVD RAM drive. Exiting now, rc=6...\n" >> $LOG
            exit_cleanup 6
        elif grep -q -i "No medium found" $MOUNT_OUTPUT; then
            echo -e "No media in the DVD RAM drive. Exiting now, rc=7\n" >> $LOG

            # Here's one place where we'll remove the indicator file. This
            # will hopefully catch the manufacturing process issue whereby
            # they are installing the pHMCs from the recovery CD, which
            # contains the indicator file, during the initial HMC install.
            # That file should not initially be on the system.  The 'no media'
            # return code from 'mount' cmd is 32.
            rm -f $IQYBCRIT

            exit_cleanup 7
        elif grep -q -i "wrong fs type" $MOUNT_OUTPUT; then
            echo -e "Wrong filesystem type, or unformatted media, or wrong media type. Exiting now, rc=8\n" >> $LOG
            exit_cleanup 8
        else
            echo -e "Unknown error while mounting the media. Exiting now, rc=5\n" >> $LOG

            echo "----- begin mount info -----" >> $LOG
            x=`cat $MOUNT_OUTPUT`
            echo "$x" >> $LOG
            echo "----- end mount info -----" >> $LOG
            exit_cleanup 5
        fi
    fi

    DVD_DEV=/dev/hdc
    HD=`/opt/hsc/bin/GetHD`
    if [ "$HD" != "hda" ]; then
        DVD_DEV=/dev/hda
    fi

else
    #-------------------------------------------------------------------------------
    # Ensure there is a remote archive file to restore - there should be.
    # 
    # Note there should be one and only one HMCBackup_yyyyMMdd.HHmmss file in this
    # directory. If not, just use the "first" one we find. All archievs will be 
    # removed when the operation completes.
    #-------------------------------------------------------------------------------
    x=`ls -1 $REM_ARCHIVE_LOCATION/HMCBackup*`
    if [ $? -ne 0 ]; then
        echo "The remotely restored archive file is not present. Exiting now, rc=3" >> $LOG
        exit_cleanup 3
    else
        # Ok, remote archive file is present, ensure there is only one of them
        # and set the script variable to point to this file.
        #
        # Set $ARCHIVE to be the first listed file - there should be only one present
        echo "The remotely restored archive file, $x, is present..." >> $LOG
        ARCHIVE=`echo $x | cut -d " " -f 1 -`
    fi
fi





#-------------------------------------------------------------------------------
# Now do the actual restore from the archive
#-------------------------------------------------------------------------------
echo -e "starting the archive restore processing for $ARCHIVE...\n" >> $LOG

#
# First thing to do is determine which files are shared objects that could 
# potentially be partially paged in the system.  Hence, a recurring read
# of that same library file could panic the system - in theory, of course.
# Rather than using the 'tar' command to restore those particular archive
# files, we'll be staging those files and using the 'mv' command.
#
# Construct a working/staging directory for pre/post processing. This working
# directory should not be present after a "clean" install.
#
WORKING_DIR=/tmp/restore/work
mkdir -p $WORKING_DIR
if [ $? -ne 0 ]; then
    # unmount the DVD here
    umount -v $MOUNTPOINT >> $LOG 2>&1
    echo "error creating the working directory for special file extraction. Exiting now, rc=11" >> $LOG
    exit_cleanup 11
fi
echo "working directory for special file extraction created successfully, continuing..." >> $LOG


#
# start with a clean extraction log, just in case...
#
rm -f $ARCHIVE_LOG
echo -e "Begin HMC file recovery log on `date`\n" > $ARCHIVE_LOG
if [ "$USE_LOCAL_BACKUP" = "1" ]; then
    /sbin/hdparm -d1 $DVD_DEV >/dev/null 2>&1
fi


#
# Script needs to query the contents of the tar archive file multiple times and this could be
# very "expensive". Redirect the output one time to a file now for all subsequent queries.
#
tar -tvzf $ARCHIVE > $WORKING_DIR/tar.list 2>> $ARCHIVE_LOG
tarRC=$?
if [ $tarRC -ne 0 ]; then
    # unable to query the archive file
    echo -e "unable to query/list the archive file, tarRC = $tarRC. Exiting now, rc=18 \n" >> $LOG
    echo -e "unable to query/list the archive file, tarRC = $tarRC. Exiting now, rc=18 \n" >> $ARCHIVE_LOG
    exit 18
fi
echo "HMC archive file query/list completed successfully, continuing..." >> $ARCHIVE_LOG

#
# Feature 587785 to prevent archive file from being unpacked on either a differnt
# HMC modle type or unpacked when starting with a different base HMC driver
#
echo "checking if restore allowed based on matching HMC driver versions..." >> $ARCHIVE_LOG
grep -w "/etc/HmcBase" $WORKING_DIR/tar.list > $WORKING_DIR/check.list 2>> $ARCHIVE_LOG
if [ $? -eq 0 ]; then
    echo "version check file exists in the archive, processing..." >> $ARCHIVE_LOG

    tar -xvzf $ARCHIVE --files-from=$WORKING_DIR/check.list --directory=$WORKING_DIR >> $ARCHIVE_LOG 2>&1
    if [ $? -eq 0 ]; then
        # unpack was ok in the $WORKING_DIR relative /etc dir, now check this file against freshly installed file
        echo "version check file unpacked ok, continuing..." >> $ARCHIVE_LOG

        if [ -f /etc/HmcBase ]; then
            # both files exist, compare them for version stamping info
            echo "now comparing archive file to recently installed file..." >> $ARCHIVE_LOG

            doChecksum $WORKING_DIR/etc/HmcBase /etc/HmcBase
            if [ $? -ne 0 ]; then
                # version miscompare
                echo "version miscompare detected, bypassing archive restoration. Exiting now, rc=16" >> $ARCHIVE_LOG
                echo "version miscompare detected, bypassing archive restoration. Exiting now, rc=16" >> $LOG

                rm -fr $WORKING_DIR
                exit_cleanup 16
            else
                echo "the archive file is compatible with the currently installed HMC version, continuing..." >> $ARCHIVE_LOG
                echo "the archive file is compatible with the currently installed HMC version, continuing..." >> $LOG
            fi
        else
            echo "version check file does not exist on the local HMC, continuing..." >> $ARCHIVE_LOG
        fi
    else
        echo "an error occurred unpacking the version check file, bypassing..." >> $ARCHIVE_LOG
    fi
else
    echo "no version check file exists in the archive, continue normal processing..." >> $ARCHIVE_LOG
fi

# This almost belongs with feature 587785 above, but do not put this guard in place quite yet since
# may have to migrate to different models and modify existing files on the HMC (XF86Config, etc)
#echo -e "checking if restore allowed based on matching HMC machine type/model...\n" >> $LOG
#tar -tvzf $ARCHIVE | grep "opt/hsc/data/hmcmodel.dat" | awk '{print $6}' > $WORKING_DIR/check.list 2>> $ARCHIVE_LOG
#if [ $? -eq 0 ]; then
#    tar -xvzf $ARCHIVE --files-from=$WORKING_DIR/check.list --directory=$WORKING_DIR >> $ARCHIVE_LOG 2>&1
#    if [ $? -eq 0 ]; then
#        # unpack was ok in the $WORKING_DIR relative /opt/hsc/data dir, check this file against freshly installed file
#        if [ -f /opt/hsc/data/hmcmodel.dat ]; then
#            # both files exist, compare them for version stamping info
#            doChecksum $WORKING_DIR/opt/hsc/data/hmcmodel.dat /opt/hsc/data/hmcmodel.dat
#            if [ $? -ne 0 ]; then
#                # machine type miscompare
#                echo -e "machine type/model miscompare detected, bypassing archive restoration...\n" >> $ARCHIVE_LOG
#                rm -fr $WORKING_DIR
#                exit_cleanup 16
#            fi
#        fi
#    fi
#fi







#
# build the list of all files that have a '.so' "extension" (except the library
# file search path cache, 'ld.so.cache' )
#
grep "\.so" $WORKING_DIR/tar.list | grep -v "ld.so.cache" | awk '{print $6}' > $WORKING_DIR/exclude.list 2>> $ARCHIVE_LOG
if [ $? -ne 0 ]; then
     # pointless - 'awk' will always rc=0...
     umount -v $MOUNTPOINT >> $LOG 2>&1
     rm -fr $WORKING_DIR

     echo "unable to generate the exclude file list. Exiting now, rc=13" >> $ARCHIVE_LOG
     exit_cleanup 13
fi

#
# Add the 'tar' command itself here. We'll restore file this with a cp/mv sequence
# when the 'tar' command has completed executing
#
grep -w "/bin/tar" $WORKING_DIR/tar.list
if [ $? -eq 0 ]; then
    echo "/bin/tar" >> $WORKING_DIR/exclude.list 2>> $ARCHIVE_LOG
fi

#
# Another special case for Squadrons here - '/bin/bash' will also not un-tar as part of
# the main body of files to unpack.  Add to exclude processing list...
#
grep -w "/bin/bash" $WORKING_DIR/tar.list
if [ $? -eq 0 ]; then
    echo "/bin/bash" >> $WORKING_DIR/exclude.list 2>> $ARCHIVE_LOG
fi

#
# Yet more file exclusions - defect 639341. Here, the tester notes that files in the sysvinit RPM
# packages are 1) first being back'd up (due to user error with setting the HMC date/time) and 2)
# upon unpacking, they received the all to common 'tar' error -> tar: <file>: Cannot open: Text file busy
#
# Very interesting why this issue is now showing up with the following list of files:
#
# /sbin/syslogd
# /sbin/init
# /bin/gzip
# /sbin/klogd
# /sbin/udevd
#
# Odd why the OS never tripped on these files all these years prior to this...
SYSVINIT_FILES="/sbin/syslogd
/sbin/init
/bin/gzip
/sbin/klogd
/sbin/udevd"

for file in $SYSVINIT_FILES
do
    grep -wq $file $WORKING_DIR/tar.list
    if [ $? -eq 0 ]; then
        echo "special file, $file, detected in archive - process accordingly..." >> $ARCHIVE_LOG
        # file was backed up. Ensure the file goes through special processing...
        echo $file >> $WORKING_DIR/exclude.list 2>> $ARCHIVE_LOG
    fi
done

echo "exclude file processing list constructed, continuing..." >> $ARCHIVE_LOG


#
# Next step is to un-tar only the previously excluded files to the
# staging area.
#
# Bypass the cache file containg references to the prior list of libraries
# residing in the directories listed in /etc/ld.so.conf.  We will dynamically
# regenerate this file using 'ldconfig' at end of 'tar'  processing
#
tar -xvzf $ARCHIVE --files-from=$WORKING_DIR/exclude.list --directory=$WORKING_DIR >> $ARCHIVE_LOG 2>&1
tarRC=$?
if [ $tarRC -ne 0 ]; then
     ARCHIVEFAIL=$tarRC
     echo "shared object 'tar' processing to working directory failed (rc = $tarRC), continuing..." >> $ARCHIVE_LOG
     echo "shared object 'tar' processing to working directory failed (rc = $tarRC), continuing..." >> $LOG
else
     echo "'tar' extraction of shared objects/misc files to working directory was successful, continuing..." >> $ARCHIVE_LOG
     echo "'tar' extraction of shared objects/misc files to working directory was successful, continuing..." >> $LOG
fi

#
# Now un-tar all remaining files from the archive, with a few exceptions
# The file "/.journal" may exist in some archives. 'tar' cmd will throw an
# error if this file is attempted to be unpacked
#
#echo "/opt/hsc/bin/restore" >> $WORKING_DIR/special.list
#echo "/etc/init.d/hmcRestore" >> $WORKING_DIR/special.list
echo "/var/hsc/log/restore.log" > $WORKING_DIR/special.list
echo "/var/hsc/log/restore.log.1" >> $WORKING_DIR/special.list
echo "/var/hsc/log/restore.log.2" >> $WORKING_DIR/special.list
echo "/var/hsc/log/hmcRestore.log" >> $WORKING_DIR/special.list
echo "/tmp/archive.log" >> $WORKING_DIR/special.list
echo "/.journal" >> $WORKING_DIR/special.list
echo "/" >> $WORKING_DIR/special.list


#
# Now do the un-tar of the majority of the archive file.
#
tar -xvzPf $ARCHIVE --overwrite --exclude-from=$WORKING_DIR/exclude.list --exclude-from=$WORKING_DIR/special.list >> $ARCHIVE_LOG 2>&1
tarRC=$?
if [ $tarRC -ne 0 ]; then
     ARCHIVEFAIL=$tarRC
     echo "remaining 'tar' archive processing failed (rc = $tarRC), continuing..." >> $ARCHIVE_LOG
     echo "remaining 'tar' archive processing failed (rc = $tarRC), continuing..." >> $LOG
else
     echo "remaining 'tar' archive file processing completed successfully." >> $ARCHIVE_LOG
     echo "remaining 'tar' archive file processing completed successfully." >> $LOG
fi


#
# Flush to disk, just in case
#   
sync

#
# The theory is here that we should now be referncing the archived library
# shared objects. Do this so we don't reference a file that is about to be
# replaced.
#
export LD_LIBRARY_PATH=$WORKING_DIR/lib:$WORKING_DIR/usr/lib

#
# Assuming this far, hopefully all files have been restored from the archive.
# Next step is to (pseudo-atomically?) relocate the *.so files.  We do this
# by 1) 'cp' the archive file to the *same filesystem* as it's current
# existing, version, then 2) 'mv' archived version to current version
#
# example: if archive file is '/tmp/file1.ext'...
# 1) copy '/tmp/restore/work/tmp/file1.ext' to '/tmp/file1.ext.stage',
# 2) move 'tmp/file1.ext.stage' to '/tmp/file1.ext'
#
# Note: Many of the *.so files will most likely be symbolic links, so
#       be sure NOT to follow them when copying
#
for i in `cat $WORKING_DIR/exclude.list`
do
      d=`/usr/bin/dirname $i`
      mkdir -p $d
      cp -d -v -p $WORKING_DIR$i $i.stage >> $ARCHIVE_LOG 2>&1
      if [ $? -ne 0 ]; then
           let CP_INDICATOR=CP_INDICATOR+1
      else
           # Continue with 'mv' only if 'cp' worked
           mv -vf $i.stage $i >> $ARCHIVE_LOG 2>&1
           if [ $? -ne 0 ]; then
                let MV_INDICATOR=MV_INDICATOR+1
           fi
      fi
done

# End 'tar' processing log
echo -e "end file recovery error log on `date`\n" >> $ARCHIVE_LOG
echo -e "file extraction from archive completed on `date`\n" >> $LOG


#
# Rebuild the library search path based on the prior /etc/ld.so.conf
# information and then remove the temporary override path
#
/sbin/ldconfig -v >> $LOG 2>&1
/sbin/ldconfig /usr/X11R6/lib/
export LD_LIBRARY_PATH=


#
# 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.
#
cd /home/
for user in *
do
     if [ -d $user ]
     then
       g=`/usr/bin/id -g -n $user`
       chown $user.$g $user
       chmod 755 $user
       
       # add per new subdirectories in users' home directories
       find $user -name "*" -type d ! -path $user/.ssh -exec chown ${user##/home/} {} \;
       
       echo "chown/chmod of $user /home directory completed" >> $ARCHIVE_LOG
     fi
done
cd /


#
# New requirement for RMC/MegaMouth. Set an indicator file so the 'ctrestore'
# script can be executed later (*after* SRC is started, do in hmcpotscfg) to
# unpack their data saved away in /var/ct.backup directory
#
if [ -d /var/ct.backup ]; then
    touch /var/hsc/log/restoreRMC
    echo -e "\nRMC indicator file set for 'ctrestore' script execution." >> $LOG
fi


if [[ $CP_INDICATOR -eq 0 && $MV_INDICATOR -eq 0 ]]; then
     echo "Restore processing of excluded file list was successful, ARCHIVEFAIL is $ARCHIVEFAIL." >> $LOG
   
     rm -f $IQYBCRIT
     echo "restore indicator file removed from HMC" >> $LOG
else
     echo -e "Restore processing of excluded file list was not successful.\n" >> $LOG
     echo "cp indicator is: $CP_INDICATOR" >> $LOG
     echo -e "mv indicator is: $MV_INDICATOR\n" >> $LOG

     echo "----- begin exclude file list -----" >> $LOG
     x=`cat /tmp/restore/work/exclude.list`
     echo "$x" >> $LOG
     echo "----- end exclude file list -----" >> $LOG
   
     ARCHIVEFAIL=127
     if [ "$USE_LOCAL_BACKUP" = "1" ]; then
         umount -v $MOUNTPOINT >> $LOG 2>&1
     fi
     exit_cleanup 15
fi


#-------------------------------------------------------------------------------
# unmount the restore media and exit
#-------------------------------------------------------------------------------
if [ "$USE_LOCAL_BACKUP" = "1" ]; then
    if umount -v $MOUNTPOINT >> $LOG 2>&1; then
        exit_cleanup 0
    else
        echo "Couldn't umount the media" >> $LOG
        exit_cleanup 4
    fi
else    
    exit_cleanup 0
fi
