# 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 ghost items reports for all collaboration areas of the current company



# Usage of the file
if [[ $# -ne 1 ]]
then
 echo "Usage "
 echo "workflow_integrity_verify.sh "
echo "Example workflow_integrity_verify.sh --code=trigo"
#echo "Example workflow_integrity_verify.sh --db=wpcdb --dbuser=wpc1 --dbpassword=wpc1"
# echo "Example doreportall.sh"
#  
 exit 0
fi

CMPCODE=`echo $1 | cut -d '=' -f2`;export CMPCODE
echo $CMPCODE

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

# get src connection descriptor
. ${TOP}/src/db/schema/util/set_db.sh --confirm_db=no
#. ${TOP}/bin/read_config_file.sh $TOP/etc/default/common.properties
#${MKDIR} ${_CCD_CONF_TMP_DIR}
#tmpfile=${_CCD_CONF_TMP_DIR}sanitycheck.master.sh.out
#${RM} $tmpfile

TOP_SQL="
set sqlnumber off;
set heading off;
set pagesize 0;
set linesize 10000;
set echo off;
set show off;
set feedback off;
set trimspool on;
set term off;
"

#CheckDupChi
#
# some bugs with duplicate chi rows
#

#CheckDupChi() {
#    ${CCD_SQLPLUS} ${CONNECTION} <<EOF >& /dev/null
#    $TOP_SQL
#    spool $tmpfile;
#    select
#    nvl(sum(count(1)), 0) || ' - ' || count(1)
#    from tctg_chi_category_hierarchy
#    where chi_next_version_id = 999999999
#    group by chi_company_id, chi_parent_id, chi_child_id
#    having count(1) > 1;
#    spool off;
#EOF
#    cnt=`${CAT} $tmpfile | ${GREP} -v 'SQL'| ${BC}`
#    if [ "$cnt" != "0" ]; then
#        ${ECHO} "Found [$cnt] chi rows that need deletion!!!!";
#    fi
#    ${RM} $tmpfile
#    ${ECHO} "done with test for dupl chi entries" 1>/dev/null 1>&2
#}

# 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/ghostItemsReports >>/dev/null 2>&1
else
mkdir logs >>/dev/null 2>&1
mkdir logs/ghostItemsReports >>/dev/null 2>&1
fi

${CAT} $DB_SCRIPTS/get_cmpid_for_cmpcode.sql | ${SED} "s/%_CCD_CODE%/$CMPCODE/g" > tempcmpid.sql
${ECHO} "Invoking the query to get cmpid"
CMP_ID_QRY=`. ${TOP}/src/db/schema/util/run_db_cmd.sh --file=tempcmpid.sql --errorfilter=yes | ${GREP} 'cmpid-'`
${ECHO} "Invoked the query to get the 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} tempcmpid.sql
else
    ${ECHO} "company id not found. Exiting..."
    exit 0
fi

#
#sqlplus ${DBUSER}/${DBUserPassword}@${DBName} <<EOF
#   ${CCD_SQLPLUS} ${CONNECTION} <<EOF >& /dev/null
echo $CCD_SQLPLUS
echo $CONNECTION
${CCD_SQLPLUS} ${CONNECTION} <<EOF >& /dev/null

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_CMP_ID=$CMP_ID;
spool off;
spool temp_category_tree.txt;
SELECT COA_ID FROM COA where COA_CONTAINER_TYPE='CATEGORY_TREE' and COA_CMP_ID=$CMP_ID;
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 " "
echo "==================================================================== "
./doreport.sh --db="${DB_SERVICE_NAME}" --dbuser="${DB_USER_NAME}"  --dbpassword="${DB_USER_PWD}" --coaid="${i}"
echo "==================================================================== "
done

# Do the category-workflow collab areas
# 9/21/07 disable category workflow ghost items checks for the time being
#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 " "
#./doreport_category.sh --db="${DB_SERVICE_NAME}" --dbuser="${DB_USER_NAME}"  --dbpassword="${DB_USER_PWD}" --coaid="${i}"
#done




rm temp_catalog.txt
# the following file is still created by above SQL, even if no actual checking of category=workflow collab areas is done. So remove it here.
rm temp_category_tree.txt
rm collabId.txt

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

${CCD_SQLPLUS} ${CONNECTION} <<EOF 

set heading off;
set pagesize 0;
 @$TOP/src/integrity/SQL/oracle/drop_indexes


EOF

exit $?

