#!/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: exportCompany.sh --company_code=<code> --output_dir=<directory path>

. ${TOP}/bin/get_params.sh

function usage ()
{
    echo "Usage: exportCompany.sh --company_code=<code> --output_dir=<directory path> [--export_content=yes|no]"
}

if [ -z ${_CCD_COMPANY_CODE} ]; then
    echo Company code not specified
    usage
    exit
fi

if [ -z ${_CCD_OUTPUT_DIR} ]; then
    echo Output directory not specified
    usage
    exit
fi

date

EXPORT_MEM_FLAGS="-Xmx1024m -Xms1024m"

JEXPORT="${JAVA_RT} ${EXPORT_MEM_FLAGS}"

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

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

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

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

# SCRIPT_DIRECTORIES="
# /params
# /scripts
# /staging_area
# /users
# /workflow/gui
# "

SCRIPT_DIRECTORIES="/"

[ -d ${_CCD_OUTPUT_DIR} ] || mkdir ${_CCD_OUTPUT_DIR}

beforeRun "Export Docstore Contents"
echo "" > ${SCRIPTS_FILE_LIST}
for SCRIPT_DIRECTORY in ${SCRIPT_DIRECTORIES}
do
  XML_FILE=`echo ${SCRIPT_DIRECTORY} | tr / _ | tr '[:lower:]' '[:upper:]'`.xml
  touch ${_CCD_OUTPUT_DIR}/${XML_FILE}
  $JEXPORT com.ibm.ccd.docstore.util.DocDownloader --companycode="${_CCD_COMPANY_CODE}" \
                                             --subtree="${SCRIPT_DIRECTORY}" \
                                             --xmlfile="${_CCD_OUTPUT_DIR}/${XML_FILE}" \
                                             --rootlocalpath="${SCRIPTS_LOCAL_PATH}"
  echo exported ${SCRIPT_DIRECTORY} to ${XML_FILE}
  echo ${XML_FILE} >> ${SCRIPTS_FILE_LIST}
done
afterRun

beforeRun "Exporting Security"
$JEXPORT com.ibm.ccd.common.security.ExportSecurity --company_code="${_CCD_COMPANY_CODE}" \
                                        --output_file_path="${SECURITY_OUTPUT_FILE}" \
                                        --export_users_script \
                                        --export_acg_script \
                                        --export_roles_script
afterRun

beforeRun "Exporting Locales"
$JEXPORT com.ibm.ccd.common.security.ExportLocales  --company_code="${_CCD_COMPANY_CODE}" \
                                        --output_file_path="${LOCALES_OUTPUT_FILE}"
afterRun

beforeRun "Exporting Attribute Collections"
$JEXPORT com.ibm.ccd.content.impexp.ExportAttributeCollections --company_code="${_CCD_COMPANY_CODE}" \
                                                         --output_file_path="${ATTRIBUTE_COLLECTIONS_OUTPUT_FILE}"
afterRun

beforeRun "Exporting Catalogs and Lookup Tables"
$JEXPORT com.ibm.ccd.content.impexp.ExportCatalogs --company_code="${_CCD_COMPANY_CODE}" \
                                             --output_file_path="${CATALOGS_OUTPUT_FILE}" \
                                             --lkp_output_file_path="${LOOKUP_TABLE_OUTPUT_FILE}" \
                                             --links_output_file_path="${CATALOG_LINKS_OUTPUT_FILE}"
afterRun

beforeRun "Export Collaboration Areas"
$JEXPORT com.ibm.ccd.content.impexp.ExportColAreas --company_code="${_CCD_COMPANY_CODE}" \
                                             --output_file_path="${COLLABORATION_AREAS_OUTPUT_FILE}"
afterRun

beforeRun "Exporting Container Access Privileges"
$JEXPORT com.ibm.ccd.content.impexp.ExportContainerAccessPrivileges --company_code="${_CCD_COMPANY_CODE}" \
                                                              --output_file_path="${ACCESS_PRIVILEGES_OUTPUT_FILE}"
afterRun

beforeRun "Exporting Views"
$JEXPORT com.ibm.ccd.content.impexp.ExportViews --company_code="${_CCD_COMPANY_CODE}" \
                                          --output_file_path="${VIEWS_OUTPUT_FILE}"
afterRun

beforeRun "Exporting Exports"
$JEXPORT com.ibm.ccd.content.impexp.ExportExports --company_code="${_CCD_COMPANY_CODE}" \
                                            --output_file_path="${EXPORTS_OUTPUT_FILE}"
afterRun

beforeRun "Exporting Hierarchies"
$JEXPORT com.ibm.ccd.content.impexp.ExportHierarchies --company_code="${_CCD_COMPANY_CODE}" \
                                                --output_file_path="${HIERARCHIES_OUTPUT_FILE}"
afterRun

beforeRun "Exporting Imports"
$JEXPORT com.ibm.ccd.content.impexp.ExportImports --company_code="${_CCD_COMPANY_CODE}" \
                                            --output_file_path="${IMPORTS_OUTPUT_FILE}"
afterRun

beforeRun "Exporting Specs"
$JEXPORT -Xmx1024m -Xms1024m com.ibm.ccd.content.impexp.ExportSpecs --company_code="${_CCD_COMPANY_CODE}" \
                                                              --output_directory="${_CCD_OUTPUT_DIR}"
afterRun

beforeRun "Exporting Spec Maps"
$JEXPORT com.ibm.ccd.content.impexp.ExportSpecMaps --company_code="${_CCD_COMPANY_CODE}" \
                                             --output_directory="${_CCD_OUTPUT_DIR}"
afterRun

beforeRun "Exporting Workflows"
$JEXPORT com.ibm.ccd.content.impexp.ExportWorkflows --company_code="${_CCD_COMPANY_CODE}" \
                                              --output_file_path="${WORKFLOWS_OUTPUT_FILE}"
afterRun

beforeRun "Exporting Distributions"
$JEXPORT com.ibm.ccd.content.impexp.ExportDistributions --company_code="${_CCD_COMPANY_CODE}" \
                                                  --output_file_path="${DISTRIBUTIONS_OUTPUT_FILE}"
afterRun

beforeRun "Exporting Reports"
$JEXPORT com.ibm.ccd.content.impexp.ExportReports --company_code="${_CCD_COMPANY_CODE}" \
                                            --output_file_path="${REPORTS_OUTPUT_FILE}"
afterRun

beforeRun "Exporting Selections"
$JEXPORT com.ibm.ccd.content.impexp.ExportSelections --company_code="${_CCD_COMPANY_CODE}" \
                                               --output_file_path="${SELECTIONS_OUTPUT_FILE}" \
                                               --content_output_file_path="${SELECTIONS_CONTENT_OUTPUT_FILE}"
afterRun

beforeRun "Exporting Sequences"
$JEXPORT com.ibm.ccd.content.impexp.ExportSequences --company_code="${_CCD_COMPANY_CODE}" \
                                              --output_file_path="${SEQUENCES_OUTPUT_FILE}"
afterRun

if [ "${_CCD_EXPORT_CONTENT}" = "yes" ]; then
    beforeRun "Exporting Container Entries"
    $JEXPORT com.ibm.ccd.content.impexp.ExportEntries --company_code="${_CCD_COMPANY_CODE}" \
                                                --output_directory="${_CCD_OUTPUT_DIR}" \
                                                --xml_file_path="${ENTRIES_OUTPUT_FILE}"
    afterRun
fi

date

