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

#      doreportblob.sh - Do bad blob reporting, for a given source catalog and all its associated collaboration areas catalogs.

#
# Usage 
if [[ $# -ne 5 ]]
then
 echo "Usage "
 echo "doreportblob.sh --db=databasename --dbuser=dbusername --dbpassword=dbuserpassword --cmpName=cmpName --ctgId=ctgId"
 echo "    where databasename, dbusername, and dbuserpassword are viewable in the WPC System Administrator > Properties menu" 
 echo "    and ctgId is the (numerical) id of a 'source' catalog in company 'cmpName'." 
 echo "    ctgId is obtainable via the WPC DBAdmin screen, with the query 'select ctg_id from ctg where ctg_name = CATALOGNAME'." 
 echo ""
 echo "Sample command line: doreportblob.sh --db=wpcdb --dbuser=wpc1 --dbpassword=wpc1 --cmpName=wildwest --ctgId=99" 
 echo ""
  
 exit 0
fi

# variables from cmd line args
DBName=`echo $1 |cut -d "=" -f2`;export DBName
DBUSER=`echo $2 |cut -d "=" -f2`;export DBUSER
DBUserPassword=`echo $3 |cut -d "=" -f2`;export DBUserPassword
CompanyName=`echo $4 |cut -d "=" -f2`;export CompanyName
CtgId=`echo $5 |cut -d "=" -f2`;export CtgId
ReportFile="Catalog_${CtgId}_`date |awk '{print "badblob_report_"$2"_"$3"_"$6"_"$4}'|sed s/":"//g`.txt";export ReportFile
ConsoleReportFile="Catalog_${CtgId}_`date |awk '{print "console_badblob_report_"$2"_"$3"_"$6"_"$4}'|sed s/":"//g`.txt";export ConsoleReportFile

#echo $CompanyName
# initialize output file
echo "" > ${ReportFile}

# Logging directory
if [ "$TOP" ]
then
mkdir ${TOP}/logs  >>${ReportFile} 2>&1
mkdir ${TOP}/logs/badBlobsReports   >>${ReportFile} 2>&1
else
mkdir logs  >>${ReportFile} 2>&1
mkdir logs/badBlobsReports  >>${ReportFile} 2>&1
fi 


# Delete all CET_BADBLOB rows for this ${CtgId};
echo " "   >> ${ReportFile} 2>&1
echo "=================================================================="   >>${ReportFile} 2>&1
echo "Deleting all CET rows for source catalog ${CtgId}"   >>${ReportFile} 2>&1
echo "=================================================================="   >>${ReportFile} 2>&1
echo " "   >>${ReportFile} 2>&1
./dodeletecet.sh  --db=${DBName} --dbuser=${DBUSER} --dbpassword=${DBUserPassword} --srcCtgId=${CtgId}   >> ${ReportFile} 2>&1


# Get all all collab area catalogs, for all collab areas associated with this source catalog
sqlplus -s ${DBUSER}/${DBUserPassword}@${DBName} <<EOF >& /dev/null
-- Get all the collab area catalogs associated with this source catalog
set heading off;
set pagesize 0;
set feedback off;
spool temp_catalog.txt;
SELECT COA_DST_CONTAINER_ID FROM COA where COA_CONTAINER_TYPE='CATALOG' and COA_SRC_CONTAINER_ID = ${CtgId};
spool off;
spool temp_coa.txt;
SELECT COA_ID FROM COA where COA_CONTAINER_TYPE='CATALOG' and COA_SRC_CONTAINER_ID = ${CtgId};
spool off;
EOF


# blob check for source catalog
echo " " >>${ReportFile}
echo "=================================================================="   >>${ReportFile} 2>&1
echo "Blobcheck for source catalog ${CtgId}"   >>${ReportFile} 2>&1
echo "=================================================================="   >>${ReportFile} 2>&1
echo " "   >>${ReportFile} 2>&1
$JAVA_RT com.ibm.ccd.element.common.EntryVerifier ${CompanyName} ${CtgId}   >>${ReportFile} 2>&1
echo ""   >>${ReportFile} 2>&1
echo "=================================================================="   >>${ReportFile} 2>&1


# Do the item-workflow collab area catalogs, for all collab areas associated with this source catalog
cat temp_catalog.txt| grep -v SQL > collab_ctg_ids.txt
(for i in `cat collab_ctg_ids.txt`
do
echo " "
echo "=================================================================="
echo "Blobcheck for collaboration area catalog ${i}"
echo "=================================================================="
echo " "
$JAVA_RT com.ibm.ccd.element.common.EntryVerifier ${CompanyName} ${i}
echo ""
echo "=================================================================="
done)   >>${ReportFile} 2>&1


# XXX Produce the console report CET rows for this ${CtgId}, and cat to stdout
echo " "    >>${ReportFile} 2>&1
echo "=================================================================="   >>${ReportFile} 2>&1
echo "Produce console output report for source catalog ${CtgId}"   >>${ReportFile} 2>&1
echo "=================================================================="   >>${ReportFile} 2>&1
echo " "   >>${ReportFile} 2>&1
./doconsolereportblob.sh  --db=${DBName} --dbuser=${DBUSER} --dbpassword=${DBUserPassword} --srcCtgId=${CtgId} --outfile=${ConsoleReportFile}  >>${ReportFile} 2>&1
echo ""  >>${ReportFile} 2>&1
echo "=================================================================="  >>${ReportFile} 2>&1


# Get the same "truncated lines" for the report to save
os=`uname`;export os
if [ "$os" = "Linux" ]; then
cat ${ConsoleReportFile} |  pr --page-width=75 | sed '/^$/d' > temp_console.txt
elif [ "$os" = "AIX" ]; then
cat ${ConsoleReportFile} | pr -w 75 | sed '/^$/d' | grep -v successfully | grep -v Page > temp_console.txt
elif [ "$os" = "SunOS" ]; then
cat ${ConsoleReportFile} | pr -w 75 | sed '/^$/d' | grep -v successfully | grep -v Page > temp_console.txt
else
cat ${ConsoleReportFile} |  pr --page-width=75 | sed '/^$/d' > temp_console.txt
fi
#cat ${ConsoleReportFile} |  pr --page-width=75 | sed '/^$/d' > temp_console.txt
#cat ${ConsoleReportFile} |  pr --page-width=75 | sed '/^$/d' | grep -v successfully | grep -v Page > temp_console.txt
mv temp_console.txt ${ConsoleReportFile} 

# Now put it out on the console
cat ${ConsoleReportFile}

# XXX clean up tmp files
rm temp_catalog.txt    >>${ReportFile} 2>&1
rm collab_ctg_ids.txt   >>${ReportFile} 2>&1
rm temp_coa.txt   >>${ReportFile} 2>&1


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

# done
exit $?

