#!      /bin/sh

#	Copyright 2009, IBM Corporation

#      dopurgesrcitems.sh - Purge the source items indicated by a source catalog in a CET_BADBLOB table

#

# Usage of the file
if [[ $# -ne 5 ]]
then
 echo "Usage "
 echo "dopurgesrcitems.sh --db=databasename --dbuser=dbusername --dbpassword=dbuserpassword --srcCtgId=srcctgid --outfile=filename"
 echo " "
 echo "Example dopurgesrcitems.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

PurgeFile="${SrcCtgId}-{`date |awk '{print "purgereport_item_ca_"$2"_"$3"_"$6"_"$4}'|sed s/":"//g`-.txt";export PurgeFile

#sed s/"= 4"/"=${SrcCtgId}"/ $TOP/src/integrity/SQL/db2/purge_src_items.sql > /tmp/purge_scratch.sql
#sed s/"srcCtgId"/"${SrcCtgId}"/ $TOP/src/integrity/SQL/db2/purge_src_items.sql > /tmp/purge_scratch.sql

echo "the value of SrcCtgId is : "
echo " ${SrcCtgId} "

db2 connect to ${DBName} user ${DBUSER} using ${DBUserPassword}

echo "********************************************************************"
echo "Dropping procedures if they already exist"
echo "Ignore all DB21034E messages"
echo "********************************************************************"

#db2 -tf $TOP/src/integrity/SQL/db2/drop_procedures_db2.sql
#db2 -tf $TOP/src/integrity/SQL/db2/drop_procedures_blob.sql


echo "********************************************************************"
echo "creating  the procedures "
echo "********************************************************************"

#db2 -td@ -f $TOP/src/integrity/SQL/db2/create_procedures_db2.sql
#db2 -tf /tmp/purge_scratch.sql


echo "********************************************************************"
echo " Deleting messages from the messages table  "
echo "********************************************************************"

db2 "delete from messages"
db2 "commit"


echo "********************************************************************"
echo "*********************** STARTING  PURGE SRC ITEMS ****************   "
echo "********************************************************************"

db2 "call PURGE_SRC_ITEMS_FROM_CET(${SrcCtgId})"
db2 "select substr(message_text,1,200) from messages" > ${Outfile}

echo "********************************************************************"
echo "*********************** FINISHED PURGE SRC ITEMS ****************   "
echo "********************************************************************"
#mv /tmp/temppurge.txt ${Outfile}
#rm purge_scratch.sql
db2 "terminate"

echo "******************Badblob cleanup is done *************"
echo "***********check ${Outfile} for cleanup report **********"
exit $?

