# IBM Confidential
# OCO Source Materials
# 5724-V51
#
# (c) Copyright IBM Corp. 2000, 2008 All Rights Reserved.
#
# The source code for this program is not published or otherwise
# divested of its trade secrets, irrespective of what has been
# deposited with the U.S. Copyright Office.

#!/bin/bash

#	Copyright 2007, IBM Corporation


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


#
# Usage of the file
if [[ $# -ne 1 ]]
then
 echo "Usage: "
 echo "docleanbloball.sh --cmpname=<company name>"
  
 exit 0
fi

#Declare variable
CMPNAME=`echo $1 | cut -d "=" -f2`;export CMPNAME

curdir="`pwd`"
echo "DIRECTORY NOW IS $curdir"

# get src connection descriptor
. ${TOP}/src/db/schema/util/set_db.sh

# 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


#
#sqlplus ${DBUSER}/${DBUserPassword}@${DBName} <<EOF
#   ${CCD_SQLPLUS} ${CONNECTION} <<EOF >& /dev/null
${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');
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
for i in `cat catalogId.txt`
do
echo " "
echo "BAD BLOB CLEANING FOR CATALOG ${i}"
echo " "
./docleanblob.sh --db="${DB_SERVICE_NAME}" --dbuser="${DB_USER_NAME}"  --dbpassword="${DB_USER_PWD}" --cmpName=${CMPNAME} --ctgId="${i}"
done

# Do the category trees
#cat temp_category_tree_ids.txt| grep -v SQL > ctrId.txt
#for i in `cat ctrId.txt`
#do
#echo " "
#echo "REPORT FOR CATEGORY-WORKFLOW COLLABORATION AREA ${i}"
#echo " "
#./doreportblob_category.sh --db="${DB_SERVICE_NAME}" --dbuser="${DB_USER_NAME}"  --dbpassword="${DB_USER_PWD}" --cmpName=bby --ctgId="${i}"
#done

#rm temp_catalog_ids.txt
#rm temp_category_tree_ids.txt
#rm catalogId.txt

if [ "$TOP" ]
then
rm ${TOP}/logs/badBlobsReports/lastreportall.txt 2>&1 >/dev/null
echo "`date |awk '{print "lastReport_"$2"_"$3"_"$6"_"$4}'|sed s/":"//g`" >> ${TOP}/logs/badBlobsReports/lastreportall.txt
else
rm logs/badBlobsReports/lastreportall.txt 2>&1 >/dev/null
echo "`date |awk '{print "lastReport_"$2"_"$3"_"$6"_"$4}'|sed s/":"//g`" >> logs/badBlobsReports/lastreportall.txt
fi


exit $?

