#!/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
# 2 - SNTF file not found
# 3 - VPDC file not found
# 08/14/2007 R. Manso  -01 Add checks for STNF and VPD files  L1243-L1644
# 11/26/2007 R. Manso  -02 Change the rc for missing files
# 11/30/2007 R. Manso  -03 Add check for bbrucbu.dat file O0760

ARCHIVE=$1
TMPDIR=$2
LOG=/console/$3
ACTBRST=$4
BUILD=$5
PATCH_FILE=/console/data/iqzmBackupChanged.trm
ZONE_INFO_RESTORE=/console/data/zoneinfo.restore
PATCHED_TAR_FILE=/PatchedFilesNotInConsole.tar.bz2
SNTF=/console/data/bbqtopn.dat
VPDC=/console/data/iqyvpdc.dat
BRST_TRM=/console/data/iqybrst.trm
VPDBU=/console/data/iqyvpdbu.dat
CBU=/console/data/bbrucbu.dat


# 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

#if SE, then make sure that the SNTF and vpdbu files are the backup list per L1243
if [ -e "$BRST_TRM" ]; then
    echo -e "It's SE, look for $SNTF , $VPDBU, and $CBU \n" >>$LOG
    if [ -e "$SNTF" ]; then
       echo -e "Adding $SNTF to the backup list \n" >>$LOG
       echo  "/console/data/bbqtopn.dat" >> $TMPDIR/filelist
    else
       # this file is critical, fail the backup if this file is not in the HDD
       echo -e "SNTF file: $SNTF was not found in the HDD, fail Backup script" >>$LOG
       exit 2
    fi
    if [ -e "$VPDBU" ]; then
       echo -e "Adding $VPDBU to the backup list \n" >>$LOG
       echo  "/console/data/iqyvpdbu.dat" >> $TMPDIR/filelist
    else
    # this file may or may not exist, so don't fail the backup if it's not in the HDD
      echo -e "VPDU file: $VPDBU was not found in the HDD, log it and continue with script" >>$LOG
    fi
    if [ -e "$CBU" ]; then
       echo -e "Adding $CBU to the backup list \n" >>$LOG
       echo  "/console/data/bbrucbu.dat" >> $TMPDIR/filelist
    else
    # this file may or may not exist, so don't fail the backup if it's not in the HDD
      echo -e "CBU file: $CBU was not found in the HDD, log it and continue with script" >>$LOG
    fi

fi
#Per L1243 and L1644, if the vpd file is not in the HDD, fail the backup
if [ -e "$VPDC" ]; then
    echo -e "Adding $VPDC to the backup list \n" >>$LOG
    echo  "/console/data/iqyvpdc.dat" >> $TMPDIR/filelist
else
# this file is critical, fail the backup if this file is not in the HDD
    echo -e "VPD file: $VPDC was not found in the HDD, fail Backup script" >>$LOG
    exit 3
fi


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 tar file name created from 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
   #we need to create a bz2 file based on the patched file
   #and add that bz2 file to the main backup.tar.bz2
   #if exist, remove the files from /usr/share/zoneinfo -- we always backup up the zoneinfo dir
   if grep -q "^\/usr\/share/\zoneinfo\/" $PATCH_FILE; then
          echo -e "Found /usr/share/zoneinfo/ in  $PATCH_FILE. Marker file was created \n" >>$LOG
          touch $ZONE_INFO_RESTORE
          #add this marker file to the filelist1
          echo  "$ZONE_INFO_RESTORE" >> $TMPDIR/filelist
          #remove all the files from the zoneinfo list, keep only the parent directory -- hard links
          sed -e '/\/usr\/share\/zoneinfo\//d' $PATCH_FILE > /ffdc/newPatchedFiles
          #just to ensure that we backup the /usr/share/zoneinfo, add that to the newPatchedFiles
          echo "/usr/share/zoneinfo" >> /ffdc/newPatchedFiles
   else
          echo -e "No /usr/share/zoneinfo/ found in  $PATCH_FILE. \n" >>$LOG
          echo -e "copy $PATCH_FILE to /ffdc/newPatchedFiles \n" >> $LOG
          cp $PATCH_FILE /ffdc/newPatchedFiles
   fi
   #sort this new list, in order to eliminate duplicates from the patched file
   sort /ffdc/newPatchedFiles -o /ffdc/newSortedPatchedFiles
   #eliminate duplicates in the patched file
   uniq /ffdc/newSortedPatchedFiles /ffdc/uniqSortedPatchedFiles
   #create a new tar file for files outside /console in the root directory
   if !(tar -cvjPf $PATCHED_TAR_FILE --files-from=/ffdc/uniqSortedPatchedFiles --ignore-failed-read >> $LOG 2>&1); then
       echo -e "tar error while processing files outside /console\n" >> $LOG
       echo -e "remove temp files from /ffdc \n" >> $LOG
       #remove temp patched files from /ffdc
       rm /ffdc/newPatchedFiles
       rm /ffdc/newSortedPatchedFiles
       rm /ffdc/uniqSortedPatchedFiles
       exit 1
   fi
   #if the tar file was created successfully, add its name to the backup filelist
   if [[ -a $PATCHED_TAR_FILE ]]; then
      echo -e "Created tar file: $PATCHED_TAR_FILE \n" >> $LOG
      echo "$PATCHED_TAR_FILE" >> $TMPDIR/filelist
      echo -e "Wrote $PATCHED_TAR_FILE into $TMPDIR/filelist \n" >> $LOG
   else
      echo -e "Failed to create tar file: $PATCHED_TAR_FILE \n" >> $LOG
   fi
   #remove temp patched files from /ffdc
   echo -e "Remove temp files from /ffdc \n" >> $LOG
   rm /ffdc/newPatchedFiles
   rm /ffdc/newSortedPatchedFiles
   rm /ffdc/uniqSortedPatchedFiles
else
   echo -e "File: $PATCH_FILE was NOT found \n" >> $LOG
fi


# copy the filelist into a temp file, to be stored in the tar.bz2
cp $TMPDIR/filelist $TMPDIR/backuplist

# add this new file to the filelist -- to verify that all the files in the
# filelist were backed-up

echo -e "Adding $TMPDIR/backuplist as the last file in the list of files to be backed-up \n" >>$LOG
echo  "$TMPDIR/backuplist" >> $TMPDIR/filelist

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 while creating backup file.. exiting\n" >> $LOG
    exit 1
fi
echo -e "<- backupccdata `date` \n" >> $LOG
exit 0
