#!/bin/sh

# restoreccdata - Restore critical console data from an archive file
#
# Usage: restoreccdata <archive> <logfile>
# This script must be run by a user with appropriate privileges (e.g., root)
#
# archive: the filename of the restore archive
# logfile: the filename of the log file to create
#
# Return Codes:
# 1 - tar error
# 02-22-07  R. Manso  Copy the tar file into /ffdc. Don't untar it
#                     There's a problem untaring Hard-Links from a
#                     different partition into /console H1508-H1549
# 06-27-07  R. Manso  Remove the --overwrite option from the tar command.

CURRENTDIR=$PWD
ARCHIVE=$1
LOG=$2
RESTORE_FILE=/mnt/upgrade/iqybcrit.dat
TARFILE=/ffdc/backup.tar.bz2
DATFILE=/console/data/iqyrestore.dat
IQYRESTORE=/console/init/framework/iqyrestore.sh
# Start log
echo -e "-> START the new RESTORE PROCESS: `date`\n" >> $LOG

# make sure we are at the root directory
cd /

echo -e " changed dir to: $PWD \n" >> $LOG
echo -e " Need to copy the backup file: $ARCHIVE \n" >>$LOG
cp $ARCHIVE $TARFILE
if [ "$?" != "0" ]; then
   echo -e "\nFailed to copy $ARCHIVE into $TARFILE \n" >> $LOG
else
   echo -e "$TARFILE created successfully \n" >> $LOG
   #Now we need to copy the new iqyrestore.sh in place
   echo -e "Check for updated version of iqyrestore.sh \n" >> $LOG
   if !(tar -xvjf $TARFILE $IQYRESTORE >> $LOG 2>&1); then
       echo -e "tar: Error extracting iqyrestore.sh from $TARFILE in restoreccdata.">> $LOG
   fi
fi
echo -e "Creating $DATFILE ">> $LOG
touch $DATFILE

echo -e "Mounting /mnt/upgrade \n" >> $LOG
mount /mnt/upgrade
echo -e "Done mounting /mnt/upgrade .. \n" >> $LOG
if [[ -a $RESTORE_FILE ]]; then
   echo -e "Found file: $RESTORE_FILE ... deleting it \n" >> $LOG
   rm $RESTORE_FILE
else
   echo -e "File: $RESTORE_FILE was not found \n" >>$LOG
fi
echo -e "<- END the new RESTORE PROCESS: `date`" >> $LOG
echo -e "Time to reboot  ... \n" >> $LOG
/sbin/reboot -f
# stop log

exit 0
