
#!/bin/bash
#	Copyright 2009, IBM Corporation

#      fullIntegrityRestore.sh - Do basic corruption cleanup, ghost items cleanup, and bad blob cleanup, 
#       for all containers in a given company.

#
# Usage 
if [[ $# -ne 5 ]]
then
 echo "Usage "
 echo "fullIntegrityRestore.sh --code=<cmp code> --ctg=<catalog name> --basic=<y/n> --wf=<y/n> --serialization=<y/n> "
 echo "For example to clean up all the corruptions"
 echo "Sample command line: fullIntegrityiRestore.sh --code=trigo --ctg='test catalog' --basic=y --wf=y --serialization=y" 
 echo "To clean only workflow corruptions"
 echo "Sample command line: fullIntegrityRestore.sh --code=trigo --ctg='testcotalog' --basic=n --wf=y --serialization=n" 
 exit 0
fi

#Declare variable
CMPCODE=`echo $1 | cut -d "=" -f2`;export CMPCODE
CTGNAME=`echo $2 | cut -d "=" -f2`;export CTGNAME
RUNBASIC=`echo $3 | cut -d "=" -f2`;export RUNBASIC
RUNWF=`echo $4 | cut -d "=" -f2`;export RUNWF
RUNSER=`echo $5 | cut -d "=" -f2`;export RUNSER
DBName=`${GREP} '^ *db_url' ${CCD_ETC_DIR}/default/common.properties | ${SED} -e 's/^.*:\([^:]*\) *$/\1/' -e's/[0-9]*//' -e s_/__ -e 's/@//'`;export DBName
DBUser=`${GREP} '^ *db_userName' ${CCD_ETC_DIR}/default/common.properties | cut -f2 -d "="`;export DBUser
DBUserPassword=`${GREP} '^ *db_password' ${CCD_ETC_DIR}/default/common.properties | cut -f2 -d "="`;export DBUserPassword
echo $DBName
echo $DBUser
echo $DBUserPassword

db2 connect to ${DBName} user ${DBUser} using ${DBUserPassword}
echo "*********************************************************************"
echo " Dropping  backup tables if they already exist, before invoking the clean up scripts "
echo "*********************************************************************"
db2 -tf $TOP/src/integrity/SQL/db2/drop_backup_tables_db2.sql

echo "********************************************************************"
echo " Creating backup tables"
echo "********************************************************************"

db2 -tf $TOP/src/integrity/SQL/db2/create_backup_tables_db2.sql

echo "*******************************************************************"
echo "Created backup tables"
echo "*******************************************************************"

db2 "terminate"


# Run master.sh

curdir="`pwd`"
echo "===================================================================================================================="
echo ">>>> STARTING FULL INTEGRITY RESTORE;  DIRECTORY IS $curdir <<<<<<<<<<"
echo "===================================================================================================================="
echo " "

# get src connection descriptor
#cd masterAndCleanup
if [[ $RUNBASIC = 'y' ]]
then
    cd ../basic/db2
    echo "==============================================="
    echo "NOW RUNNING BASIC INTEGRITY RESTORE.SH"
    echo "==============================================="

    # Redundant, but matches with analagous redundant line that will be prod    uced in ghost items and bad blobs runs
    #echo "==============================================="
    echo "DIRECTORY NOW IS `pwd`"
    #echo "==============================================="

    ./basic_integrity_restore.sh --code=$CMPCODE


    # Be sure we're in original working directory
    cd $curdir
    echo "================================================"
    echo "DONE WITH BASIC INTEGRITY VERIFY.SH;  DIRECTORY NOW IS `pwd`"
    echo "================================================"
    echo " "    
    echo " "
    echo " "
    echo " "
fi

# Run ghostitems checks

# get src connection descriptor
if [ $RUNWF = 'y' ]
then
    cd workflow/db2
    echo "==============================================="

    echo "NOW RUNNING WORKFLOW INTEGRITY RESTORE"
    echo "==============================================="

    ./workflow_integrity_restore.sh db=$DBName --dbuser=$DBUser --dbpassword=$DBUserPassword --code=$CMPCODE --ctgname="$CTGNAME"

    # Go back to original working directory
    cd $curdir
    echo "================================================"
    echo "DONE WITH WORKFLOW INTEGRITY ITEM RESTORATION;  DIRECTORY NOW IS `pwd`"
    echo "================================================"
    echo " "
    echo " "
    echo " "
    echo " "
fi


# Run dobadblob checks

# get src connection descriptor
if [ ${RUNSER} = 'y' ]
then
   
    cd serialization/db2
    echo "==============================================="
    echo "NOW RUNNING SERIALIZATION INTEGRITY RESTORATION"
    echo "==============================================="
    echo $CMPCODE
    ./serialization_integrity_restore.sh --db=$DBName --dbuser=$DBUser --dbpassword=$DBUserPassword --code=$CMPCODE --ctgname="$CTGNAME"

# Go back to original working directory
    cd $curdir
    echo "================================================"
    echo "DONE WITH SERIALIZATION INTEGRITY RESTORATION;  DIRECTORY NOW IS `pwd`"
    echo "================================================"
    echo " "
    echo " "
    echo " "
    echo " "
fi


# save logs to proper location
#if [ "$TOP" ]
#then
#mv ${ReportFile}  ${TOP}/logs/fullVerifyReports
#mv ${ConsoleReportFile}  ${TOP}/logs/fullVerifyReports
#else
#mv ${ReportFile}  logs/fullVerifyReports
#mv ${ConsoleReportFile}  logs/fullVerifyReports
#fi    >>${ReportFile} 2>&1


echo "===================================================================================================================="
echo ">>>> FINISHED FULL INTEGRITY RESTORE; DIRETORY NOW IS `pwd` <<<<<<<<<<"
echo "===================================================================================================================="
echo " "



# done
exit $?

