#!/bin/bash

# Copyright 2005, IBM Corporation

# Do report for one collaboration area.
# Run the SQL command "select coa_name, coa_id from coa order by coa_id" in your WPC DB Admin to see a list of collab areas and their ids.
#


 Usage of the file
if [[ $# -ne 5 ]]
then
 echo "Usage "
 echo "report_ghost_items_db2.sh --db=databasename --dbuser=touser --dbpassword=touserpassword --collabid=CollabAreaID --reportfile=ReportFileName"
 echo "Example report_ghost_items_db2.sh --db=wpcdb --dbuser=wpc1 --dbpassword=wpc1 --collabid=15 --reportfile=myreport"
 
 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
CollabID=`echo $4 |cut -d "=" -f2`;export CollabID
ReportFile=`echo $5 |cut -d "=" -f2`;export ReportFile


sed s/"= 4"/"= ${CollabID}"/ create_views_db2.sql > create_views.sql

db2 connect to ${DBName} user ${DBUSER} using ${DBUserPassword}

echo "***********************************************************"
date
echo "Report for Collab ID = ${CollabID}"
echo "***********************************************************"

echo "***********************************************************"
echo "Creating the backup tables if they don't exist"
echo "Ignore all DB21034E messages"
echo "***********************************************************"

db2 -tf create_backup_tables_db2.sql
db2 "commit"

echo "***********************************************************"
echo "Deleting messages from the messages table"
echo "***********************************************************"

db2 "delete from messages"
db2 "commit"

echo "***********************************************************"
echo "Deleting messages is done"
echo "Dropping the views if they already exist"
echo "Ignore all DB21034E messages"
echo "***********************************************************"

db2 -tf drop_views_db2.sql

echo "***********************************************************"
echo "Creating the views"
echo "Ignore all SQL0100W messages"
echo "***********************************************************"

db2 -tf create_views.sql

echo "***********************************************************"
echo "Dropping the procedures if they already exist"
echo "Ignore all DB21034E messages"
echo "***********************************************************"

db2 -tf drop_procedures_db2.sql

echo "***********************************************************"
echo "Creating the procedures"
echo "***********************************************************"

db2 -td@ -f create_procedures_db2.sql

echo "***********************************************************"
echo "Starting the report process"
echo "***********************************************************"

db2 "call report_views"

db2 "select substr(message_text,1,250) from messages" > tempreport

db2 "terminate"

cat tempreport | grep -v "\-\-\-\-\-"|grep -v "selected." > ${ReportFile}
rm tempreport

echo "***********************************************************"
date
echo "Reporting is done"
echo "Check ${ReportFile} file for report"
echo "***********************************************************"

