# 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/sh

#Copyright 2005, IBM Corporation



#      Do restore from collaboration area backup tables, 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.
# (i.e. the values of its "coa_id" field).
#
#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 restore.
#
#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 a suitably named file.
#

# Usage of the file
if [[ $# -ne 4 ]]
then
 echo "Usage "
 echo "dorestore_category.sh --db=databasename --dbuser=dbusername --dbpassword=dbuserpassword --collabid=CollabAreaID"
 echo "Example dorestore_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
RestoreFile="${CollabID}-{`date |awk '{print "restorereport_category_ca_"$2"_"$3"_"$6"_"$4}'|sed s/":"//g`-.txt";export RestoreFile


#

sed s/"clnCoaId"/"${CollabID}"/ restore_category.sql > restore_category_scratch.sql

echo "***********************************************************"
date
echo "Starting to restore CollabID ${CollabID} from backup tables"
echo "***********************************************************"

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



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

spool ${RestoreFile}

@drop_views_category

-- compile procs
@procs_category

@restore_category_scratch


EOF

rm restore_category_scratch.sql

if [ "$TOP" ]
then
mv ${RestoreFile}  ${TOP}/logs/restoreLog
else
mv ${RestoreFile}  logs/restoreLog
fi

exit $?

