#!/bin/bash
# IBM Confidential
# OCO Source Materials
# 5724-V51
#
# (c) Copyright IBM Corp. 2000, 2008 All Rights Reserved.
#
# The source code for this program is not published or otherwise
# divested of its trade secrets, irrespective of what has been
# deposited with the U.S. Copyright Office.

# 
# # 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} COMMON_INFO_USAGE_SH "$0 --code=<companyCode> [--name=<companyName> --confirm_db=no --confirm_delete=no --qa_setup=no]"

    exit
fi

TXT4="Schema synchronization error. The database and codeline are not in step."
TXT5="To avoid possible data corruption the application will not be started"	
TXT6="You must update your Schema before proceeding"	
${JAVA_RT} com.ibm.ccd.synchronize.DBSchemaVersion --verify > $TOP/schemaValid.txt
NOSYNCH=`${GREP} "False" $TOP/schemaValid.txt|${AWK} '{print $1}'`
if [ $NOSYNCH ]; then
	${ECHO} 
	${ECHO} ${TXT4}
	${ECHO} ${TXT5}	
	${ECHO} 
	${ECHO} ${TXT6}							
        ${RM} $TOP/schemaValid.txt	
        exit   
else    
        ${RM} $TOP/schemaValid.txt    
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} COMMON_INFO_COMPANY_NAME_SH $CMP_NAME
#i18n_begin by tao 2004-07-12
${ECHO} COMMON_INFO_COMPANY_LOCALE_SH $CMP_LOCALE
#i18n_end
${ECHO} COMMON_INFO_COMPANY_CODE_SH $CMP_CODE
. $DB_DIR/util/set_db_localized.sh

${ECHO} ====================================================
${ECHO} COMMON_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

# Krishna - if the company to be recreated is trigo, the default 
# company, then we branch off from here and use the existing 
# create_ccd.sh script which is the script used to create trigo
# company when a new wpc db is created.

if [[ $CMP_CODE = "trigo" ]]
then
	CMP_COMPANY_ID="1"

	${ECHO}
	${ECHO} ====================================================
	${ECHO} COMMON_INFO_CREATE_MKT_SH
	${ECHO} ====================================================
	${ECHO}

	${TOP}/src/db/schema/trigo/create_ccd.sh --confirm_db=no --action=recreate >> $tmplog

else

	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

BASIC_SCRIPT_SRC_FILE_2=${CMP_DIR}/basic_company_setup_2.script
BASIC_SCRIPT_FILE_2=${CMP_DIR}/basic_company_setup.script_2.tmp

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


${ECHO} ====================================================
${ECHO} COMMON_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}

${CAT} ${BASIC_SCRIPT_SRC_FILE_2} \
    | ${SED} "s/%CMP_CODE%/$CMP_CODE/g" \
    | ${SED} "s/%CMP_NAME%/$CMP_NAME/g" \
    > ${BASIC_SCRIPT_FILE_2}

${ECHO}
${ECHO} ====================================================
${ECHO} COMMON_INFO_POPULATE_DATA_SH 
${ECHO} ====================================================
${ECHO}

cd $CMP_DIR

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

${ECHO}
${ECHO} ====================================================
${ECHO} COMMON_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} COMMON_INFO_LOAD_SCRIPT_SH 
${ECHO} ====================================================
${ECHO}
$JAVA_RT com.ibm.ccd.docstore.util.DocLoader "$CMP_CODE" Admin ${SCRIPTS_DIR}/common/common_scripts.xml Cp1252 >> $tmplog

$JAVA_RT com.ibm.ccd.docstore.util.DocLoader "$CMP_CODE" Admin ${SCRIPTS_DIR}/common/report_scripts.xml Cp1252 >> $tmplog

$JAVA_RT_NOOUT com.ibm.ccd.docstore.util.DocLoader "$CMP_CODE" Admin ${SCRIPTS_DIR}/common/ldap_scripts.xml Cp1252 >> $tmplog
${ECHO}
${ECHO} ====================================================
${ECHO} COMMON_INFO_BUILD_DISTRIBS_SH
${ECHO} ====================================================
${ECHO}
${JAVA_RT} com.ibm.ccd.common.interpreter.test.TestScript --companycode="$CMP_CODE" \
                                              --username="trigo_root" \
                                              --scriptpath="${BASIC_SCRIPT_FILE_2}" >> $tmplog

#FP7 Install DocStore maintenance PIM script       
#DocStore maintenance spec
${JAVA_RT} com.ibm.ccd.common.interpreter.test.TestScript --companycode="$CMP_CODE" --scriptpath="${SCRIPTS_DIR}/common/report/DocStoreMaintenanceSpec.script" >> $tmplog

#Data Maintenance Spec
${JAVA_RT} com.ibm.ccd.common.interpreter.test.TestScript --companycode="$CMP_CODE" --scriptpath="${SCRIPTS_DIR}/common/report/DataMaintenanceSpec.script" >> $tmplog

if [ "$CCD_DB" = "db2" ]; then
  ${JAVA_RT} com.ibm.ccd.docstore.util.DocLoader "$CMP_CODE" Admin $TOP/src/maintenance/docStore/xml/docStoreMaintenanceQueries_db2.xml Cp1252 >> $tmplog
else
  ${JAVA_RT} com.ibm.ccd.docstore.util.DocLoader "$CMP_CODE" Admin $TOP/src/maintenance/docStore/xml/docStoreMaintenanceQueries_oracle.xml Cp1252 >> $tmplog
fi
#DocStore maintenance lookup
${JAVA_RT} com.ibm.ccd.common.interpreter.test.TestScript --companycode="$CMP_CODE" --scriptpath="${SCRIPTS_DIR}/common/report/DocStoreMaintenanceLookup.script" >> $tmplog

#Data Maintenance Lookup
${JAVA_RT} com.ibm.ccd.common.interpreter.test.TestScript --companycode="$CMP_CODE" --scriptpath="${SCRIPTS_DIR}/common/report/DataMaintenanceLookup.script" >> $tmplog

#DocStore scripts
${JAVA_RT} com.ibm.ccd.docstore.util.DocLoader "$CMP_CODE" Admin $TOP/src/maintenance/docStore/xml/docStoreMaintenanceScripts.xml Cp1252 >> $tmplog

#DocStore Reports
${JAVA_RT} com.ibm.ccd.common.interpreter.test.TestScript --companycode="$CMP_CODE" --scriptpath="${SCRIPTS_DIR}/common/report/DocStoreMaintenanceCreate_Report.script" >> $tmplog

#Data Maintenance Code library
${JAVA_RT} com.ibm.ccd.docstore.util.DocLoader "$CMP_CODE" Admin $TOP/src/maintenance/profile_data/xml/dataMaintenanceCodeLibrary.xml Cp1252 >> $tmplog
#Profile data maintenance script
${JAVA_RT} com.ibm.ccd.docstore.util.DocLoader "$CMP_CODE" Admin $TOP/src/maintenance/profile_data/xml/profileDataMaintenanceScripts.xml Cp1252 >> $tmplog
#Profile data maintenance reports
${JAVA_RT} com.ibm.ccd.common.interpreter.test.TestScript --companycode="$CMP_CODE" --scriptpath="${SCRIPTS_DIR}/common/report/ProfileDataMaintenanceCreate_Report.script" >> $tmplog

${RM} ${DML_SQL_FILE}
${RM} ${BASIC_SCRIPT_FILE}
${RM} ${BASIC_SCRIPT_FILE_2}


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

fi
