#!      /bin/sh

#	Copyright 2005, IBM Corporation


#      Do cleanup for one 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 "dooneca_clean_ghostitem.sh --db=databasename --dbuser=dbusername --dbpassword=dbuserpassword --collabid=CollabAreaID"
 echo "Example dooneca_clean_ghostitem.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
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 "blobcleanreport_item_ca_"$2"_"$3"_"$6"_"$4}'|sed s/":"//g`-.txt";export CleanFile


# replace symbolic constants
sed s/"clnCoaId"/"${CollabID}"/ $TOP/src/integrity/SQL/oracle/create_views.sql > create_views_scratch.sql
sed s/"clnCoaId"/"${CollabID}"/ $TOP/src/integrity/SQL/oracle/clean.sql > clean_scratch.sql

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

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

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

@$TOP/src/integrity/SQL/oracle/procs

-- clean
@clean_scratch

-- Clean up views, to leave a clean situation for next creator/user of them
@$TOP/src/integrity/SQL/oracle/drop_views

EOF

rm clean_scratch.sql
rm create_views_scratch.sql

# move logfile to standard location
#if [ "$TOP" ]
#then
#mv ${CleanFile}  ${TOP}/logs/cleanLog
#else
#mv ${CleanFile}  logs/cleanLog
#fi

exit $?

