#!/bin/bash

# Copyright 2005, IBM Corporation

# Do report for all collaboration areas.


# Usage of the file
if [[ $# -ne 4 ]]
then
 echo "Usage "
 echo $0 " --db=databasename --dbuser=touser --dbpassword=touserpassword --code=cmp code"
 echo "Example " $0 " --db=wpcdb --dbuser=wpc1 --dbpassword=wpc1 --code=trigo"

 exit 0
fi

curdir="`pwd`"
echo "Directory now is $curdir"
# 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
RepFile=`date |awk '{print "ghost_items_reports/report_"$2"_"$3"_"$6"_"$4}'|sed s/":"//g`;export RepFile

# Creating Logs Directory
mkdir -p ghost_items_reports

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

if [ "$CMP_ID" != "" ];then
    ${ECHO} "company id found $CMP_ID"
    ${RM} tmpcmpid.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_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 "Running report for Collab Area ID ${CollabID}"
echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"

. $TOP/src/integrity/oneCmp/workflow/db2/report_ghost_items_db2.sh --db=${DBName} --dbuser=${DBUSER} --dbpassword=${DBUserPassword} --collabid=${CollabID} --reportfile=${RepFile}_${CollabID}.txt

echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
echo "Report on Collab Area ID ${CollabID} is done"
echo "Look at file ${RepFile}_${CollabID}.txt for report"
echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
done
mkdir -p $TOP/logs/ghost_items_reports
mv $TOP/src/integrity/oneCmp/workflow/db2/ghost_items_reports/*.txt $TOP/logs/ghost_items_reports
rm create_views.sql
exit $?