#!/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.


# finds the company id of a company (using the company code)
# sets a variable CMP_ID with the result (empty if not found)

OUTPUT_FILE="${TOP}/logs/tmp_cmp_id.log"

. ${TOP}/bin/get_params.sh

if [ "${_CCD_CODE}" = "" ]; then
    ${ECHO} "Usage: $0 --code=<cmp code>"
    exit
fi

. ${DB_DIR}/util/set_db.sh --db_output=no


#${CCD_SQLPLUS} ${CONNECTION} <<EOF
#set head off
#set feedback off
#spool ${OUTPUT_FILE}
#select cmp_company_id from tsec_cmp_company where cmp_company_code='${_CCD_CODE}';
#EOF

#CMP_ID=`${GREP} -v 'SQL' $TOP/logs/tmp_cmp_id.log|tail -1|${SED} -e 's/ //g'`

#----------------------------------------------------------------------


# Getting Company Id for the TRIGO CODE

${TOUCH} tempcmpid.sql

#${ECHO} "Getting Company Id for the $_CCD_CODE"

${CAT} $DB_SCRIPTS/get_cmpid_for_cmpcode.sql | ${SED} "s/%_CCD_CODE%/$_CCD_CODE/g" > tempcmpid.sql

#${CAT} tempcmpid.sql

#${CCD_SQLPLUS} ${CONNECTION} <<EOF 2>&1 | ${GREP} 'cmpid-'
#${DB_SQLFILE_EXECUTE} tempcmpid.sql
#EOF


#CMP_ID_QRY=`${CCD_SQLPLUS} ${CONNECTION} <<EOF 2>&1 | ${GREP} 'cmpid-'
#${DB_SQLFILE_EXECUTE} tempcmpid.sql
#EOF`
#${ECHO} "Before running Query "

#. ${DB_DIR}/util/run_db_cmd.sh --file=tempcmpid.sql --errorfilter=yes


#pvedul -- adding run_db_cmd.sh
CMP_ID_QRY=`. ${DB_DIR}/util/run_db_cmd.sh --file=tempcmpid.sql --errorfilter=yes | ${GREP} 'cmpid-'`

#${ECHO} "After running query "

#${ECHO} $CMP_ID_QRY


CMP_QRY_VAL="${CMP_ID_QRY}"

#${ECHO}  "query val ${CMP_QRY_VAL}"

CMP_ID=`${ECHO} $CMP_QRY_VAL | ${GREP} 'cmpid-' | cut -f2 -d "-"`

#TMP_CMP_ID=" "

#for i in $CMP_QRY_VAL
#do
	#${ECHO} $i
#	TMP_CMP_ID=`${ECHO} $i | ${GREP} 'cmpid-' | cut -f2 -d "-"`	
#	${ECHO} $TMP_CMP_ID
#	if [ ! -z $TMP_CMP_ID ]; then
#		${ECHO} no empty
#		CMP_ID="$TEMP_CMP_ID"
#		${ECHO} "$TEMP_CMP_ID"
#		break
#	fi
#done

${ECHO} "Company ID found $CMP_ID"

${RM} tempcmpid.sql



