#!/bin/bash

# Copyright 2005, IBM Corporation

# Clean Ghost Items in 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 "clean_ghost_items_db2.sh --db=databasename --dbuser=touser --dbpassword=touserpassword --collabid=CollabAreaID --reportfile=ReportFileName"
 echo "Example clean_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}"/ $TOP/src/integrity/SQL/db2/create_views_db2.sql > /tmp/create_views.sql

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

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

db2 "delete from messages"
db2 "commit"

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

db2 -tf $TOP/src/integrity/SQL/db2/drop_backup_tables_db2.sql
db2 -tf $TOP/src/integrity/SQL/db2/drop_views_db2.sql

echo "***********************************************************"
echo "Creating the backup tables and views"
echo "Ignore all SQL0100W messages"
echo "***********************************************************"

db2 -tf $TOP/src/integrity/SQL/db2/create_backup_tables_db2.sql
db2 -tf /tmp/create_views.sql

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

db2 -tf $TOP/src/integrity/SQL/db2/drop_procedures_db2.sql

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

db2 -td@ -f $TOP/src/integrity/SQL/db2/create_procedures_db2.sql

echo "***********************************************************"
date
echo "Starting the report process before cleanup"
echo "***********************************************************"

db2 "call report_views"

db2 "select substr(message_text,1,200) from messages" > ${ReportFile}_before_cleanup.txt

echo "***********************************************************"
echo "Deleting messages from the messages table after initial report"
echo "***********************************************************"

db2 "delete from messages"
db2 "commit"

echo "***********************************************************"
date
echo "****** STARTING CLEANUP PROCESS ******"
echo "***********************************************************"

db2 "call CLEAN_AND_BACKUP_DATABASE_ROWS(${CollabID})"

db2 "select substr(message_text,1,200) from messages" > ${ReportFile}_cleanup.txt

echo "***********************************************************"
date
echo "Deleting messages from the messages table after cleanup"
echo "***********************************************************"

db2 "delete from messages"
db2 "commit"

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

db2 -tf $TOP/src/integrity/SQL/db2/drop_views_db2.sql

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

db2 -tf /tmp/create_views.sql

echo "***********************************************************"
date
echo "Starting the report process after cleanup"
echo "***********************************************************"

db2 "call report_views"

db2 "select substr(message_text,1,200) from messages" > ${ReportFile}_after_cleanup.txt

db2 "terminate"

echo "***********   Cleanup is done   ***********"
date
echo "****** Check ${ReportFile}_before_cleanup.txt file for report before cleanup *******"
echo "****** Check ${ReportFile}_cleanup.txt file for cleanup report *******"
echo "****** Check ${ReportFile}_after_cleanup.txt file for report after cleanup *******"
