#!/bin/bash

#      doconsolereportblob.sh - Do report for one collaboration area, whose associated workflow is an item workflow.

#	Copyright 2007, IBM Corporation

#

# Usage of the file
if [[ $# -ne 5 ]]
then
 echo "Usage "
 echo "doconsolereportblob.sh --db=databasename --dbuser=dbusername --dbpassword=dbuserpassword --srcCtgId=srcCtgId  --outfile=filename"
 echo "Example: doconsolereportblob.sh --db=wpcdb --dbuser=wpc1 --dbpassword=wpc1 --srcCtgId=6 --outfile=foo.txt"
  
 exit 0
fi


# Declare the variables
DBName=`echo $1 |cut -d "=" -f2`;export DBName
DBUSER=`echo $2 |cut -d "=" -f2`;export DBUSER
DBUserPassword=`echo $3 |cut -d "=" -f2`;export DBUserPassword
SrcCtgId=`echo $4 |cut -d "=" -f2`;export SrcCtgId
OutFile=`echo $5 |cut -d "=" -f2`;export Outfile


ReportFile="${SrcCtgId}-{`date |awk '{print "report_item_ca_"$2"_"$3"_"$6"_"$4}'|sed s/":"//g`-.txt";export ReportFile
sed s/"srcCtgId"/"${SrcCtgId}"/ reportblob.sql > reportblob_scratch.sql
${ECHO} "=================================================================" >>tempreport.txt
${ECHO} "STARTING REPORT FOR BAD SERIALIZED FORMS FOR CATALOG $SrcCtgId" >> tempreport.txt
#${ECHO} "Total number of bad items found : " >> tempreport.txt
${ECHO} "Total number of items having null blob values : " >> tempreport.txt

db2 connect to ${DBName} user ${DBUSER} using ${DBUserPassword}
db2 "select cet_primary_key from cet_badblob where cet_src_container_id=$SrcCtgId and cet_primary_key like '%Null%'" | grep  record | awk '{print $1}'>> tempreport.txt 
#db2 -tvf reportblob_scratch.sql -z tempreport.txt
${ECHO} "Total number of items having corrupt serialized forms: " >> tempreport.txt

#db2 connect to ${DBName} user ${DBUSER} using ${DBUserPassword}
db2 "select cet_primary_key from cet_badblob where cet_src_container_id=$SrcCtgId and cet_primary_key not like '%Null%'" | grep  record | awk '{print $1}'>> tempreport.txt 
db2 terminate
${ECHO} "=================================================================" >> tempreport.txt
#sqlplus -s ${DBUSER}/${DBUserPassword}@${DBName} <<EOF
#--set heading off;
#--set linesize 80;
#--set pagesize 0;
#--set feedback off;
#@procs
#spool tempreport.txt;
#@reportblob_scratch
#spool off
#EOF

mv tempreport.txt  ${OutFile}
rm reportblob_scratch.sql

exit $?
 


