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

# export scripts to recreate company
# usage: inputCompany.sh --company_code=<code> --input_dir=<directory path>

. ${TOP}/bin/get_params.sh

if [ -z ${_CCD_COMPANY_CODE} ]; then
    echo Company code not specified
    echo "usage: inputCompany.sh --company_code=<code> --input_dir=<directory path>"
    exit
fi

if [ -z ${_CCD_INPUT_DIR} ]; then
    echo Input directory not specified
    echo "usage: inputCompany.sh --company_code=<code> --input_dir=<directory path>"
    exit
fi

IMPORT_MEM_FLAGS="-Xmx1024m -Xms1024m"

JIMPORT="${JAVA_RT} ${IMPORT_MEM_FLAGS}"

#
# Hack to get import of specs to works!
#
echo -e '#!/bin/bash' \\n '${JAVA_RT} com.ibm.ccd.content.impexp.DatabaseUploader -action=i -debug=n $@' >  ${_CCD_INPUT_DIR}/specimport.sh
chmod a+rx ${_CCD_INPUT_DIR}/specimport.sh

function beforeRun ()
{
    echo "***** $1 *****"
    echo "============================================================"
}

function afterRun ()
{
    echo "============================================================"
}

function loadSpecsOfType ()
{
    beforeRun "$1"
    local SPEC_FILES=${_CCD_INPUT_DIR}
    ls -1 ${SPEC_FILES} | grep "^${3}_.*_${4}$" >/dev/null 2>&1
    if [ 0 -eq $? ]; then
        cd ${_CCD_INPUT_DIR}
        ls -1 ${SPEC_FILES} | grep "^${3}_.*_${4}$" | xargs ${_CCD_INPUT_DIR}/specimport.sh
    else
        echo "$2"
    fi
    afterRun
}

ACCESS_PRIVILEGES_OUTPUT_FILE=${_CCD_INPUT_DIR}/accessPrivileges.script
VIEWS_OUTPUT_FILE=${_CCD_INPUT_DIR}/views.script
ATTRIBUTE_COLLECTIONS_OUTPUT_FILE=${_CCD_INPUT_DIR}/attributeCollections.script
CATALOGS_OUTPUT_FILE=${_CCD_INPUT_DIR}/catalogs.script
COLLABORATION_AREAS_OUTPUT_FILE=${_CCD_INPUT_DIR}/CollaborationAreas.script
EXPORTS_OUTPUT_FILE=${_CCD_INPUT_DIR}/exports.script
HIERARCHIES_OUTPUT_FILE=${_CCD_INPUT_DIR}/hierarchies.script
IMPORTS_OUTPUT_FILE=${_CCD_INPUT_DIR}/imports.script
LOOKUP_TABLE_OUTPUT_FILE=${_CCD_INPUT_DIR}/lookupTables.script
UDL_OUTPUT_FILE=${_CCD_INPUT_DIR}/udls.script
LAST_SCRIPT_OUTPUT_FILE=${_CCD_INPUT_DIR}/last_script.script
CATALOG_LINKS_OUTPUT_FILE=${_CCD_INPUT_DIR}/linkCatalogs.script
SECURITY_OUTPUT_FILE=${_CCD_INPUT_DIR}/security.script
LOCALES_OUTPUT_FILE=${_CCD_INPUT_DIR}/locales.script
WORKFLOWS_OUTPUT_FILE=${_CCD_INPUT_DIR}/workflows.script
SELECTIONS_OUTPUT_FILE=${_CCD_INPUT_DIR}/selections.script
SELECTIONS_CONTENT_OUTPUT_FILE=${_CCD_INPUT_DIR}/selections_content.script
DISTRIBUTIONS_OUTPUT_FILE=${_CCD_INPUT_DIR}/distributions.script
REPORTS_OUTPUT_FILE=${_CCD_INPUT_DIR}/reports.script
SEQUENCES_OUTPUT_FILE=${_CCD_INPUT_DIR}/sequences.script

SCRIPTS_LOCAL_PATH=${_CCD_INPUT_DIR}/docstore
SCRIPTS_FILE_LIST=${_CCD_INPUT_DIR}/script_file_list.txt


beforeRun "load locales"
${JIMPORT} com.ibm.ccd.common.interpreter.test.TestScript -companycode="${_CCD_COMPANY_CODE}" \
                                              --username="trigo_root" \
                                              --scriptpath="${LOCALES_OUTPUT_FILE}"
afterRun

loadSpecsOfType "load all lookup table specs" "No lookup table specs found" 'LKP_SPEC' 'SPEC.xml'

beforeRun "create lookup tables"
${JIMPORT} com.ibm.ccd.common.interpreter.test.TestScript --companycode="${_CCD_COMPANY_CODE}" \
                                              --username="trigo_root" \
                                              --scriptpath="${LOOKUP_TABLE_OUTPUT_FILE}"
afterRun

for file in `ls ${_CCD_INPUT_DIR}/LKP_SPEC_*_SPEC.xml`; do
    ${ECHO} "  $file"
    cat $file \
        | ${SED} "s/<Action>Create<\/Action>/<Action>Update<\/Action>/g" \
        > ${file}.update
done

loadSpecsOfType "reload all lookup table specs" "No lookup table specs found" 'LKP_SPEC' 'SPEC.xml.update'

loadSpecsOfType "load all sub specs" "No sub specs found" 'SUB_SPEC' 'SPEC.xml'

loadSpecsOfType "load primary specs" "No primary specs found" 'PRIMARY_SPEC' 'SPEC.xml'

beforeRun "load spec types that don't need special handling"

${JIMPORT} com.ibm.ccd.content.impexp.DatabaseUploader -action=i -debug=n `ls ${_CCD_INPUT_DIR}/FILE_SPEC_*_SPEC.xml` \
                                                                    `ls ${_CCD_INPUT_DIR}/MKT_SPEC_*_SPEC.xml` \
                                                                    `ls ${_CCD_INPUT_DIR}/ORG_SPEC_*_SPEC.xml` \
                                                                    `ls ${_CCD_INPUT_DIR}/SCRIPT_INPUT_SPEC_*_SPEC.xml`
afterRun

loadSpecsOfType "load secondary specs" "load secondary specs" 'SECONDARY_SPEC' 'SPEC.xml' 

loadSpecsOfType "load old catalog specs" "No old catalog specs" 'CAT_SPEC' 'SPEC.xml' 

loadSpecsOfType "load old category tree specs" "No old category tree specs" 'CAT_TREE_SPEC' 'SPEC.xml'

loadSpecsOfType "load old category specs" "No old category specs" 'CTG_SPEC' 'SPEC.xml'

beforeRun "load scripts"
for XML_FILE in `cat ${SCRIPTS_FILE_LIST}`
do
  ${JIMPORT} com.ibm.ccd.docstore.util.DocLoader --companycode="${_CCD_COMPANY_CODE}" \
                                           --xmlfile="${_CCD_INPUT_DIR}/${XML_FILE}" \
                                           --rootlocalpath="${SCRIPTS_LOCAL_PATH}"
done
afterRun

beforeRun "load security setup"
${JIMPORT} com.ibm.ccd.common.interpreter.test.TestScript --companycode="${_CCD_COMPANY_CODE}" \
                                              --username="trigo_root" \
                                              --scriptpath="${SECURITY_OUTPUT_FILE}"
afterRun

beforeRun "load attribute collections"
${JIMPORT} com.ibm.ccd.common.interpreter.test.TestScript --companycode="${_CCD_COMPANY_CODE}" \
                                              --username="trigo_root" \
                                              --scriptpath="${ATTRIBUTE_COLLECTIONS_OUTPUT_FILE}"
afterRun

beforeRun "load hierarchies"
${JIMPORT} com.ibm.ccd.common.interpreter.test.TestScript --companycode="${_CCD_COMPANY_CODE}" \
                                              --username="trigo_root" \
                                              --scriptpath="${HIERARCHIES_OUTPUT_FILE}"
afterRun

beforeRun "load catalogs"
${JIMPORT} com.ibm.ccd.common.interpreter.test.TestScript --companycode="${_CCD_COMPANY_CODE}" \
                                              --username="trigo_root" \
                                              --scriptpath="${CATALOGS_OUTPUT_FILE}"
afterRun

beforeRun "load container access privileges"
${JIMPORT} com.ibm.ccd.common.interpreter.test.TestScript --companycode="${_CCD_COMPANY_CODE}" \
                                              --username="trigo_root" \
                                              --scriptpath="${ACCESS_PRIVILEGES_OUTPUT_FILE}"
afterRun

beforeRun "load workflows"
${JIMPORT} com.ibm.ccd.common.interpreter.test.TestScript --companycode="${_CCD_COMPANY_CODE}" \
                                              --username="trigo_root" \
                                              --scriptpath="${WORKFLOWS_OUTPUT_FILE}"
afterRun

beforeRun "load collaboration areas"
${JIMPORT} com.ibm.ccd.common.interpreter.test.TestScript --companycode="${_CCD_COMPANY_CODE}" \
                                              --username="trigo_root" \
                                              --scriptpath="${COLLABORATION_AREAS_OUTPUT_FILE}"
afterRun

beforeRun "load spec maps"
${JIMPORT} com.ibm.ccd.content.impexp.DatabaseUploader -action=i -debug=n `ls ${_CCD_INPUT_DIR}/*_SPEC_MAP.xml`
afterRun

beforeRun "load imports"
${JIMPORT} com.ibm.ccd.common.interpreter.test.TestScript --companycode="${_CCD_COMPANY_CODE}" \
                                              --username="trigo_root" \
                                              --scriptpath="${IMPORTS_OUTPUT_FILE}"
afterRun

beforeRun "load links between catalogs and collaboration areas"
${JIMPORT} com.ibm.ccd.common.interpreter.test.TestScript --companycode="${_CCD_COMPANY_CODE}" \
                                              --username="trigo_root" \
                                              --scriptpath="${CATALOG_LINKS_OUTPUT_FILE}"
afterRun

beforeRun "load views"
${JIMPORT} com.ibm.ccd.common.interpreter.test.TestScript --companycode="${_CCD_COMPANY_CODE}" \
                                              --username="trigo_root" \
                                              --scriptpath="${VIEWS_OUTPUT_FILE}"
afterRun

beforeRun "load distributions"
${JIMPORT} com.ibm.ccd.common.interpreter.test.TestScript --companycode="${_CCD_COMPANY_CODE}" \
                                              --username="trigo_root" \
                                              --scriptpath="${DISTRIBUTIONS_OUTPUT_FILE}"
afterRun

beforeRun "load exports"
${JIMPORT} com.ibm.ccd.common.interpreter.test.TestScript --companycode="${_CCD_COMPANY_CODE}" \
                                              --username="trigo_root" \
                                              --scriptpath="${EXPORTS_OUTPUT_FILE}"
afterRun

beforeRun "load reports"
${JIMPORT} com.ibm.ccd.common.interpreter.test.TestScript --companycode="${_CCD_COMPANY_CODE}" \
                                              --username="trigo_root" \
                                              --scriptpath="${REPORTS_OUTPUT_FILE}"
afterRun

beforeRun "load selections"
${JIMPORT} com.ibm.ccd.common.interpreter.test.TestScript --companycode="${_CCD_COMPANY_CODE}" \
                                              --username="trigo_root" \
                                              --scriptpath="${SELECTIONS_OUTPUT_FILE}"
afterRun

beforeRun "load sequences"
${JIMPORT} com.ibm.ccd.common.interpreter.test.TestScript --companycode="${_CCD_COMPANY_CODE}" \
                                              --username="trigo_root" \
                                              --scriptpath="${SEQUENCES_OUTPUT_FILE}"
afterRun
beforeRun "create udls"
${JIMPORT} com.ibm.ccd.common.interpreter.test.TestScript --companycode="${_CCD_COMPANY_CODE}" \
                                              --username="trigo_root" \
                                              --scriptpath="${UDL_OUTPUT_FILE}"
afterRun

beforeRun "last script"
${JIMPORT} com.ibm.ccd.common.interpreter.test.TestScript --companycode="${_CCD_COMPANY_CODE}" \
                                              --username="trigo_root" \
                                              --scriptpath="${LAST_SCRIPT_OUTPUT_FILE}"
afterRun

beforeRun "Validating scripts"
${JIMPORT} com.ibm.ccd.common.interpreter.util.ValidateScripts --company_code="${_CCD_COMPANY_CODE}" 2>/dev/null
afterRun

