#!/bin/bash

#	Copyright 2007, IBM Corporation


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


#
# Usage of the file
if [[ $# -ne 1 ]]
then
 echo "Usage: "
 echo "serialization_integrity_verify.sh --code=<cmp code> "
  
 exit 0
fi

#Declare variable
CMPCODE=`echo $1 | cut -d "=" -f2`;export CMPCODE
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 "-"`

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

#${CAT} $TOP/src/integrity/common/get_cmpname_for_cmpcode.sql | ${SED} "s/%_CCD_CODE%/$CMPCODE/g" > tempcmpname.sql
#CMP_NAME_QRY=`. ${TOP}/src/db/schema/util/run_db_cmd.sh --file=tempcmpname.sql --errorfilter=yes | ${GREP} 'cmpname-'`
#${ECHO} $CMP_NAME_QRY

#CMP_QRY_VAL="${CMP_NAME_QRY}"
#CMP_NAME=`${ECHO} $CMP_QRY_VAL | ${GREP} 'cmpname-' | cut -f2 -d "-"`
#${ECHO} "company name - $CMP_NAME"
#
#sqlplus ${DBUSER}/${DBUserPassword}@${DBName} <<EOF
#   ${CCD_SQLPLUS} ${CONNECTION} <<EOF >& /dev/null
#Veena - FP12 changes commenting following
${CCD_SQLPLUS} ${CONNECTION} <<EOF >& /dev/null

set heading off;
set pagesize 0;
set feedback off;
spool temp_catalog_ids.txt;
SELECT CTG_ID FROM ctg WHERE ctg_type IN ('CATALOG')and CTG_COMPANY_ID=$CMP_ID;
spool off;

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
echo "BAD BLOB REPORT FOR ALL CONTAINERS"
./doreportblob.sh --db="${DB_SERVICE_NAME}" --dbuser="${DB_USER_NAME}"  --dbpassword="${DB_USER_PWD}" --cmpName="$CMPCODE" --ctgId="ALL"

#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 $?

