# 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 report for one collaboration area, whose associated workflow is an item workflow.

#	1. Run the SQL command "select coa_name, coa_id from coa order by coa_id;"
#		in your WPC DB Admin, or sqlplus, window, 
#		to see a list of collab areas and their ids.
#
#	2. Find (the name of) the collab area you want to do this report for,
#		 and write down its id.
#		 (i.e. the values of its "coa_id" field).
#       Be sure the associated workflow of this collab area is an item workflow, and not a category workflow.
#
#	3.  Before running this script, replace the "7" in the sed command below with the collab area id (in quotes) 
#            of the collab area you want to clean.
#
#	4.  Before running this script, fill in the correct <db username>/<db password>@<db schema> in the sqlplus command below.
#
#
#	5. Output will be written to file report.txt
#
# Usage of the file
if [[ $# -ne 4 ]]
then
 echo "Usage "
 echo "doreport.sh --db=databasename --dbuser=dbusername --dbpassword=dbuserpassword --collabid=CollabAreaID"
 echo "Example: doreport.sh --db=wpcdb --dbuser=wpc1 --dbpassword=wpc1 --collabid=6"
 echo "  (Be sure the associated workflow of this collab area is an item, and not a category, workflow)"
  
 exit 0
fi


# Declare the variables using the cmd line args
DBName=`echo $1 |cut -d "=" -f2`;export DBName
DBUSER=`echo $2 |cut -d "=" -f2`;export DBUSER
DBUserPassword=`echo $3 |cut -d "=" -f2`;export DBUserPassword
CollabID=`echo $4 |cut -d "=" -f2`;export CollabID


ReportFile="CollabArea_${CollabID}_`date |awk '{print "ghostitems_report_item_ca_"$2"_"$3"_"$6"_"$4}'|sed s/":"//g`-.txt";export ReportFile
ConsoleReportFile="CollabArea_${CollabID}_`date |awk '{print "console_ghostitems_report_item_ca_"$2"_"$3"_"$6"_"$4}'|sed s/":"//g`.txt";export ConsoleReportFile

# Replace the "formal parameter" in the "template" file create_views.sql, with the actual CollabID
sed s/"clnCoaId"/"${CollabID}"/ $TOP/src/integrity/SQL/oracle/create_views.sql > create_views_scratch.sql

# initialize output file
echo "" > ${ReportFile}

# Logging directory
if [ "$TOP" ]
then
mkdir ${TOP}/logs  >>${ReportFile} 2>&1
mkdir ${TOP}/logs/badBlobsReports   >>${ReportFile} 2>&1
else
mkdir logs  >>${ReportFile} 2>&1
mkdir logs/badBlobsReports  >>${ReportFile} 2>&1
fi 


sqlplus ${DBUSER}/${DBUserPassword}@${DBName}<<EOF   >>${ReportFile} 2>&1


-- set heading off
-- set line 10000
-- set wrap off
-- set newpage NONE

-- spool ${ReportFile}

@$TOP/src/integrity/SQL/oracle/drop_views
@create_views_scratch
@$TOP/src/integrity/SQL/oracle/procs
@$TOP/src/integrity/SQL/oracle/report

-- Clean up views and scratch files
--@$TOP/src/integrity/SQL/oracle/drop_views

EOF

# Produce console output and console report
# use "magic" patterns that work against the output of the REPORT_ITEMS_IN_VIEW SQL procs in procs.sql
cat ${ReportFile} | egrep 'true|Total' | grep -v event 
cat ${ReportFile} | egrep 'true|Total' | grep -v event > ${ConsoleReportFile} 


# remove scratch file
rm create_views_scratch.sql

# Move reports to right place
if [ "$TOP" ]
then
mv ${ReportFile}  ${TOP}/logs/ghostItemsReports
mv ${ConsoleReportFile}  ${TOP}/logs/ghostItemsReports
else
mv ${ReportFile}  logs/ghostItemsReports
mv ${ConsoleReportFile}  logs/ghostItemsReports
fi

exit $?
 


