#!/bin/bash

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

# 
# # 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}/bin/get_params.sh

cd $TOP
${MKDIR} logs >/dev/null
cd logs
${TOUCH} create_cmp.log >/dev/null

tmplog=${TOP}/logs/create_cmp.log


${ECHO} "========================================================" >> $tmplog
${ECHO} "Event: Creating company starts	at `date`	by  `whoami`" >> $tmplog


#----------------------------------------
#
# usage 
#
#----------------------------------------

if [ "${_CCD_CODE}" = "" ]; then

    ${ECHO} CREATE_COMPANY_INFO_USAGE_SH "$0 --code=<companyCode> [--name=<companyName> --confirm_db=no --confirm_delete=no --qa_setup=no]"

    exit
fi

#----------------------------------------
# get company id
#----------------------------------------

CMP_CODE=${_CCD_CODE}
#i18n_begin by tao 2004-07-12
CMP_LOCALE=${_CCD_LOCALE}
#i18n_end
CMP_NAME=${_CCD_NAME}
QA_SETUP=${_CCD_QA_SETUP}
if [ -z "$CMP_NAME" ]; then
    export CMP_NAME=$CMP_CODE
fi

${ECHO}
${ECHO} CREATE_COMPANY_INFO_COMPANY_NAME_SH $CMP_NAME
#i18n_begin by tao 2004-07-12
${ECHO} CREATE_COMPANY_INFO_COMPANY_LOCALE_SH $CMP_LOCALE
#i18n_end
${ECHO} CREATE_COMPANY_INFO_COMPANY_CODE_SH $CMP_CODE
. $DB_DIR/util/set_db_localized.sh

${ECHO} ====================================================
${ECHO} CREATE_COMPANY_INFO_CLEANUP_DATA_SH 
${ECHO} ====================================================

. ${DB_DIR}/util/cleanup_cmp_localized.sh --confirm_db=no --code="$CMP_CODE" --db_output=no 

# Get CMP_ID_PREFIX
. ${DB_DIR}/cmp/get_cmpid_prefix.sh --confirm_db=no


#----------------------------------------
# init vars
#----------------------------------------

CMP_DIR=$DB_DIR/cmp

DML_SQL_SRC_FILE=${CMP_DIR}/dml_create_cmp.sql
DML_SQL_FILE=${CMP_DIR}/tmpcreatecmp.sql

CMP_PREFIX=$CMP_ID_PREFIX
CMP_COMPANY_ID="${CMP_PREFIX}0001"

BASIC_SCRIPT_SRC_FILE=${CMP_DIR}/basic_company_setup.script
BASIC_SCRIPT_FILE=${CMP_DIR}/basic_company_setup.script.tmp

# Get Sequence Values
. ${CMP_DIR}/set_create_cmp.sh --confirm_db=no


${ECHO} ====================================================
${ECHO} CREATE_COMPANY_INFO_CREATE_SQL_SH 
${ECHO} ====================================================

cd $CMP_DIR
${CAT} ${DML_SQL_SRC_FILE} \
    | ${SED} "s/%CMP_CODE%/$CMP_CODE/g" \
    | ${SED} "s/%CMP_NAME%/$CMP_NAME/g" \
    | ${SED} "s/%CMP_COMPANY_ID%/$CMP_COMPANY_ID/g" \
    | ${SED} "s/%CMP_PREFIX%/$CMP_PREFIX/g" \
    > ${DML_SQL_FILE}

${CAT} ${BASIC_SCRIPT_SRC_FILE} \
    | ${SED} "s/%CMP_CODE%/$CMP_CODE/g" \
    | ${SED} "s/%CMP_NAME%/$CMP_NAME/g" \
    > ${BASIC_SCRIPT_FILE}

${ECHO}
${ECHO} ====================================================
${ECHO} CREATE_COMPANY_INFO_POPULATE_DATA_SH 
${ECHO} ====================================================
${ECHO}

cd $CMP_DIR

. ${DB_DIR}/util/run_db_cmd.sh --file=${DML_SQL_FILE} >> $tmplog

${ECHO}
${ECHO} ====================================================
${ECHO} CREATE_COMPANY_INFO_BUILD_OBJ_SH 
${ECHO} ====================================================
${ECHO}

#i18n_comment_out by tao
#${JAVA_RT} com.ibm.ccd.common.interpreter.test.TestScript --companycode="$CMP_CODE" \
#                                              --username="trigo_root" \
#                                              --scriptpath="${BASIC_SCRIPT_FILE}"

#i18n_begin by tao 2004-07-12 pass company locale argument
${JAVA_RT} com.ibm.ccd.common.interpreter.test.TestScript --companycode="$CMP_CODE" \
					      --companylocale="$CMP_LOCALE" \
                                              --username="trigo_root" \
                                              --scriptpath="${BASIC_SCRIPT_FILE}" >> $tmplog
#i18n_end
${ECHO}
${ECHO} ====================================================
${ECHO} CREATE_COMPANY_INFO_LOAD_SCRIPT_SH 
${ECHO} ====================================================
${ECHO}
$JAVA_RT_NOOUT com.ibm.ccd.docstore.util.DocLoader "$CMP_CODE" Admin ${SCRIPTS_DIR}/common/common_scripts.xml Cp1252 >> $tmplog

$JAVA_RT_NOOUT com.ibm.ccd.docstore.util.DocLoader "$CMP_CODE" Admin ${SCRIPTS_DIR}/common/ldap_scripts.xml Cp1252 >> $tmplog
${RM} ${DML_SQL_FILE}
${RM} ${BASIC_SCRIPT_FILE}

${ECHO} "Event: Creating company ends	at `date`	by  `whoami`" >> $tmplog
${ECHO} "========================================================" >> $tmplog

