#!/bin/bash

if [[ $# -lt 6 ]]
then
    ${ECHO} "Invalid number of arguments."
    ${ECHO} "Usage: $0 --cmpid=<CompanyID> --ctgid=<CatalogId> --date=YYYY-MM-DD --dbuser=<dbuser> --dbpassword=<password> --dbname=<dbname>"
 exit
fi

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

${ECHO} "Company ID : ${company_id}"
${ECHO} "Date : ${version_date}"
${ECHO} "DB User Name : ${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} and ver_object_id = ${catalog_id} and ver_object_type = 'CTG'" |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 before " $version_date " is " $max_ver_id
else
 ${ECHO} "no versions before " $version_date " or check for error"
 exit 0
fi
${RM} /tmp/max_verid.txt

db2 -x "select 'MaxItemID=', max(itm_id) MAXITMID from tctg_itm_item where itm_container_id =  ${catalog_id} and itm_company_id = ${company_id}" |tee /tmp/max_itmid.txt
db2 -x "select 'MinItemID=', min(itm_id) MINITMID from tctg_itm_item where itm_container_id =  ${catalog_id} and 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`

${ECHO} "========================================================"
${ECHO} "== Delete estimates"
${ECHO} "========================================================"

db2 -v "select count(1) ita_rows from ita where ita_next_version_id <= ${max_ver_id} and ita_company_id = ${company_id} and ita_catalog_id = ${catalog_id} with ur"
db2 -v "select count(1) itm_rows from itm where itm_next_version_id <= ${max_ver_id} and itm_container_id = ${catalog_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_catalog_id = ${catalog_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_container_id = ${catalog_id} and itd_company_id = ${company_id} with ur"

${ECHO} "========================================================"
${ECHO} "== Dropping indexes from 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  > $TOP/logs/default/del_version_drop_indexes.out

${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_container_id,itm_company_id)" > $TOP/logs/default/del_version_crt_index.out

db2 "create index temp_ita_0 on tctg_ita_item_attributes (ita_item_id,ita_next_version_id,ita_catalog_id,ita_company_id)" >> $TOP/logs/default/del_version_crt_index.out
db2 "create index temp_icm_0 on tctg_icm_item_category_map (icm_item_id,icm_next_version_id,icm_catalog_id,icm_company_id)" >> $TOP/logs/default/del_version_crt_index.out

db2 "create index temp_itd_0 on tctg_itd_item_detail (itd_item_id,itd_next_version_id,itd_container_id,itd_company_id)" >> $TOP/logs/default/del_version_crt_index.out

db2 "runstats on table ${db_username}.tctg_itm_item and indexes all allow write access" > $TOP/logs/default/del_version_crt_index.out

db2 "runstats on table ${db_username}.tctg_ita_item_attributes and indexes all allow write access" > $TOP/logs/default/del_version_crt_index.out

db2 "runstats on table ${db_username}.tctg_icm_item_category_map and indexes all allow write access" > $TOP/logs/default/del_version_crt_index.out

db2 "runstats on table ${db_username}.tctg_itd_item_detail and indexes all allow write access" > $TOP/logs/default/del_version_crt_index.out

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

${ECHO} $min_itm_id > $TOP/logs/default/del_versions.out
while [ ${min_itm_id} -le ${max_itm_id} ]
do
${ECHO} $min_itm_id
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} and ita_catalog_id = ${catalog_id}" >> $TOP/logs/default/del_versions.out
db2 "delete from itm where itm_id <= ${min_itm_id}+100000 and itm_next_version_id <= ${max_ver_id} and itm_container_id = ${catalog_id} and itm_company_id = ${company_id}" >> $TOP/logs/default/del_versions.out
db2 "delete from icm where icm_item_id <= ${min_itm_id}+100000 and icm_next_version_id <= ${max_ver_id} and icm_catalog_id = ${catalog_id} and icm_company_id = ${company_id}" >> $TOP/logs/default/del_versions.out
db2 "delete from itd where itd_item_id <= ${min_itm_id}+100000 and itd_next_version_id <= ${max_ver_id} and itd_container_id = ${catalog_id} and itd_company_id = ${company_id}" >> $TOP/logs/default/del_versions.out
db2 "commit" >> $TOP/logs/default/del_versions.out

min_itm_id=`expr ${min_itm_id} + 100000`

done

db2 "delete from caa where caa_next_version_id <= ${max_ver_id} and caa_catalog_id = ${catalog_id} and caa_company_id = ${company_id}"
db2 "commit" >> $TOP/logs/default/del_versions.out

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} and ver_object_id = ${catalog_id} and ver_object_type = 'CTG'"
db2 "commit" >> $TOP/logs/default/del_versions.out

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

db2 "drop index temp_itm_0" >> $TOP/logs/default/del_version_drop_indexes.out
db2 "drop index temp_ita_0" >> $TOP/logs/default/del_version_drop_indexes.out
db2 "drop index temp_icm_0" >> $TOP/logs/default/del_version_drop_indexes.out
db2 "drop index temp_itd_0" >> $TOP/logs/default/del_version_drop_indexes.out

${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 >> $TOP/logs/default/del_version_crt_index.out

${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 >> $TOP/logs/default/del_version_crt_index.out

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