#!      /bin/sh

#	Copyright 2005, IBM Corporation


#      Do cleanup for one category-workflow collaboration area.

#	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.
#
#	3. Before running this script, fill in the correct value of the "coa_id" (i.e. in quotes) for the "7" in the two sed lines below.
#
#	4. Before running this script, fill in the correct <db username>/<db password>@<db schema> in the sqlplus command below.
#
#	5. Before running this script, decide whether you want fresh backup tables, or to re-use the existing ones (see below).  
#		The default is to re-use the tables.  To get fresh ones, uncomment the designated two lines below.
#
#	6. Output will be written to file clean.txt
#

# Usage of the file
if [[ $# -ne 4 ]]
then
 echo "Usage "
 echo "doclean_category.sh --db=databasename --dbuser=dbusername --dbpassword=dbuserpassword --collabid=CollabAreaID"
 echo "Example doclean_category.sh --db=wpcdb --dbuser=wpc1 --dbpassword=wpc1 --collabid=6"
 echo " (be sure that collabid is for a collab area with a category, not an item, workflow)"
   
 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
CleanFile="${CollabID}-{`date |awk '{print "cleanreport_category_ca_"$2"_"$3"_"$6"_"$4}'|sed s/":"//g`-.txt";export CleanFile



# REQUIRED: Before running this script, replace the 7 in the two lines below with the actual collab area id of the collab area you want to clean
sed s/"clnCoaId"/"${CollabID}"/ $TOP/src/integrity/SQL/oracle/create_views_category.sql > create_views_category_scratch.sql
sed s/"clnCoaId"/"${CollabID}"/ $TOP/src/integrity/SQL/oracle/clean_category.sql > clean_category_scratch.sql

sqlplus ${DBUSER}/${DBUserPassword}@${DBName} <<EOF

set heading off
set line 100000
set wrap off
set newpage NONE

spool ${CleanFile}


-- REQUIRED: Before running this script, decide whether to uncomment the next two lines (fresh backup tables), or not (re-use existing ones).
--	If the backup tables already exist, you should be able to leave these lines commented and reuse them.
--       (See header comment in clean.sql for more details.)
-- @drop_backup_tables
-- @create_backup_tables

-- create fresh views
@$TOP/src/integrity/SQL/oracle/drop_views_category
@create_views_category_scratch

-- compile procs
@$TOP/src/integrity/SQL/oracle/procs_category

-- Get a pre-cleanup report
@$TOP/src/integrity/SQL/oracle/report_category

-- clean
@clean_category_scratch

-- Get a post-cleanup report
-- create fresh (cleaned up) views
@$TOP/src/integrity/oracle/drop_views_category
@create_views_category_scratch
@$TOP/src/integrity/oracle/report_category

-- Clean up views and scatch files
@$TOP/src/integrity/oracle/drop_views_category


EOF

rm clean_category_scratch.sql
rm create_views_category_scratch.sql

if [ "$TOP" ]
then
mv ${CleanFile}  ${TOP}/logs/cleanLog
else
mv ${CleanFile}  logs/cleanLog
fi
exit $?
