#!/bin/bash

if [[ $# -lt 5 ]]
then
    ${ECHO} "Invalid number of arguments."
    ${ECHO} "Usage: $0 --cmpid=<CompanyID> --date=YYYY-MM-DD --dbuser=<dbuser> --dbpassword=<password> --dbname=<dbname>"
 exit
fi
${ECHO} "Last Revision: 5.3.2 FP11 20080805"

company_id=`${ECHO} $1 |cut -d "=" -f2`;export company_id
version_date=`${ECHO} $2 |cut -d "=" -f2`;export version_date
db_username=`${ECHO} $3 | cut -d "=" -f2`;export db_username
db_password=`${ECHO} $4 | cut -d "=" -f2`;export db_password
db_name=`${ECHO} $5 | cut -d "=" -f2`;export db_name

${ECHO} "CMP_ID : ${company_id}"
${ECHO} "VERSION_DATE : ${version_date}"
${ECHO} "DB_USER : ${db_username}"
${ECHO} "DB_Password : ${db_password}"
${ECHO} "DB_Name : ${db_name}"

db2 "connect to ${db_name} user ${db_username} using ${db_password}"

#db_username=`${ECHO} ${db_username}|tr '[:lower:]' '[:upper:]'`

db2 -x "select 'MaxVersionID=', max(ver_id) VER_ID from ver where ver_date < timestamp_format('${version_date} 00:00:00','YYYY-MM-DD HH24:MI:SS') and ver_company_id = ${company_id} " |tee /tmp/max_verid.txt

max_ver_id=`cat /tmp/max_verid.txt|cut -d "=" -f2`
if [ $max_ver_id != "-" ]
then
 max_ver_id=`expr ${max_ver_id} + 0`
 ${ECHO} "LATEST_VERSION_ID < " $version_date " = " $max_ver_id
else
 ${ECHO} "NO VERSIONS < " $version_date " OR ERROR "
 exit 0
fi
${RM} /tmp/max_verid.txt

db2 -x "select 'MaxItemID=', max(itm_id) MAXITMID from tctg_itm_item where itm_company_id = ${company_id}" |tee /tmp/max_itmid.txt
db2 -x "select 'MinItemID=', min(itm_id) MINITMID from tctg_itm_item where itm_company_id = ${company_id}" |tee /tmp/min_itmid.txt

max_itm_id=`cat /tmp/max_itmid.txt|cut -d "=" -f2`
min_itm_id=`cat /tmp/min_itmid.txt|cut -d "=" -f2`

${RM} /tmp/max_itmid.txt
${RM} /tmp/min_itmid.txt

max_itm_id=`expr ${max_itm_id} + 0`
min_itm_id=`expr ${min_itm_id} + 0`

db2 -v "select count(1) ita_rows from ita where ita_next_version_id <= ${max_ver_id} and ita_company_id = ${company_id} with ur"
db2 -v "select count(1) itm_rows from itm where itm_next_version_id <= ${max_ver_id} and itm_company_id = ${company_id} with ur"
db2 -v "select count(1) icm_rows from icm where icm_next_version_id <= ${max_ver_id} and icm_company_id = ${company_id} with ur"
db2 -v "select count(1) itd_rows from itd where itd_next_version_id <= ${max_ver_id} and itd_company_id = ${company_id} with ur"

crt_index_out_file=$TOP/logs/default/del_version_crt_index.out
del_versions_out_file=$TOP/logs/default/del_versions.out

${ECHO} "========================================"
${ECHO} "== Dropping Indexes on Catalog Tables =="
${ECHO} "========================================"

# drop catalog indexes so that deleting rows doesn't take forever
${GREP} 'drop index' $DB_DIR/gen/idx_ctg_catalog.sql > /tmp/DropCtgIndexes.sql

db2 -tf /tmp/DropCtgIndexes.sql > $crt_index_out_file

${RM} /tmp/DropCtgIndexes.sql

${ECHO} "================================================================="
${ECHO} "== Creating temporary indexes on ITM, ITA, ICM and ITD tables"
${ECHO} "=================================================================="

db2 "create index temp_itm_0 on tctg_itm_item (itm_id,itm_next_version_id,itm_company_id)" > $crt_index_out_file

db2 "create index temp_ita_0 on tctg_ita_item_attributes (ita_item_id,ita_next_version_id,ita_company_id)" >> $crt_index_out_file

db2 "create index temp_icm_0 on tctg_icm_item_category_map (icm_item_id,icm_next_version_id,icm_company_id)" >> $crt_index_out_file

db2 "create index temp_itd_0 on tctg_itd_item_detail (itd_item_id,itd_next_version_id,itd_company_id)" >> $crt_index_out_file

db2 "runstats on table ${db_username}.tctg_itm_item and indexes all allow write access" >> $crt_index_out_file

db2 "runstats on table ${db_username}.tctg_ita_item_attributes and indexes all allow write access" >> $crt_index_out_file

db2 "runstats on table ${db_username}.tctg_icm_item_category_map and indexes all allow write access" >> $crt_index_out_file

db2 "runstats on table ${db_username}.tctg_itd_item_detail and indexes all allow write access" >> $crt_index_out_file

${ECHO} "========================================================"
${ECHO} "== Dropping old versions info from catalog tables"
${ECHO} "========================================================"

${ECHO} $min_itm_id > $del_versions_out_file
while [ ${min_itm_id} -le ${max_itm_id} ]
do
${ECHO} $min_itm_id >> $del_versions_out_file
db2 "delete from ita where ita_item_id <= ${min_itm_id}+100000 and ita_next_version_id <= ${max_ver_id} and ita_company_id = ${company_id}" >> $del_versions_out_file
db2 "delete from itm where itm_id <= ${min_itm_id}+100000 and itm_next_version_id <= ${max_ver_id} and itm_company_id = ${company_id}" >> $del_versions_out_file
db2 "delete from icm where icm_item_id <= ${min_itm_id}+100000 and icm_next_version_id <= ${max_ver_id} and icm_company_id = ${company_id}" >> $del_versions_out_file
db2 "delete from itd where itd_item_id <= ${min_itm_id}+100000 and itd_next_version_id <= ${max_ver_id} and itd_company_id = ${company_id}" >> $del_versions_out_file
db2 "commit"  >> $del_versions_out_file

min_itm_id=`expr ${min_itm_id} + 100000`

done

db2 "delete from caa where caa_next_version_id <= ${max_ver_id} and caa_company_id = ${company_id}"  >> $del_versions_out_file
db2 "commit"  >> $del_versions_out_file
db2 "delete from cta where cta_next_version_id <= ${max_ver_id} and cta_company_id = ${company_id}"  >> $del_versions_out_file
db2 "commit"  >> $del_versions_out_file
db2 "delete from cat where cat_next_version_id <= ${max_ver_id} and cat_company_id = ${company_id}"  >> $del_versions_out_file
db2 "commit"  >> $del_versions_out_file
db2 "delete from cad where cad_next_version_id <= ${max_ver_id} and cad_company_id = ${company_id}"  >> $del_versions_out_file
db2 "commit"  >> $del_versions_out_file
db2 "delete from cab where cab_next_version_id <= ${max_ver_id} and cab_company_id = ${company_id}"  >> $del_versions_out_file
db2 "commit"  >> $del_versions_out_file
db2 "delete from csm where csm_next_version_id <= ${max_ver_id} and csm_company_id = ${company_id}"  >> $del_versions_out_file
db2 "commit"  >> $del_versions_out_file
db2 "delete from csa where csa_next_version_id <= ${max_ver_id} and csa_company_id = ${company_id}"  >> $del_versions_out_file
db2 "commit"  >> $del_versions_out_file
db2 "delete from cfp where cfp_next_version_id <= ${max_ver_id} and cfp_company_id = ${company_id}"  >> $del_versions_out_file
db2 "commit"  >> $del_versions_out_file
db2 "delete from chi where chi_next_version_id <= ${max_ver_id} and chi_company_id = ${company_id}"  >> $del_versions_out_file
db2 "commit"  >> $del_versions_out_file
db2 "delete from ccm where ccm_next_version_id <= ${max_ver_id} and ccm_company_id = ${company_id}"  >> $del_versions_out_file
db2 "commit"  >> $del_versions_out_file
db2 "delete from spa where spa_next_version_id <= ${max_ver_id} and spa_company_id = ${company_id}"  >> $del_versions_out_file
db2 "commit"  >> $del_versions_out_file
db2 "delete from nod where nod_next_version_id <= ${max_ver_id} and nod_company_id = ${company_id}"  >> $del_versions_out_file
db2 "commit"  >> $del_versions_out_file
db2 "delete from noa where noa_next_version_id <= ${max_ver_id} and noa_company_id = ${company_id}"  >> $del_versions_out_file
db2 "commit"  >> $del_versions_out_file
db2 "delete from noh where noh_next_version_id <= ${max_ver_id} and noh_company_id = ${company_id}"  >> $del_versions_out_file
db2 "commit"  >> $del_versions_out_file
db2 "delete from nom where nom_next_version_id <= ${max_ver_id} and nom_company_id = ${company_id}"  >> $del_versions_out_file
db2 "commit"  >> $del_versions_out_file
db2 "delete from obm where obm_next_version_id <= ${max_ver_id} and obm_company_id = ${company_id}"  >> $del_versions_out_file
db2 "commit"  >> $del_versions_out_file
db2 "delete from omd where omd_next_version_id <= ${max_ver_id} and omd_company_id = ${company_id}"  >> $del_versions_out_file
db2 "commit"  >> $del_versions_out_file
db2 "delete from oms where oms_next_version_id <= ${max_ver_id} and oms_company_id = ${company_id}"  >> $del_versions_out_file
db2 "commit"  >> $del_versions_out_file
db2 "delete from cgm where cgm_next_version_id <= ${max_ver_id} and cgm_company_id = ${company_id}"  >> $del_versions_out_file
db2 "commit"  >> $del_versions_out_file
db2 "delete from rul where rul_next_version_id <= ${max_ver_id} and rul_company_id = ${company_id}"  >> $del_versions_out_file
db2 "commit"  >> $del_versions_out_file
db2 "delete from rgm where rgm_next_version_id <= ${max_ver_id} and rgm_company_id = ${company_id}"  >> $del_versions_out_file
db2 "commit"  >> $del_versions_out_file
db2 "delete from tmt where tmt_next_version_id <= ${max_ver_id} and tmt_company_id = ${company_id}"  >> $del_versions_out_file
db2 "commit"  >> $del_versions_out_file
db2 "delete from odd where odd_next_version_id <= ${max_ver_id} and odd_company_id = ${company_id}"  >> $del_versions_out_file
db2 "commit"  >> $del_versions_out_file
db2 "delete from agn where agn_next_version_id <= ${max_ver_id} and agn_company_id = ${company_id}"  >> $del_versions_out_file
db2 "commit"  >> $del_versions_out_file
db2 "delete from agl where agl_next_version_id <= ${max_ver_id} and agl_company_id = ${company_id}"  >> $del_versions_out_file
db2 "commit"  >> $del_versions_out_file
db2 "delete from agd where agd_next_version_id <= ${max_ver_id} and agd_company_id = ${company_id}"  >> $del_versions_out_file
db2 "commit"  >> $del_versions_out_file
db2 "delete from rea where rea_next_version_id <= ${max_ver_id} and rea_company_id = ${company_id}"  >> $del_versions_out_file
db2 "commit"  >> $del_versions_out_file

db2 "delete from ver where ver_date < timestamp_format('${version_date} 00:00:00','YYYY-MM-DD HH24:MI:SS') and ver_id <= ${max_ver_id} and ver_company_id = ${company_id}"  >> $del_versions_out_file
db2 "commit"  >> $del_versions_out_file

# CR 38504 to include LCK, WFE and WEE tables
db2 "delete from lck where lck_object_type = 'ITEM' and not exists (select 1 from itm where lck_object_id = itm_id and itm_next_version_id = 999999999)" >> $del_versions_out_file
db2 "commit"  >> $del_versions_out_file

db2 "delete from lck where lck_object_type = 'WORKFLOW_EVENT' and exists (select 1 from wfe where lck_object_id = wfe_id and date(wfe_start_time) < '${version_date}' and wfe_cmp_id = ${company_id} and date(wfe_start_time) < (current date - 7 days))" >> $del_versions_out_file
db2 "commit"  >> $del_versions_out_file

db2 "delete from wee where exists (select 1 from wfe where wfe_id = wee_event_id and date(wfe_start_time) < '${version_date}' and wfe_cmp_id = ${company_id} and date(wfe_start_time) < (current date - 7 days))" >> $del_versions_out_file
db2 "commit"  >> $del_versions_out_file

db2 "delete from wfe where date(wfe_start_time) < '${version_date}' and date(wfe_start_time) < (current date - 7 days) and wfe_cmp_id = ${company_id}" >> $del_versions_out_file
db2 "commit"  >> $del_versions_out_file

${ECHO} "====================================================="
${ECHO} "== Deleting temporary indexes"
${ECHO} "====================================================="

db2 "drop index temp_itm_0" >> $crt_index_out_file
db2 "drop index temp_ita_0" >> $crt_index_out_file
db2 "drop index temp_icm_0" >> $crt_index_out_file
db2 "drop index temp_itd_0" >> $crt_index_out_file

${ECHO} "========================================================"
${ECHO} "== Re-creating indexes on catalog tables"
${ECHO} "========================================================"

${GREP} -v 'drop index' $DB_DIR/gen/idx_ctg_catalog.sql > /tmp/tmpCreateCtgIndexes.sql

db2 -tf /tmp/tmpCreateCtgIndexes.sql >> $crt_index_out_file

${RM} /tmp/tmpCreateCtgIndexes.sql


${ECHO} "===================================================="
${ECHO} "Analyzing the schema, This could take a while"
${ECHO} "===================================================="

. ${TOP}/src/db/schema/util/analyze_schema.sh --confirm_db=no >> $crt_index_out_file

${ECHO} "===================================================="
${ECHO} " Done - Check log files $TOP/logs/default "
${ECHO} "===================================================="
