#!/bin/bash
# Licensed Materials - Property of IBM
# 5724-I68
#
# (C) Copyright IBM Corp. 2000, 2006
#
# US Government Users Restricted Rights - Use, duplication, or
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp.

#                     logs
. ${TOP}/src/db/schema/util/set_db.sh

${ECHO}
${ECHO} ===========================================================
${ECHO} Dropping log tables and sequences used in user defined logs
${ECHO} ===========================================================
${ECHO}

#USER_DEFINED_LOG_TBL_AND_SEQ=`${CCD_SQLPLUS} ${CONNECTION} << EOF | ${GREP} 'TCTG_ULE_USER_LOG_ENTRY_' 
#${DB_SQLFILE_EXECUTE} $DB_SCRIPTS/drop_log_tbl_seq.sql
#EOF`
USER_DEFINED_LOG_TBL_AND_SEQ=`. ${DB_DIR}/util/run_db_cmd.sh --file=$DB_SCRIPTS/drop_log_tbl_seq.sql | ${GREP} 'TCTG_ULE_USER_LOG_ENTRY_'`

LOG_TBL_SEQ="${USER_DEFINED_LOG_TBL_AND_SEQ}"
${TOUCH} temp.sql


for j in $LOG_TBL_SEQ
do
LOG_TABLE_NAME=`${ECHO} $j | ${GREP} 'TCTG_ULE_USER_LOG_ENTRY_' | ${GREP} -v '%'`

EXEC_LINE=""
	if [ ! -z $LOG_TABLE_NAME ]; then
		if [ "$CCD_DB" = "oracle" ]; then 
			# ORACLE ( needs CASCADE CONSTRAINTS)	
			EXEC_LINE="drop table $LOG_TABLE_NAME cascade constraints;"
		elif [ "$CCD_DB" = "db2" ]; then
			# DB2 
			EXEC_LINE="drop table $LOG_TABLE_NAME;"
		else 
			${ECHO} "Error! Database Environment has not been set."
			exit
		fi
	
		${ECHO} $EXEC_LINE >> temp.sql
	fi
done


#USER_DEFINED_LOG_SEQUENCES=`${CCD_SQLPLUS} ${CONNECTION} <<EOF | ${GREP} 'QCTG_ULE_' 
#$LOG_SEQ_LIST
#EOF`

#LOG_SEQUENCES="${USER_DEFINED_LOG_SEQUENCES}"

for k in $LOG_TBL_SEQ
do
LOG_SEQ_NAME=`${ECHO} $k | ${GREP} 'QCTG_ULE_' |  ${GREP} -v '%'`
	if [ ! -z $LOG_SEQ_NAME ]; then
		if [ "$CCD_DB" = "oracle" ]; then 
			# ORACLE
			EXEC_LINE="drop sequence $LOG_SEQ_NAME;"
		elif [ "$CCD_DB" = "db2" ]; then
			# DB2 ( needs a key word RESTRICT )
			EXEC_LINE="drop sequence $LOG_SEQ_NAME restrict;"
		else 
			${ECHO} "Error! Database Environment has not been set."
			exit
		fi
		${ECHO} $EXEC_LINE >> temp.sql
	fi
done

# CR 37895 - make sure to delete the _references_ to UDLs
${ECHO} "delete from tctg_udl_user_defined_logs;" >> temp.sql

${ECHO} "commit;" >> temp.sql


#${CCD_SQLPLUS} ${CONNECTION} <<EOF
#${DB_SQLFILE_EXECUTE} temp.sql
#EOF

. ${DB_DIR}/util/run_db_cmd.sh --file=temp.sql

${RM} temp.sql


