#!/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.
# 
# # 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}/src/db/schema/util/set_db_localized.sh
_ERROR_LOGS=$TOP/logs/estimate_old_verions.out
tmpfile=$TOP/logs/del_version_temp.out
tmpsqlfile=$TOP/logs/del_version_temp.sql

START_TIME=`date`
${ECHO} "Last update of script: MDMPIM 6.0.0-FP004 2009-06-02" > $_ERROR_LOGS

if [ "${_CCD_CODE}" = "" ]; then
    ${ECHO} "Usage: $0 --code=<company code> --end-date=<YYYY-MM-DD.HH24:MI:SS>"
    exit
fi

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

if [ "$CMP_ID" != "" ]; then
#  ${ECHO} $CMP_ID_QRY
  ${ECHO} "COMPANY ID = " $CMP_ID >> $_ERROR_LOGS
  ${RM} tmpcmpid.sql
else 
  #. ${TOP}/src/db/schema/util/run_db_cmd.sh --file=tmpcmpid.sql
  ${ECHO} "COMMON_INFO_NO_COMPANY_CODE_SH" ${_CCD_CODE}
  exit 0
fi

${ECHO} "END DATE = "   $_CCD_END_DATE >> $_ERROR_LOGS
${ECHO} "CATALOG ID = " $_CCD_CTG >> $_ERROR_LOGS
${ECHO} "START DATE = " $_CCD_START_DATE >> $_ERROR_LOGS

sdate_flag=0
ctg_flag=0
if [ $_CCD_CTG ] && [ $_CCD_START_DATE ]; then
  #echo "ctg and start date exists"
  ctg_flag=1
  sdate_flag=1
else  
  if [ $_CCD_CTG ]; then
    #echo "ctg only exists"
    ctg_flag=1
  elif [ $_CCD_START_DATE ]; then
    #echo "start only exists"
    sdate_flag=1
  fi
fi

cleanup_exit()
{
${RM} $tmpfile
${RM} $tmpsqlfile
exit
}

ctg_id_check()
{
${ECHO} "select 'ctgid= ', ctg_id from ctg where ctg_id = $_CCD_CTG and ctg_type='CATALOG' and ctg_company_id = $CMP_ID;" > $tmpsqlfile
. ${TOP}/src/db/schema/util/run_db_cmd.sh --file=$tmpsqlfile > $tmpfile
}

# catalog id check
if [ $ctg_flag -eq 1 ]; then
  ctg_id_check
  if [ `${GREP} -c -i error $tmpfile` -gt 0 ]; then
    ${ECHO} "COMMON_INFO_NO_CTG_ID_SH" $_CCD_CTG 
    cleanup_exit
  fi
  ita_ctg="and ita_catalog_id = $_CCD_CTG"
  itd_ctg="and itd_container_id = $_CCD_CTG"
  icm_ctg="and icm_catalog_id = $_CCD_CTG"
  caa_ctg="and caa_catalog_id = $_CCD_CTG"  
  itm_ctg="and itm_container_id = $_CCD_CTG"
fi

db2_date_check()
{
dt_part1=`${ECHO} $dt |${CUT} -d"." -f1`
dt_part2=`${ECHO} $dt |${CUT} -d"." -f2`
#echo $dt_part1 $dt_part2
${ECHO} "select 'date= ' , timestamp_format('$dt_part1 $dt_part2','YYYY-MM-DD HH24:MI:SS') chk from sysibm.sysdummy1;" > $tmpsqlfile
. ${TOP}/src/db/schema/util/run_db_cmd.sh --file=$tmpsqlfile > $tmpfile
}

oracle_date_check()
{
${ECHO} "select 'date= ' || to_date('$dt','YYYY-MM-DD.HH24:MI:SS') chk_date from dual;" > $tmpsqlfile
. ${TOP}/src/db/schema/util/run_db_cmd.sh --file=$tmpsqlfile > $tmpfile
}

db2_min_max_version()
{
${ECHO} "select 'max_version= ', coalesce(char(max(ver_id)),'') from ver where ver_date < timestamp_format('$END_DATE','YYYY-MM-DD HH24:MI:SS') and ver_company_id in ($CMP_ID,-1);" > $tmpsqlfile
MAX_VERSION_ID=`. ${TOP}/src/db/schema/util/run_db_cmd.sh --file=$tmpsqlfile | ${GREP} 'max' | cut -f2 -d"="`
${ECHO} "select 'min_version= ', coalesce(char(min(ver_id)),'') from ver where ver_date > timestamp_format('$START_DATE','YYYY-MM-DD HH24:MI:SS') and ver_company_id in ($CMP_ID,-1);" > $tmpsqlfile
MIN_VERSION_ID=`. ${TOP}/src/db/schema/util/run_db_cmd.sh --file=$tmpsqlfile | ${GREP} 'min' | cut -f2 -d"="`
${ECHO} "START VERSION ID = " $MIN_VERSION_ID "; END VERSION ID = " $MAX_VERSION_ID >> $_ERROR_LOGS
}

oracle_min_max_version()
{
${ECHO} "select 'max_version= ', max(ver_id) from ver where ver_date < to_date('$_CCD_END_DATE','YYYY-MM-DD.HH24:MI:SS') and ver_company_id in ($CMP_ID,-1);" > $tmpsqlfile
MAX_VERSION_ID=`. ${TOP}/src/db/schema/util/run_db_cmd.sh --file=$tmpsqlfile | ${GREP} 'max' | cut -f2 -d"="`
${ECHO} "select 'min_version= ', min(ver_id) from ver where ver_date > to_date('$_CCD_START_DATE','YYYY-MM-DD.HH24:MI:SS') and ver_company_id in ($CMP_ID,-1);" > $tmpsqlfile
MIN_VERSION_ID=`. ${TOP}/src/db/schema/util/run_db_cmd.sh --file=$tmpsqlfile | ${GREP} 'min' | cut -f2 -d"="`
${ECHO} "START VERSION ID = " $MIN_VERSION_ID "; END VERSION ID = " $MAX_VERSION_ID >> $_ERROR_LOGS
}

min_max_item_id()
{
${ECHO} "select 'max_id= ', max(itm_id) from itm where itm_company_id = $CMP_ID;" > $tmpsqlfile
MAX_ITEM_ID=`. ${TOP}/src/db/schema/util/run_db_cmd.sh --file=$tmpsqlfile | ${GREP} 'max' | cut -f2 -d"="`
${ECHO} "select 'min_id= ', min(itm_id) from itm where itm_company_id = $CMP_ID;" > $tmpsqlfile
MIN_ITEM_ID=`. ${TOP}/src/db/schema/util/run_db_cmd.sh --file=$tmpsqlfile | ${GREP} 'min' | cut -f2 -d"="`
${ECHO} "START ITEM ID = " $MIN_ITEM_ID "; END ITEM ID = " $MAX_ITEM_ID >> $_ERROR_LOGS
}

estimate_core_tables()
{
date >> $_ERROR_LOGS
${ECHO} "
select count(1) ITA_COUNT from ita where ita_item_id between $MIN_ITEM_ID and $MAX_ITEM_ID  $ita_ctg and ita_next_version_id between $MIN_VERSION_ID and $MAX_VERSION_ID and ita_version_id between $MIN_VERSION_ID and $MAX_VERSION_ID and ita_company_id = $CMP_ID;
select count(1) ITD_COUNT from itd where itd_item_id between $MIN_ITEM_ID and $MAX_ITEM_ID  $itd_ctg and itd_next_version_id between $MIN_VERSION_ID and $MAX_VERSION_ID and itd_version_id between $MIN_VERSION_ID and $MAX_VERSION_ID and itd_company_id = $CMP_ID;
select count(1) ICM_COUNT from icm where icm_item_id between $MIN_ITEM_ID and $MAX_ITEM_ID  $icm_ctg and icm_next_version_id between $MIN_VERSION_ID and $MAX_VERSION_ID and icm_version_id between $MIN_VERSION_ID and $MAX_VERSION_ID and icm_company_id = $CMP_ID;
select count(1) ITM_COUNT from itm where itm_id between $MIN_ITEM_ID and $MAX_ITEM_ID $itm_ctg and itm_next_version_id between $MIN_VERSION_ID and $MAX_VERSION_ID and itm_version_id between $MIN_VERSION_ID and $MAX_VERSION_ID and itm_company_id = $CMP_ID;
select count(1) LCK_COUNT from lck where lck_object_id between $MIN_ITEM_ID and $MAX_ITEM_ID and lck_object_type = 'ITEM' and not exists (select 1 from itm where lck_object_id = itm_id and itm_next_version_id = 999999999) ;
COMMIT; " > $tmpsqlfile
. ${TOP}/src/db/schema/util/run_db_cmd.sh --file=$tmpsqlfile >> $_ERROR_LOGS
}
#-------------------------------------------------------------------------------
if [ "$CCD_DB" = "db2" ]; then
  #echo db2

  dt=$_CCD_END_DATE
  db2_date_check
  if [ `${GREP} -c -i ^SQL $tmpfile` -gt 0 ]; then
    ${ECHO} "COMMON_INFO_DATE_ERROR_SH" $dt 
    cleanup_exit
  fi
  END_DATE=`${ECHO} "$dt_part1 $dt_part2"`
  
  # start date check
  if [ $sdate_flag -eq 1 ]; then
    dt=$_CCD_START_DATE
  else
    dt='1111-11-11.11:11:11'
  fi
  db2_date_check
  if [ `${GREP} -c -i ^SQL $tmpfile` -gt 0 ]; then
    ${ECHO} "COMMON_INFO_DATE_ERROR_SH" $dt 
    cleanup_exit
  fi
  START_DATE=`${ECHO} "$dt_part1 $dt_part2"`
  MAX_VERSION_ID=""
  MIN_VERSION_ID=""
  db2_min_max_version
  if [ ! $MAX_VERSION_ID ]; then
    ${ECHO} "COMMON_INFO_NO_VERSION_SH"
    cleanup_exit
  fi
  if [ ! $MIN_VERSION_ID ]; then
    ${ECHO} "COMMON_INFO_NO_VERSION_SH"
    cleanup_exit
  fi
  WEE_QUERY="select count(1) WORKFLOW_COUNT1 from wee where exists (select 1 from wfe where wfe_id = wee_event_id and wfe_event_status in ('DONE','ERROR') and (wfe_start_time < timestamp_format('$END_DATE','YYYY-MM-DD HH24:MI:SS') or wfe_start_time < (current timestamp - 7 days))  and wfe_cmp_id = $CMP_ID);"
  WFE_QUERY="select count(1) WORKFLOW_COUNT2 from wfe where wfe_event_status  in ('DONE','ERROR') and (wfe_start_time < timestamp_format('$END_DATE','YYYY-MM-DD HH24:MI:SS') or wfe_start_time < (current timestamp - 7 days))  and wfe_cmp_id = $CMP_ID;"
#-------------------------------------------------------------------------------  
elif [ "$CCD_DB" = "oracle" ]; then

  dt=$_CCD_END_DATE
  oracle_date_check
  if [ `${GREP} -c -i error $tmpfile` -gt 0 ]; then
    ${ECHO} "COMMON_INFO_DATE_ERROR_SH" $dt 
    cleanup_exit
  fi

  # start date check
  if [ $sdate_flag -eq 0 ]; then
    _CCD_START_DATE='1111-11-11.11:11:11'
  fi
  dt=$_CCD_START_DATE
  oracle_date_check
  if [ `${GREP} -c -i error $tmpfile` -gt 0 ]; then
    ${ECHO} "COMMON_INFO_DATE_ERROR_SH" $dt
    cleanup_exit 
  fi
  MAX_VERSION_ID=""
  MIN_VERSION_ID=""
  oracle_min_max_version
  if [ ! $MAX_VERSION_ID ]; then
    ${ECHO} "COMMON_INFO_NO_VERSION_SH"
    cleanup_exit
  fi
  if [ ! $MIN_VERSION_ID ]; then
    ${ECHO} "COMMON_INFO_NO_VERSION_SH"
    cleanup_exit
  fi
  WEE_QUERY="select count(1) WORKFLOW_COUNT1 from wee where exists (select 1 from wfe where wfe_id = wee_event_id and wfe_event_status in ('DONE','ERROR') and (wfe_start_time < to_date('$_CCD_END_DATE','YYYY-MM-DD.HH24:MI:SS') or wfe_start_time < sysdate - 7)  and wfe_cmp_id = $CMP_ID);"
  WFE_QUERY="select count(1) WORKFLOW_COUNT2 from wfe where wfe_event_status = 'DONE' and (wfe_start_time < to_date('$_CCD_END_DATE','YYYY-MM-DD.HH24:MI:SS') or wfe_start_time < sysdate - 7)  and wfe_cmp_id = $CMP_ID;"
#-------------------------------------------------------------------------------  
else  
  ${ECHO} "COMMON_INFO_UNSET_ENV_SH"
  cleanup_exit 
fi

${ECHO} "COMMON_INFO_EXECUTING_SCRIPT_SH" "estimate_old_versions.sh"

MAX_ITEM_ID=""
MIN_ITEM_ID=""
min_max_item_id
if [ ! $MAX_ITEM_ID ]; then
  ${ECHO} "COMMON_INFO_NO_VERSION_SH"
  cleanup_exit
else
  #check for db2
  if [ $MAX_ITEM_ID = "-" ]; then
    ${ECHO} "COMMON_INFO_NO_VERSION_SH"
    cleanup_exit
  fi
fi
if [ ! $MIN_ITEM_ID ]; then
  ${ECHO} "COMMON_INFO_NO_VERSION_SH"
  cleanup_exit
else  
  if [ $MIN_ITEM_ID = "-" ]; then
    ${ECHO} "COMMON_INFO_NO_VERSION_SH"
    cleanup_exit
  fi
fi

estimate_core_tables
date >> $_ERROR_LOGS

${ECHO} "select count(1) CAA_COUNT from caa where caa_next_version_id between $MIN_VERSION_ID and $MAX_VERSION_ID and caa_company_id = $CMP_ID $caa_ctg;" > $tmpsqlfile
. ${TOP}/src/db/schema/util/run_db_cmd.sh --file=$tmpsqlfile >> $_ERROR_LOGS
date >> $_ERROR_LOGS
${ECHO} $WEE_QUERY > $tmpsqlfile
. ${TOP}/src/db/schema/util/run_db_cmd.sh --file=$tmpsqlfile >> $_ERROR_LOGS
date >> $_ERROR_LOGS
${ECHO} $WFE_QUERY > $tmpsqlfile
. ${TOP}/src/db/schema/util/run_db_cmd.sh --file=$tmpsqlfile >> $_ERROR_LOGS
date >> $_ERROR_LOGS

if [ $ctg_flag -eq 0 ]; then
${ECHO} "
select count(1) EEM_COUNT from eem where eem_next_version_id between $MIN_VERSION_ID and $MAX_VERSION_ID and eem_version_id between $MIN_VERSION_ID and $MAX_VERSION_ID and eem_company_id = $CMP_ID; 
commit;                                                                                                                                                                                               
select count(1) CAB_COUNT from cab where cab_next_version_id between $MIN_VERSION_ID and $MAX_VERSION_ID and cab_version_id between $MIN_VERSION_ID and $MAX_VERSION_ID and cab_company_id = $CMP_ID; 
commit;                                                                                                                                                                                               
select count(1) CAT_COUNT from cat where cat_next_version_id between $MIN_VERSION_ID and $MAX_VERSION_ID and cat_version_id between $MIN_VERSION_ID and $MAX_VERSION_ID and cat_company_id = $CMP_ID; 
commit;                                                                                                                                                                                               
select count(1) CCM_COUNT from ccm where ccm_next_version_id between $MIN_VERSION_ID and $MAX_VERSION_ID and ccm_version_id between $MIN_VERSION_ID and $MAX_VERSION_ID and ccm_company_id = $CMP_ID; 
commit;                                                                                                                                                                                               
select count(1) CGM_COUNT from cgm where cgm_next_version_id between $MIN_VERSION_ID and $MAX_VERSION_ID and cgm_version_id between $MIN_VERSION_ID and $MAX_VERSION_ID and cgm_company_id = $CMP_ID; 
commit;                                                                                                                                                                                               
select count(1) CHI_COUNT from chi where chi_next_version_id between $MIN_VERSION_ID and $MAX_VERSION_ID and chi_version_id between $MIN_VERSION_ID and $MAX_VERSION_ID and chi_company_id = $CMP_ID; 
commit;                                                                                                                                                                                               
select count(1) CSA_COUNT from csa where csa_next_version_id between $MIN_VERSION_ID and $MAX_VERSION_ID and csa_version_id between $MIN_VERSION_ID and $MAX_VERSION_ID and csa_company_id = $CMP_ID; 
commit;                                                                                                                                                                                               
select count(1) CSM_COUNT from csm where csm_next_version_id between $MIN_VERSION_ID and $MAX_VERSION_ID and csm_version_id between $MIN_VERSION_ID and $MAX_VERSION_ID and csm_company_id = $CMP_ID; 
commit;                                                                                                                                                                                               
select count(1) CTA_COUNT from cta where cta_next_version_id between $MIN_VERSION_ID and $MAX_VERSION_ID and cta_version_id between $MIN_VERSION_ID and $MAX_VERSION_ID and cta_company_id = $CMP_ID; 
commit;                                                                                                                                                                                               
select count(1) NOA_COUNT from noa where noa_next_version_id between $MIN_VERSION_ID and $MAX_VERSION_ID and noa_version_id between $MIN_VERSION_ID and $MAX_VERSION_ID and noa_company_id = $CMP_ID; 
commit;                                                                                                                                                                                               
select count(1) NOD_COUNT from nod where nod_next_version_id between $MIN_VERSION_ID and $MAX_VERSION_ID and nod_version_id between $MIN_VERSION_ID and $MAX_VERSION_ID and nod_company_id = $CMP_ID; 
commit;                                                                                                                                                                                               
select count(1) NOH_COUNT from noh where noh_next_version_id between $MIN_VERSION_ID and $MAX_VERSION_ID and noh_version_id between $MIN_VERSION_ID and $MAX_VERSION_ID and noh_company_id = $CMP_ID; 
commit;                                                                                                                                                                                               
select count(1) OBM_COUNT from obm where obm_next_version_id between $MIN_VERSION_ID and $MAX_VERSION_ID and obm_version_id between $MIN_VERSION_ID and $MAX_VERSION_ID and obm_company_id = $CMP_ID; 
commit;                                                                                                                                                                                               
select count(1) OMD_COUNT from omd where omd_next_version_id between $MIN_VERSION_ID and $MAX_VERSION_ID and omd_version_id between $MIN_VERSION_ID and $MAX_VERSION_ID and omd_company_id = $CMP_ID; 
commit;                                                                                                                                                                                               
select count(1) OMS_COUNT from oms where oms_next_version_id between $MIN_VERSION_ID and $MAX_VERSION_ID and oms_version_id between $MIN_VERSION_ID and $MAX_VERSION_ID and oms_company_id = $CMP_ID; 
commit;                                                                                                                                                                                               
select count(1) RUL_COUNT from rul where rul_next_version_id between $MIN_VERSION_ID and $MAX_VERSION_ID and rul_version_id between $MIN_VERSION_ID and $MAX_VERSION_ID and rul_company_id = $CMP_ID; 
commit;                                                                                                                                                                                               
select count(1) SPA_COUNT from spa where spa_next_version_id between $MIN_VERSION_ID and $MAX_VERSION_ID and spa_version_id between $MIN_VERSION_ID and $MAX_VERSION_ID and spa_company_id = $CMP_ID; 
commit;                                                                                                                                                                                               
select count(1) TMT_COUNT from tmt where tmt_next_version_id between $MIN_VERSION_ID and $MAX_VERSION_ID and tmt_version_id between $MIN_VERSION_ID and $MAX_VERSION_ID and tmt_company_id = $CMP_ID; 
commit;
select count(1) VER_COUNT from ver where ver_id between $MIN_VERSION_ID and $MAX_VERSION_ID and ver_company_id in ($CMP_ID,-1);
commit;
" > $tmpsqlfile
else
${ECHO} "
select count(1) CTG_VERSIONS from ver where ver_id between $MIN_VERSION_ID and $MAX_VERSION_ID and ver_object_id = $_CCD_CTG and ver_object_type = 'CTG' and ver_company_id = $CMP_ID;
commit;                                                                                                                        
" > $tmpsqlfile                                                                                                                
fi
. ${TOP}/src/db/schema/util/run_db_cmd.sh --file=$tmpsqlfile >> $_ERROR_LOGS
date >> $_ERROR_LOGS                                                        

END_TIME=`date` 
${ECHO} "START:" $START_TIME
${ECHO} "END  :" $END_TIME

${ECHO} "COMMON_INFO_CHECK_ERROR_LOG_FILE_SH" $_ERROR_LOGS

cleanup_exit
