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


#steps involved
# - load specs
# - load maps
# - load scripts
# - create workflows
# - load lookup tables [optional]
# - create selections

. ${TOP}/bin/get_params.sh

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

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


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


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

if [ "${_CCD_CODE}" = "" ]; then
    ${ECHO} COMMON_INFO_USAGE_SH: "$0 --code=<acmeCode> [--name=<acmeName> --confirm_db=no]"
    exit
fi

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

ACME_CODE=${_CCD_CODE}
ACME_NAME=${_CCD_NAME}
if [ -z "$ACME_NAME" ]; then
    export ACME_NAME=$ACME_CODE
fi

. $DB_DIR/cmp/create_cmp_localized.sh --code="${ACME_CODE}" --name="${ACME_NAME}" 

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

ACME_DIR=$DB_DIR/acme
ACME_TMP_DIR=$ACME_DIR/tmp

${RM} $ACME_TMP_DIR

#----------------------------------------
#
# specs
#
#----------------------------------------

${ECHO}
${ECHO} ====================================================
${ECHO} COMMON_INFO_CREATE_SPEC_SH
${ECHO} ====================================================
${ECHO}

${MKDIR} $ACME_TMP_DIR/specs
cd $DB_ROOT_DIR/specs/acme

for file in `ls *.xml`; do
    ${ECHO} "  $file" >> $tmplog
    cat $file \
        | ${SED} "s/%ACME_CODE%/$ACME_CODE/g" \
        | ${SED} "s/%ACME_NAME%/$ACME_NAME/g" \
        > $ACME_TMP_DIR/specs/$file
done

#----------------------------------------
#
# maps
#
#----------------------------------------

${ECHO}
${ECHO} ====================================================
${ECHO} COMMON_INFO_CREATE_MAP_SH
${ECHO} ====================================================
${ECHO}

${MKDIR} $ACME_TMP_DIR/maps
cd $DB_ROOT_DIR/maps/acme

for file in `ls *.xml`; do
    ${ECHO} "  $file" >> $tmplog
    cat $file \
        | ${SED} "s/%ACME_CODE%/$ACME_CODE/g" \
        | ${SED} "s/%ACME_NAME%/$ACME_NAME/g" \
        > $ACME_TMP_DIR/maps/$file
done

BASIC_SCRIPT_SRC_FILE=${ACME_DIR}/acme_setup.script
BASIC_SCRIPT_FILE=${ACME_DIR}/acme_setup.script.tmp

${CAT} ${BASIC_SCRIPT_SRC_FILE} \
    | ${SED} "s/%ACME_CODE%/$ACME_CODE/g" \
    | ${SED} "s/%ACME_NAME%/$ACME_NAME/g" \
    > ${BASIC_SCRIPT_FILE}

BASIC_SCRIPT_SRC_FILE_2=${ACME_DIR}/acme_setup_2.script
BASIC_SCRIPT_FILE_2=${ACME_DIR}/acme_setup_2.script.tmp

${CAT} ${BASIC_SCRIPT_SRC_FILE_2} \
    | ${SED} "s/%ACME_CODE%/$ACME_CODE/g" \
    | ${SED} "s/%ACME_NAME%/$ACME_NAME/g" \
    > ${BASIC_SCRIPT_FILE_2}

${ECHO}
${ECHO} ====================================================
${ECHO} COMMON_INFO_INSERT_SPEC_SH
${ECHO} ====================================================
${ECHO}
#
#this needs the `ls *.xml` because specs need to be inserted in order, parent before child
#
cd ${TOP}/src/db/schema/acme/tmp/specs
$JAVA_RT com.ibm.ccd.content.impexp.DatabaseUploader -action=i -debug=n `ls *.xml` >> $tmplog

${ECHO}
${ECHO} ====================================================
${ECHO} COMMON_INFO_LOAD_SPEC_SH
${ECHO} ====================================================
${ECHO}
${ECHO} "Do not worry about the message:" >> $tmplog
${ECHO} "  Cannot find the declaration of element 'trigoDummyElement'" >> $tmplog
${ECHO}

cd $DB_ROOT_DIR/specs/acme
$JAVA_RT com.ibm.ccd.content.impexp.SpecXSDBridge -company_code="${ACME_CODE}" \
                                                  -file_name="po.xsd" \
                                                  -spec_name="PO_XSD_PRIMARY_SPEC" \
                                                  -spec_type=PRIMARY_SPEC \
                                                  -primary_key_path="PO_XSD_PRIMARY_SPEC/PONumber" \
                                                  -max_ancestors=5 \
                                                  -top_level_name="purchaseOrder" >> $tmplog
                                                  
$JAVA_RT com.ibm.ccd.content.impexp.SpecXSDBridge -company_code="${ACME_CODE}" \
                                                  -file_name="po.xsd" \
                                                  -file_type=X \
                                                  -spec_name="PO_XSD_FILE_SPEC" \
                                                  -spec_type=FILE_SPEC \
                                                  -max_ancestors=5 \
                                                  -top_level_name="purchaseOrder" >> $tmplog

$JAVA_RT com.ibm.ccd.content.impexp.SpecXSDBridge -company_code="${ACME_CODE}" \
                                                  -file_name="po.xsd" \
                                                  -file_type=X \
                                                  -spec_name="purchaseOrder" \
                                                  -spec_type=FILE_SPEC \
                                                  -max_ancestors=5 \
                                                  -top_level_name="purchaseOrder" >> $tmplog

$JAVA_RT com.ibm.ccd.content.impexp.SpecXSDBridge -company_code="${ACME_CODE}" \
                                                  -file_name="po.xsd" \
                                                  -file_type=X \
                                                  -spec_name="PO_XSD_MKT_SPEC" \
                                                  -spec_type=MKT_SPEC \
                                                  -max_ancestors=5 \
                                                  -top_level_name="purchaseOrder" >> $tmplog

${ECHO}
${ECHO} ====================================================
${ECHO} COMMON_INFO_MAP_SPEC_SH
${ECHO} ====================================================
${ECHO}
#this needs the `ls *.xml` because specs need to be inserted in order, parent before child
#
cd ${TOP}/src/db/schema/acme/tmp/maps
$JAVA_RT com.ibm.ccd.content.impexp.DatabaseUploader -company_code=$ACME_CODE -action=i -debug=n `ls *.xml` >> $tmplog

${ECHO}
${ECHO} ====================================================
${ECHO} COMMON_INFO_LOAD_SCRIPT_SH
${ECHO} ====================================================
${ECHO}

$JAVA_RT com.ibm.ccd.docstore.util.DocLoader -companycode=$ACME_CODE -username=Admin -xmlfile=${SCRIPTS_DIR}/acme/acme_scripts.xml -encoding=Cp1252 >> $tmplog

#$JAVA_RT com.ibm.ccd.docstore.util.DocLoader -companycode=$ACME_CODE -username=jwilson -xmlfile=${DB_ROOT_DIR}/lkp/com/ibm/ccd/common/com/ibm/ccd/common_lkp.xml -encoding=Cp1252
#$JAVA_RT com.ibm.ccd.docstore.util.DocLoader -companycode=$ACME_CODE -username=jwilson -xmlfile=${DB_ROOT_DIR}/lkp/acme/acme_lkp.xml -encoding=Cp1252

${ECHO}
${ECHO} ====================================================
${ECHO} COMMON_INFO_POP_CONSOLE_SH
${ECHO} ====================================================
${ECHO}

${JAVA_RT} com.ibm.ccd.common.interpreter.test.TestScript --companycode="$ACME_CODE" \
                                                          --username="trigo_root" \
                                                          --scriptpath="${BASIC_SCRIPT_FILE}" >> $tmplog

${RM} ${BASIC_SCRIPT_FILE}

${JAVA_RT} com.ibm.ccd.common.interpreter.test.TestScript --companycode="$ACME_CODE" \
                                                          --username="trigo_root" \
                                                          --scriptpath="${BASIC_SCRIPT_FILE_2}" >> $tmplog

${RM} ${BASIC_SCRIPT_FILE_2}


${ECHO}
${ECHO} ====================================================
${ECHO} COMMON_INFO_LOAD_FILE_SH
${ECHO} ====================================================
${ECHO}

${RM} $TOP/public_html/suppliers/$ACME_CODE/aggregated_files
$JAVA_RT com.ibm.ccd.docstore.util.DocLoader -companycode=$ACME_CODE -username=Admin -xmlfile=${ACME_DIR}/acme_ctg_load.xml >> $tmplog

#The below call has been commented as part of CR:40116/40186 which led to creation of export with duplicate name in "acme" company.
#cd ${ACME_DIR}
#./acme_create_syns.sh ${ACME_CODE} >> $tmplog


# ${ECHO} ====================================================
# ${ECHO} loading widgets for ${ACME_CODE}
# ${ECHO} ====================================================
# ${TOP}/bin/loadwidgets.sh ${ACME_CODE}

#${TOP}/src/db/schema/util/create_default_org.sh ${ACME_CODE}



cd $ACME_DIR
./acme_create_workflows.sh ${ACME_CODE} >> $tmplog




if [ "${_CCD_LOAD_LKP}" = "yes" ]; then
  ${ECHO}
  ${ECHO} ====================================================
  ${ECHO} COMMON_INFO_LOAD_LKP_SH
  ${ECHO} ====================================================
  ${ECHO}

    cd $ACME_DIR
    ./loadlkps.sh $ACME_CODE Admin '/lkp/common' >> $tmplog
    ./loadlkps.sh $ACME_CODE Admin '/lkp/acme' >> $tmplog
fi

#${ECHO}
#${ECHO} ====================================================
#${ECHO} Inserting selections for testing security for $ACME_CODE
#${ECHO} ====================================================
#${ECHO}
#. ${TOP}/src/db/schema/regression/4.2/security/test_setup.sh

${RM} $OUTPUT_FILE

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

