#!/bin/bash

#	Copyright 2009, IBM Corporation


#      Do bad blob cleaning for all collaboration areas of the current company


#
# Usage of the file
if [[ $# -ne 4 ]]
then
 echo "Usage: "
 echo "serialization_integrity_restore.sh --db=$DBName --dbuser=$DBUser --dbpassword=$DBUserPassword --code=<company code>"
  
 exit 0
fi

#Declare variable
DBName=`echo $1 | cut -d "=" -f2`;export DBName
DBUser=`echo $2 | cut -d "=" -f2`;export DBUser
DBUserPassword=`echo $3 | cut -d "=" -f2`;export DBUserPassword
CMPCODE=`echo $4 | cut -d "=" -f2`;export CMPCODE

curdir="`pwd`"
echo "DIRECTORY NOW IS $curdir"

# get src connection descriptor
. ${TOP}/src/db/schema/util/set_db.sh --confirm_db=no


# Be sure we're in original working directory
cd $curdir
echo "DIRECTORY NOW IS `pwd`"


if [ "$TOP" ]
then
mkdir ${TOP}/logs >>/dev/null 2>&1
mkdir ${TOP}/logs/badBlobsReports >>/dev/null 2>&1
else
mkdir logs >>/dev?null 2>&1
mkdir logs/badBlobsReports >>/dev/null 2>&1
fi

${CAT} $DB_SCRIPTS/get_cmpid_for_cmpcode.sql | ${SED} "s/%_CCD_CODE%/$CMPCODE/g" > tempcmpid.sql

CMP_ID_QRY=`. ${TOP}/src/db/schema/util/run_db_cmd.sh --file=tempcmpid.sql --errorfilter=yes | ${GREP} 'cmpid-'`
${ECHO} $CMP_ID_QRY
CMP_QRY_VAL="${CMP_ID_QRY}"

CMP_ID=`${ECHO} $CMP_QRY_VAL | ${GREP} 'cmpid-' | cut -f2 -d "-"`

if [ "$CMP_ID" != "" ]; then
    ${ECHO} "company id - $CMP_ID"
    ${RM} tempcmpid.sql
else
    ${ECHO} "company id not found. Exiting..."
    exit 0
fi


db2 connect to ${DBName} user $DBUser using ${DBUserPassword}
db2 "SELECT CTG_ID FROM ctg WHERE ctg_type IN ('CATALOG') and ctg_company_id=$CMP_ID" |grep -v CTG_ID |grep -v "\-\-"|grep -v record > catalogId.txt
db2 terminate

#Do the report to repopulate the CET_BADBLOb table
./doreportblob.sh --db=${DBName} --dbuser=${DBUser} --dbpassword=${DBUserPassword} --cmpName=${CMPCODE} --ctgId="ALL"
#

#db2 connect to ${DBName} user $DBUser using ${DBUserPassword}
#db2 "SELECT CTG_ID FROM ctg WHERE ctg_type IN ('CATALOG') and ctg_company_id=$CMP_ID" |grep -v CTG_ID |grep -v "\-\-"|grep -v record > catalogId.txt
#db2 terminate

# NOTE: Need to add get CTR ids to the above, in file temp_category_tree_ids.txt, to be able to look for bad blobs in (the categories of) category trees


# Do the catalogs
#cat temp_catalog_ids.txt | grep -v SQL > catalogId.txt
for i in `cat catalogId.txt`
do
echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ "
echo "STARTING BAD BLOB CLEANING FOR CATALOG ${i}"
echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ "
./doIntegrityRestoreBlob.sh --db="${DBName}" --dbuser="${DBUser}"  --dbpassword="${DBUserPassword}" --cmpName=${CMPCODE} --ctgId="${i}"

echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
echo "cleanup for the catalogId ${i} done"
echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"

done

#rm temp_catalog_ids.txt
rm catalogId.txt

if [ "$TOP" ]
then
rm ${TOP}/logs/badBlobsReports/lastreportall.txt 2>&1 >/dev/null
echo "`date |awk '{print "lastReport_"$2"_"$3"_"$6"_"$4}'|sed s/":"//g`" >> ${TOP}/logs/badBlobsReports/lastreportall.txt
else
rm logs/badBlobsReports/lastreportall.txt 2>&1 >/dev/null
echo "`date |awk '{print "lastReport_"$2"_"$3"_"$6"_"$4}'|sed s/":"//g`" >> logs/badBlobsReports/lastreportall.txt
fi


exit $?

