#!/bin/sh

# backupccdata - Backup critical console data to an archive file
#
# Usage: backupccdata <archive> <tempdir> <logfile> <actbrst> <build>
# This script must be run by a user with appropriate privileges (e.g., root)
#
# archive: the filename of the backup archive to create
# tempdir: the name of the directory to contain temporary/work files
# logfile: the filename of the log file to create
# actbrst: the filename of the actbrst file
# build:   the build identifier
#
# Return Codes:
# 1 - tar error

ARCHIVE=$1
TMPDIR=$2
LOG=/console/$3
ACTBRST=$4
BUILD=$5
PATCH_FILE=/console/data/iqzmBackupChanged.trm

# Start log
echo -e "-> backupccdata\n" > $LOG
echo -e "Backup log for `date`\n" >> $LOG

cd /console
echo -e "just did the ch dir to /console \n" >>$LOG

#make sure that the restoreccdata is the first file in the backup list.
echo  "/console/native/runAsRoot/restoreccdata" > $TMPDIR/filelist
#make sure that the iqyrestore.sh is the second file in the backup list.
echo  "/console/init/framework/iqyrestore.sh" >> $TMPDIR/filelist
#make sure that the RestoreTasklet.class is the third file in the backup list.
echo  "/console/com/ibm/hwmca/base/rescrcon/RestoreTasklet.class" >> $TMPDIR/filelist
#make sure that the RestoreImpl.class is the fourth file in the backup list.
echo  "/console/com/ibm/hwmca/base/rescrcon/RestoreImpl.class" >> $TMPDIR/filelist
#make sure that the RestoreErrorIds.class is the fifth file in the backup list.
echo  "/console/com/ibm/hwmca/base/rescrcon/RestoreErrorIds.class" >> $TMPDIR/filelist


# change /backup to / when doing real backups
# find / -xdev -type f -mmin -$elapsedtime -print > $TMPDIR/filelist
# find -cnewer /console/data/builddate -type f -print >$TMPDIR/bubba

echo -e "about to do the newer thang \n" >>$LOG
#find -newer $BUILD -type f -print >$TMPDIR/filelist
/console/bin/framework/filelist -c -d /console -l /console/data/master.backup.lst -o $TMPDIR/filelist1 -r /console/data/deletelist

echo -e "\n about to copy the contents of filelist1 into filelist \n" >> $LOG
  cat $TMPDIR/filelist1 >> $TMPDIR/filelist
#echo $TMPDIR/deletelist >> $TMPDIR/filelist

echo -e "done copying the filelist, now add the $ACTBRST to it \n" >>$LOG
# special file that might not be backed up if we don't explicitly add this to the script

echo "$ACTBRST" >> $TMPDIR/filelist
echo -e "just added the $ACTBRST file to filelist \n">>$LOG
echo -e "$ARCHIVE \n" >> $LOG
echo -e "$ACTBRST \n" >> $LOG

# add the patchedfile to the file list. If patched file doesn't exist, write that in the log 
if [[ -a $PATCH_FILE ]]; then 
   echo -e "Found file: $PATCH_FILE \n" >> $LOG
   cat $PATCH_FILE >> $TMPDIR/filelist
   echo -e "Wrote contents of $PATCH_FILE into $TMPDIR/filelist \n" >> $LOG
else
   echo -e "File: $PATCH_FILE was NOT found \n" >> $LOG
fi

echo -e "now let'z do the tar - zan, Jane! \n" >>$LOG
# create new archive
if !(tar -cvjPf $ARCHIVE --exclude-from=$TMPDIR/exfiles --files-from=$TMPDIR/filelist --ignore-failed-read >> $LOG 2>&1); then # floppy test
    echo -e "tar error.. exiting\n" >> $LOG
    exit 1
fi
echo -e "<- backupccdata `date` \n" >> $LOG
exit 0
