#!/bin/bash

#	Copyright 2005, IBM Corporation


#      Do clean for all collaboration area.


#
# Usage of the file
if [[ $# -ne 5 ]]
then
 echo "Usage "
 echo "doIntegrityRestoreAll.sh --db=databasename --dbuser=dbusername --dbpassword=dbuserpassword --cmpid=companyId --coaname=CollabAreaName"
 echo "Example doIntegrityRestoreAll.sh --db=wpcdb --dbuser=wpc1 --dbpassword=wpc1 --code=1 --coaname=TestCollabArea"
  
 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
#CMPCODE=`echo $4 |cut -d "=" -f2`;export CMPCODE
CMPID=`echo $4 |cut -d "=" -f2`;export CMPID
COANAME=`echo $5 |cut -d "=" -f2`;export COANAME

if [ "$TOP" ]
then
mkdir ${TOP}/logs 2>&1 >>/dev/null
mkdir ${TOP}/logs/ghostItemsCleanings  2>&1 >>/dev/null
else
mkdir logs 2>&1 >>/dev/null
mkdir logs/ghostItemsCleanings 2>&1 >>/dev/null
fi



#
sqlplus ${DBUSER}/${DBUserPassword}@${DBName} <<EOF


set heading off;
set pagesize 0;
set feedback off;
spool temp_catalog.txt;
SELECT COA_ID FROM COA where COA_CONTAINER_TYPE='CATALOG' and COA_NAME='$COANAME' and COA_CMP_ID=$CMPID;
spool off;
spool temp_category_tree.txt;
SELECT COA_ID FROM COA where COA_CONTAINER_TYPE='CATEGORY_TREE' and COA_NAME='$COA_NAME' and COA_CMP_ID=$CMPID;
spool off;

@$TOP/src/integrity/SQL/oracle/create_indexes
EOF

# Do the item-workflow collab areas
cat temp_catalog.txt| grep -v SQL > collabId.txt
for i in `cat collabId.txt`
do
echo " "
echo "REPORT FOR ITEM-WORKFLOW COLLABORATION AREA ${i}"
echo " "
./doIntegrityRestore.sh --db="${DBName}" --dbuser="${DBUSER}"  --dbpassword="${DBUserPassword}" --coaid="${i}"
done

# Do the category-workflow collab areas
# 9/21/07 disable
#cat temp_category_tree.txt| grep -v SQL > collabId.txt
#for i in `cat collabId.txt`
#do
#echo " "
#echo "REPORT FOR CATEGORY-WORKFLOW COLLABORATION AREA ${i}"
#echo " "
#./doclean_category.sh --db="${DBName}" --dbuser="${DBUSER}"  --dbpassword="${DBUserPassword}" --coaid="${i}"
#done

rm temp_catalog.txt 
rm temp_category_tree.txt
rm collabId.txt


if [ "$TOP" ]
then
rm ${TOP}/logs/ghostItemsCleanings/lastcleanall.txt
echo "`date |awk '{print "lastClean_"$2"_"$3"_"$6"_"$4}'|sed s/":"//g`" >> ${TOP}/logs/ghostItemsCleanings/lastcleanall.txt
else
rm logs/ghostItemsCleanings/lastcleanall.txt
echo "`date |awk '{print "lastClean_"$2"_"$3"_"$6"_"$4}'|sed s/":"//g`" >> logs/ghostItemsCleanings/lastcleanall.txt
fi

sqlplus ${DBUSER}/${DBUserPassword}@${DBName} <<EOF
set heading off;
set pagesize 0;

@$TOP/src/integrity/SQL/oracle/drop_indexes
 
spool off;
EOF

exit $?

