#!/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}"

db2 "select 'ITA ROWS BEFORE ${version_date} =', count(1) ita_rows from ita where ita_next_version_id <= (select max(ver_id) VER_ID from ver where date(ver_date) <  '${version_date}') and ita_company_id =  ${company_id} with ur"

db2 "select 'ITM ROWS BEFORE ${version_date} =', count(1) itm_rows from itm where itm_next_version_id <= (select max(ver_id) VER_ID from ver where date(ver_date) <  '${version_date}') and itm_company_id = ${company_id} with ur"

db2 "select 'ICM ROWS BEFORE ${version_date} =', count(1) icm_rows from icm where icm_next_version_id <= (select max(ver_id) VER_ID from ver where date(ver_date) < '${version_date}') and icm_company_id = ${company_id} with ur"

db2 "select 'ITD ROWS BEFORE ${version_date} =', count(1) itd_rows from itd where itd_next_version_id <= (select max(ver_id) VER_ID from ver where date(ver_date) < '${version_date}') and itd_company_id = ${company_id} with ur"

db2 "select 'LCK ROWS OF OBSOLETE ITEMS =', count(1) lck_rows 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) with ur"

db2 "select 'LCK WORKFLOW ROWS BEFORE ${version_date} =', count(1) lck_rows 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)) with ur"

db2 "select 'WEE ROWS BEFORE ${version_date} =', count(1) wee_rows 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)) with ur"

db2 "select 'WFE ROWS BEFORE ${version_date} =', count(1) wfe_rows from wfe where date(wfe_start_time) < '${version_date}' and date(wfe_start_time) < (current date - 7 days) and wfe_cmp_id = ${company_id} with ur"
