#!/bin/bash
# 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.

# 
# # Copyright (c) 2000, 2004 IBM Corp. - All Rights Reserved.
# # IBM makes no representations or warranties about the suitability of
# # this program, either express or implied, including but not limited to
# # the implied warranties of merchantability, fitness for a particular
# # purpose, or non-infringement. 
# 
#


. ${TOP}/bin/get_params.sh

tmplog=${TOP}/logs/create_cmp.log

#----------------------------------------
#
# usage
#
#----------------------------------------

if [ "${_CCD_CODE}" = "" ]; then
    ${ECHO} "Usage: $0 --code=<company code> [--confirm_delete=no]"
    exit
fi


#${ECHO} "Alert: to make this faster, drop the indices on the tctg_ita_item_attributes table and create one on (item_id, company_id)"
#----------------------------------------
#
# cleanup
#
#----------------------------------------

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

. ${DB_ROOT_DIR}/schema/util/get_cmp_id.sh --code=${_CCD_CODE} --confirm_db=no

#${ECHO} ====================================================
#${ECHO} COMMON_INFO_ID_RECEIVED_SH 
#${ECHO} ====================================================

if [ ! "${CMP_ID}" = "" ]; then
    if [ ! "${_CCD_CONFIRM_DELETE}" = "no" ]; then
        ${ECHO}
        ${ECHO} COMMON_INFO_ISEXIST_SH
        ${ECHO} COMMON_INFO_ISCONTINUE_SH
        read res
        if [ ! "$res" = "y" ]; then
            ${ECHO} COMMON_INFO_NOCONTINUE_SH
            exit
        fi
    fi

${ECHO} ====================================================
${ECHO} COMMON_INFO_DELETE_TABLE_SH  
${ECHO} ====================================================

#${CCD_SQLPLUS} ${CONNECTION} <<EOF
#set define '&'
#define CMP_ID=${CMP_ID};

#-- put sql here for tables that do not have a cmp_id, using &CMP_ID



#--
#-- delete from ita, icm, itm first
#--
#-- to make it go faster, drop the indices on the ita table and create a different one on just item_id and company_id
#--


#DECLARE

#delete_size constant number := 10000;
#min_itm_id number;
#max_itm_id number;
#current_itm_id number;
#company_id constant number := &CMP_ID;

#BEGIN

#select max(itm_id) into max_itm_id from tctg_itm_item where itm_company_id = company_id;
#select min(itm_id) into min_itm_id from tctg_itm_item where itm_company_id = company_id;

#while min_itm_id <= max_itm_id loop
#delete from tctg_ita_item_attributes where ita_item_id <= min_itm_id + delete_size and ita_item_id >= min_itm_id and ita_company_id = company_id;
#delete from tctg_itm_item where itm_id <= min_itm_id + delete_size and itm_id >= min_itm_id and itm_company_id = company_id;
#delete from tctg_icm_item_category_map where icm_item_id <= min_itm_id + delete_size and icm_item_id >= min_itm_id and icm_company_id = company_id;
#commit;
#min_itm_id := min_itm_id + delete_size;
#END LOOP;
#END;
#/





#--
#-- now delete the other tables
#--


#`cat ${DB_DIR}/gen/cleanup.sql`

#EOF


# To get variables for MAX/MIN ITM_ID
. ${DB_ROOT_DIR}/schema/util/set_cleanup_cmp.sh>/dev/null

${TOUCH} tempdel.sql
${TOUCH} tempcleanup.sql
${ECHO} " " > tempdel.sql
${ECHO} " " > tempcleanup.sql

#${ECHO} ====================================================
#${ECHO} " Maximum Item Id recieved $MAX_ITM_ID"
#${ECHO} " Maximum Item Id recieved $MIN_ITM_ID"
#${ECHO} ====================================================

if [ -z "$MAX_ITM_ID" ]; then
	${ECHO} "Maximum Item Id could not be retrieved." >> $tmplog 
	${ECHO} "Either there are no Items to delete or there might be an error." >> $tmplog
	${ECHO} "Script will continue deleting old records." >> tmplog
fi
if [ -z "$MIN_ITM_ID" ]; then
	${ECHO} "Minimum Item Id could not be retrieved. Script cannot continue." >> $tmplog
	${ECHO} "Either there are no Items to delete or there might be an error." >> $tmplog
        ${ECHO} "Script will continue deleting old records." >> $tmplog
fi
if [[($MAX_ITM_ID > 0 && $MIN_ITM_ID > 0)]]; then
	DELETE_SIZE=3000
	MAX_ITM_ID_VAL=$MAX_ITM_ID
	MIN_ITM_ID_VAL=$MIN_ITM_ID
	CURR_MIN_ITM_ID_VAL=0

#	${ECHO} ====================================================
#	${ECHO} "Maximum Item Id before start of loop $MAX_ITM_ID_VAL"
#	${ECHO} "Minimum Item Id before start of loop $MIN_ITM_ID_VAL"
#	${ECHO} ====================================================
	while [ $MIN_ITM_ID_VAL -le $MAX_ITM_ID_VAL ]
	do 
	
	
		CURR_MIN_ITM_ID_VAL=`expr $MIN_ITM_ID_VAL + $DELETE_SIZE`
#		${ECHO} ====================================================
#		${ECHO} " Current Minimum Item Id $CURR_MIN_ITM_ID_VAL"
#		${ECHO} ====================================================
		DEL_SQL1="delete from tctg_ita_item_attributes where ita_item_id <= $CURR_MIN_ITM_ID_VAL and ita_item_id >= $MIN_ITM_ID_VAL and ita_company_id = $CMP_ID;"
		DEL_SQL2="delete from tctg_itm_item where itm_id <= $CURR_MIN_ITM_ID_VAL and itm_id >= $MIN_ITM_ID_VAL and itm_company_id = $CMP_ID;"
		DEL_SQL3="delete from tctg_icm_item_category_map where icm_item_id <= $CURR_MIN_ITM_ID_VAL and icm_item_id >= $MIN_ITM_ID_VAL and icm_company_id = $CMP_ID;"
		DEL_SQL4="delete from tctg_itd_item_detail where itd_item_id <= $CURR_MIN_ITM_ID_VAL and itd_item_id >= $MIN_ITM_ID_VAL and itd_company_id = $CMP_ID;"
    ${ECHO} $DEL_SQL1 >> tempdel.sql
    ${ECHO} "commit ;" >> tempdel.sql
		${ECHO} $DEL_SQL2 >> tempdel.sql
		${ECHO} $DEL_SQL3 >> tempdel.sql
		${ECHO} $DEL_SQL4 >> tempdel.sql
		${ECHO} "commit ;" >> tempdel.sql
		MIN_ITM_ID_VAL=$CURR_MIN_ITM_ID_VAL
	done 
	${ECHO} "commit ;" >> tempdel.sql
fi



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

. ${DB_DIR}/util/run_db_cmd.sh --file=tempdel.sql >> $tmplog


#${CAT} tempdel.sql

${CAT} ${DB_DIR}/gen/cleanup.sql | ${SED} "s/%CMP_ID%/$CMP_ID/g" > tempcleanup.sql

#${CAT} tempcleanup.sql

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

. ${DB_DIR}/util/run_db_cmd.sh --file=tempcleanup.sql >> $tmplog


${RM} tempdel.sql
${RM} tempcleanup.sql


fi
