#!/bin/bash

# Copyright 2005, IBM Corporation

# Do report for all collaboration areas.


 Usage of the file
if [[ $# -ne 5 ]]
then
 echo "Usage "
 echo $0 " --db=databasename --dbuser=touser --dbpassword=touserpassword --code=companycode --coa=coaname"
 echo "Example " $0 " --db=wpcdb --dbuser=wpc1 --dbpassword=wpc1 --code=trigo --coa='testcollabarea'"

 exit 0
fi

curdir="`pwd`"
 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
COANAME=`echo $5 |cut -d "=" -f2`;export COANAME
RepFile=`date |awk '{print "ghost_items_cleanings/report_"$2"_"$3"_"$6"_"$4}'|sed s/":"//g`;export RepFile

# Creating Logs Directory
mkdir -p ghost_items_cleanings

. ${TOP}/src/db/schema/util/set_db.sh --confirm_db=no
#Getting the company id for CMP CODE
${CAT} $DB_SCRIPTS/get_cmpid_for_cmpcode.sql | ${SED} "s/%_CCD_CODE%/$CMPCODE/g" > tempcmpid.sql
CMP_ID_QRY=`. ${TOP}/src/db/schema/util/run_db_cmd.sh --file=tempcmpid.sql --errorfilter=yes | ${GREP} 'cmpid-'`
CMP_QRY_VAL="${CMP_ID_QRY}"
CMP_ID=`${ECHO} $CMP_QRY_VAL | ${GREP} 'cmpid-' | cut -f2 -d "-"`

if [ "$CMP_ID" != "" ]; then
    ${ECHO} "company id $CMP_ID"
    ${RM} tempcmpid.sql
else
   ${ECHO} "company id not found. Exiting ..."
   exit 0
 fi

cd $curdir
db2 connect to ${DBName} user ${DBUSER} using ${DBUserPassword}
db2 "select coa_id from coa where COA_CONTAINER_TYPE='CATALOG' and COA_NAME='$COANAME' and COA_CMP_ID=$CMP_ID" |grep -v COA_ID |grep -v "\-\-"|grep -v record > coaid.txt
db2 terminate

for CollabID in `cat coaid.txt`

do
echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
echo "Starting Cleanup of Collab Area ID ${CollabID}"
echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"

./clean_ghost_items_db2.sh --db=${DBName} --dbuser=${DBUSER} --dbpassword=${DBUserPassword} --collabid=${CollabID} --reportfile=${RepFile}_${CollabID}

echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
echo "Cleanup of Collab Area ID ${CollabID} is done"
echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"

done
mkdir -p $TOP/logs/ghost_items_cleanings
mv $TOP/src/integrity/oneCmpOneCA/workflow/db2/ghost_items_cleanings/*.txt $TOP/logs/ghost_items_cleanings
