#!/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

CURRENTDIR=$PWD
ARCHIVE=$1
LOG=$2
RESTORE_FILE=/mnt/upgrade/iqybcrit.dat

# Start log
echo -e "-> restore  data `date`\n" >> $LOG

# make sure we are in the root directory
cd /console/data

echo -e " changed to /console/data $PWD \n" >> $LOG
#create the restore directory
mkdir iqyrestore_area

echo -e " created iqyrestore_area \n" >> $LOG

echo -e "\nSTART the new RESTORE PROCESS in dir: $PWD\n" >>$LOG

echo -e "Doing a cd to the new staging area \n" >> $LOG
#move to the restore staging area
cd /console/data/iqyrestore_area

echo -e " changed to /console/data/iqyrestore_area $PWD \n" >> $LOG
RESTORE=/console/data/iqyrestore_area
echo -e "Moved to stage dir: $RESTORE \n" >>$LOG

if !(tar -xvjpf $ARCHIVE --overwrite > /tmp/tareslog2 2>&1); then
    echo -e "tar: Error extracting files from archive.\n" >> $LOG
fi
echo -e "tar was successful - creating iqyrestore.dat \n" >> $LOG
touch /console/data/iqyrestore.dat
echo -e "Done untaring I6950 version file $ARCHIVE  \n" >> $LOG
DATFILE=/console/data/iqyrestore.dat
echo -e "Adding files to $DATFILE \n" >> $LOG

#Now find all the files in the restore staging area
for dir in $(find . -type d); do
    if [ "$?" != "0" ]; then
       echo -e "\n Failed to find a dir using find . -type d \n" >> $LOG
    fi
    echo -e "Using directory /$dir \n" >> $LOG
    if [ -d /$dir ]; then
        echo -e "/$dir already exist \n" >> $LOG
    else
       echo "mkdir -p /$dir " >> $DATFILE
       echo "chown hmcmanager:nobody /$dir " >> $DATFILE
       echo -e "Ready to create  /$dir  \n" >> $LOG
    fi
done

echo -e "\n Done copying mkdir \n" >> $LOG

for file in $(find . -type f); do
    if [ "$?" != "0" ]; then
        echo -e "\nFailed to find . -type f  $file \n" >> $LOG
    fi
    echo "mv -f $RESTORE/$file /$file" >> $DATFILE
done

DELFILE=$RESTORE/console/data/deletelist
cat $RESTORE/console/data/iqzmBackupDeleted.trm >> $DELFILE
 echo -e "Set up to delete files  \n" >>$LOG
       cat $DELFILE | while read stf;
       do
         echo "rm $stf" >> $DATFILE
       done
echo -e "Created file: $DATFILE  ... it contents: \n" >> $LOG
echo -e "\nThis is the content of $DATFILE\n" >> $LOG
cat $DATFILE >>$LOG

# return to the directory we started from, otherwise we could mess up the HMC
cd $CURRENTDIR
echo -e "\nThis is the content of untaring $ARCHIVE \n" >> $LOG
cat /tmp/tareslog2 >> $LOG

echo -e "Moved to dir = $CURRENTDIR  \n" >> $LOG

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
#Now we need to copy the new iqyrestore.sh in place
echo -e "Check for updated version of iqyrestore.sh \n" >> $LOG
cd /   
IQYRESTORE=/console/init/framework/iqyrestore.sh
if !(tar -xvjf $ARCHIVE $IQYRESTORE --overwrite >> $LOG 2>&1); then
    echo -e "tar: Error extracting iqyrestore.sh from archive.">> $LOG
    cd /console/data
fi
if [ "$?" != "0" ]; then
    echo -e "No updated version of iqyrestore.sh was found \n" >> $LOG
else
    echo -e "The new version of iqyrestore.sh has been updated\n" >> $LOG
fi
echo -e "Ejecting backup DVD-RAM \n" >> $LOG
eject
echo -e "Time to reboot  ... \n" >> $LOG
reboot -f
# stop log
echo -e "<- restoreccdata\n" >> $LOG

exit 0
