#!/bin/bash

#	Copyright 2007, IBM Corporation


#      Do ghost items cleanings for all collaboration areas of the current company


#
# Usage of the file
if [[ $# -ne 1 ]]
then
 echo "Usage "
 echo "workflow_integrity_restore.sh  --code=<cmpcode>"
 echo "Example workflow_integrity_restore.sh --code=trigo"
 exit 0
fi


CMPCODE=`echo $1 |cut -d "=" -f2`;export CMPCODE
curdir="`pwd`"
echo "DIRECTORY NOW IS $curdir"

# get src connection descriptor
. ${TOP}/src/db/schema/util/set_db.sh --confirm_db=no
#. ${TOP}/bin/read_config_file.sh $TOP/etc/default/common.properties
#${MKDIR} ${_CCD_CONF_TMP_DIR}
#tmpfile=${_CCD_CONF_TMP_DIR}sanitycheck.master.sh.out
#${RM} $tmpfile

CleanAllFile="Cleanall_`date |awk '{print "cleanreport_item_ca_"$2"_"$3"_"$6"_"$4}'|sed s/":"//g`.txt";export CleanAllFile


# Be sure we're in original working directory
cd $curdir
echo "DIRECTORY NOW IS `pwd`"


if [ "$TOP" ]
then
mkdir ${TOP}/logs >>/dev/null 2>&1
mkdir ${TOP}/logs/ghostItemsCleanings >>/dev/null 2>&1
else
mkdir logs >>/dev/null 2>&1
mkdir logs/ghostItemsCleanings >>/dev/null 2>&1
fi

${CAT} $DB_SCRIPTS/get_cmpid_for_cmpcode.sql | ${SED} "s/%_CCD_CODE%/$CMPCODE/g" > tempcmpid.sql
CMP_ID_QRY=`. ${TOP}/src/db/schema/util/run_db_cmd.sh --file=tempcmpid.sql --errorfilter=yes | ${GREP} 'cmpid-'`
CMP_QRY_VAL="${CMP_ID_QRY}"
CMP_ID=`${ECHO} $CMP_QRY_VAL | ${GREP} 'cmpid-' | cut -f2 -d "-"`

if [ "$CMP_ID" != "" ]; then
    ${ECHO} "company id $CMP_ID"
    ${RM} tempcmpid.sql
else
    ${ECHO} "company id not found. Exiting..."
    exit 0
fi

echo $DB_SERVICE_NAME
echo $DB_USER_NAME
echo $DB_USER_PWD
echo $CMP_ID
# Do the item-workflow collab areas
./doIntegrityRestoreAll.sh --db="${DB_SERVICE_NAME}" --dbuser="${DB_USER_NAME}"  --dbpassword="${DB_USER_PWD}" --cmpid="$CMP_ID"  2>&1 >${CleanAllFile}
#./docleanall.oldArgs.sh --db="${DB_SERVICE_NAME}" --dbuser="${DB_USER_NAME}"  --dbpassword="${DB_USER_PWD}" --cmpid="${CMP_ID}" 

# Do the category-workflow collab areas
# 9/21/07 disable category workflow ghost items checks for the time being

if [ "$TOP" ]
then
# 9/21/07 - don't need these since done by inner proc; also have bad names
#rm ${TOP}/logs/ghostItemsCleanings/lastreportall.txt
#echo "`date |awk '{print "lastReport_"$2"_"$3"_"$6"_"$4}'|sed s/":"//g`" >> ${TOP}/logs/ghostItemsCleanings/lastreportall.txt
mv ${CleanAllFile}  ${TOP}/logs/ghostItemsCleanings
else
#rm logs/ghostItemsCleanings/lastreportall.txt
#echo "`date |awk '{print "lastReport_"$2"_"$3"_"$6"_"$4}'|sed s/":"//g`" >> logs/ghostItemsCleanings/lastreportall.txt
mv ${CleanAllFile} logs/ghostItemsCleanings
fi



exit $?

