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

db2 "select 'ITA ROWS IN ${catalog_id} 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_catalog_id = ${catalog_id} and ita_company_id =  ${company_id} with ur"

db2 "select 'ITM ROWS IN ${catalog_id} 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_container_id = ${catalog_id} and itm_company_id = ${company_id} with ur"

db2 "select 'ICM ROWS IN ${catalog_id} 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_catalog_id = ${catalog_id} and icm_company_id = ${company_id} with ur"

db2 "select 'ITD ROWS IN ${catalog_id} 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_container_id = ${catalog_id} and itd_company_id = ${company_id} with ur"
