#!/bin/bash

#	Copyright 2007, IBM Corporation


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


#
# Usage of the file
if [[ $# -ne 5 ]]
then
 echo "Usage: "
 echo "serialization_integrity_verify.sh --db=databasename --dbuser=touser --dbpassword=touserpassword --code=<cmp code> --coaname=<CollabArea Name>"
  
 exit 0
fi

curdir="`pwd`"
echo "Directory now is $curdir"
#Declare variable
DBNAME=`echo $1 | cut -d "=" -f2`;export DBNAME
DBUser=`echo $2 | cut -d "=" -f2`;export DBUser
DBUSERPWD=`echo $3 | cut -d "=" -f2`;export DBUSERPWD
CMPCODE=`echo $4 | cut -d "=" -f2`;export CMPCODE
COANAME=`echo $5 | cut -d "=" -f2`;export COANAME
echo $CMPCODE


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

# get src connection descriptor
. ${TOP}/src/db/schema/util/set_db.sh --confirm_db=no
# Declare the variables
#DB_SERVICE_NAME=`echo $1 |cut -d "=" -f2`;export DB_SERVICE_NAME
#DB_USER_NAME=`echo $2 |cut -d "=" -f2`;export DB_USER_NAME
#DB_USER_PWD=`echo $3 |cut -d "=" -f2`;export DB_USER_PWD

# 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 "-"`

${ECHO} "company Id - $CMP_ID"
if [ "$CMP_ID" != "" ];then
    ${ECHO} "company id found $CMP_ID"
    ${RM} tempcmpid.sql
else
    ${ECHO} "company id not found. Exiting..."
    exit 0
fi
#echo $DBUSERPWD
db2 connect to $DBNAME USER $DBUser USING $DBUSERPWD >> connection.txt
cd $curdir
#

#db2 connect to ${DBNAME} user ${DBUSER} using  ${DBUSERPWD}
db2 "SELECT COA_SRC_CONTAINER_ID FROM coa WHERE coa_container_type IN ('CATALOG')and COA_NAME='$COANAME' and COA_CMP_ID=$CMP_ID" | grep -v COA_SRC_CONTAINER_ID | grep -v "\-\-"|grep -v record > catalogId.txt
#spool off;
db2 terminate >> connection.txt
#EOF
# 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

# NOTE: XXX TODO: note hardwired "bby" company in following commands;  need to make more general

# Do the catalogs
#cat temp_catalog_ids.txt | grep -v SQL > catalogId.txt
for i in `cat catalogId.txt`
do
echo " "
echo "BAD BLOB REPORT FOR CATALOG ${i}"
echo " "
./doreportblob.sh --db="${DBNAME}" --dbuser="${DBUser}"  --dbpassword="${DBUSERPWD}" --cmpName="$CMPCODE" --ctgId="${i}"
done

# Do the category trees
#cat temp_category_tree_ids.txt| grep -v SQL > ctrId.txt
#for i in `cat ctrId.txt`
#do
#echo " "
#echo "REPORT FOR CATEGORY-WORKFLOW COLLABORATION AREA ${i}"
#echo " "
#./doreportblob_category.sh --db="${DB_SERVICE_NAME}" --dbuser="${DB_USER_NAME}"  --dbpassword="${DB_USER_PWD}" --cmpName=bby --ctgId="${i}"
#done

#rm temp_catalog_ids.txt
#rm temp_category_tree_ids.txt
rm catalogId.txt

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


exit $?

