#!/bin/bash
# Licensed Materials - Property of IBM
# 5724-I68
#
# (C) Copyright IBM Corp. 2000, 2006
#
# US Government Users Restricted Rights - Use, duplication, or
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp.


# import relationship data for a company exported using exportCompany.sh

. ${TOP}/bin/get_params.sh

USAGE="usage: importRelationshipsAsCompany.sh --company_code=<code> --zip_path=<content zip file path> [--temp_folder=<temporary folder>]"

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

if [ -z ${_CCD_ZIP_PATH} ]; then
    echo Zip file for relationship attribute import is not specified
    echo ${USAGE}
    exit
fi

if [ -z ${_CCD_TEMP_FOLDER} ]; then
    _CCD_TEMP_FOLDER="temp"
fi    
    
_CCD_TEMP_FOLDER="${_CCD_TEMP_FOLDER}/imprel53"

IMPORT_MEM_FLAGS="-Xmx1024m -Xms1024m"

JIMPORT="${JAVA_RT} ${IMPORT_MEM_FLAGS}"
${JIMPORT} com.ibm.ccd.content.impexp.ImportRelationshipsFor53 --action=unzip --company_code="${_CCD_COMPANY_CODE}" \
                                                    --zipfilepath="${_CCD_ZIP_PATH}" --tempfolder="${_CCD_TEMP_FOLDER}"

TMP_FILE="tmpContent.csv"
RELN_CTRL_FILE="relationship_containers.xml"
currentdir=`pwd`

cd ${_CCD_TEMP_FOLDER}

workingdir='pwd'

if [ $workingdir = $currentdir ]; then
    echo Not able to create temp folder so exiting.
    exit
fi

PARENTDIR=""

ARCHIVEFOLDER_FILE_LIST=`grep -l "<File>archives/" ImportEnvControl.xml`
for ia in ${ARCHIVEFOLDER_FILE_LIST}
do
PARENTDIR="archives/"
done


RELN_FILE_LIST=`grep -l RELATIONSHIP_INFO ${PARENTDIR}*_CONTENT/*_CONTENT_*_DATA.csv`
RELN_CSV_XML="CSVFileList.xml"
CSVFILEPATHS=""
for i in ${RELN_FILE_LIST}
do
grep RELATIONSHIP_INFO ${i} >> $TMP_FILE 
mv $TMP_FILE ${i}
CSVFILEPATHS="${CSVFILEPATHS}/${i}|"

done
echo "${CSVFILEPATHS}"

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

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

date

beforeRun "load relationship data"
echo ${currentdir}

${JIMPORT} com.ibm.ccd.content.impexp.ImportRelationshipsFor53 --company_code="${_CCD_COMPANY_CODE}" \
                                                    --control_file="${PARENTDIR}HIERARCHY_CONTENT/HIERARCHY_CONTENT_DATA.xml" --csvFilePaths="${CSVFILEPATHS}"

${JIMPORT} com.ibm.ccd.content.impexp.ImportRelationshipsFor53 --company_code="${_CCD_COMPANY_CODE}" \
                                                    --control_file="${PARENTDIR}CATALOG_CONTENT/CATALOG_CONTENT_DATA.xml" --csvFilePaths="${CSVFILEPATHS}"
afterRun

if [ $workingdir != $currentdir ]; then
rm -rf *
cd ${currentdir}
fi
date