#!/bin/bash

#	Copyright 2007, IBM Corporation

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

#
# Usage 
if [[ $# -ne 5 ]]
then
 echo "Usage "
 echo "doIntegrityRestoreBlob.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: doIntegrityRestoreBlob.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 "cleanblobreport_"$2"_"$3"_"$6"_"$4}'|sed s/":"//g`.txt";export ReportFile
ConsoleReportFile="Catalog_${CtgId}_`date |awk '{print "console_cleanblobreport_"$2"_"$3"_"$6"_"$4}'|sed s/":"//g`.txt";export ConsoleReportFile

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

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

# Do the report, to re-populate the CET_BADBLOB table
#doreportblob.sh --db=${DBName} --dbuser=${DBUSER} --dbpassword=${DBUserPassword} --cmpName=${CompanyName} --ctgId=${CtgId} 


# 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, and all the collab areas, 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


# now do "ghost items" cleanup, for all collab areas associated with this source catalog
cat temp_coa.txt | grep -v SQL > collab_ids.txt  
(for i in `cat collab_ids.txt`
do
echo " "   >>${ReportFile} 2>&1
echo "=================================================================="   >>${ReportFile} 2>&1
echo "CET-driven bad items cleanup for collaboration area ${i}"   >>${ReportFile} 2>&1
echo "=================================================================="  >>${ReportFile} 2>&1
echo " "  >>${ReportFile} 2>&1
dooneca_clean_blob.sh --db=${DBName} --dbuser=${DBUSER} --dbpassword=${DBUserPassword} --collabid=${i}  >>${ReportFile} 2>&1
dooneca_clean_ghostitem.sh --db=${DBName} --dbuser=${DBUSER} --dbpassword=${DBUserPassword} --collabid=${i}  >>${ReportFile} 2>&1
echo ""  >>${ReportFile} 2>&1
echo "=================================================================="  >>${ReportFile} 2>&1
done)  >>${ReportFile} 2>&1  >>${ReportFile} 2>&1

# item purge for source catalog
echo " "  >>${ReportFile} 2>&1
echo "=================================================================="  >>${ReportFile} 2>&1
echo "CET-driven bad items purge for source catalog ${CtgId}"  >>${ReportFile} 2>&1
echo "=================================================================="  >>${ReportFile} 2>&1
echo " "  >>${ReportFile} 2>&1
dopurgesrcitems.sh  --db=${DBName} --dbuser=${DBUSER} --dbpassword=${DBUserPassword} --srcCtgId=${CtgId} --outfile=${ConsoleReportFile}  >>${ReportFile} 2>&1
echo ""  >>${ReportFile} 2>&1
echo "=================================================================="  >>${ReportFile} 2>&1

cat ${ConsoleReportFile} |  pr --page-width=75 | sed '/^$/d'

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

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

# done
exit $?

