#!/bin/bash

##################################################################
# Migrating the following entities from 2.5 to 3.1
# custom fields, content class, and definition.xml
##################################################################
## Back up 2.5 files ##
cp /opt/tandbergtv/cms/plugins/content/com.tandbergtv.metadata.ecmf.ui/resources/definition.xml $backupdir/.
cp /opt/tandbergtv/cms/plugins/content/com.tandbergtv.metadata.ecmf.ui/resources/ecmf.properties $backupdir/.
cp /opt/tandbergtv/cms/plugins/content/com.tandbergtv.metadata.ecmf.ui/resources/series_definition.xml $backupdir/.
cp /opt/tandbergtv/cms/plugins/content/com.tandbergtv.metadata.ecmf.ui/resources/series_ecmf.properties $backupdir/.

RESOURCE_DIR="/opt/tandbergtv/cms/plugins/content/com.tandbergtv.metadata.ecmf.ui/resources"

cd upgrade30/dist/customfields
ACTIVITY="upgrading custom fields"
logBeginMsg "begin $ACTIVITY"
DEF_FILE="$RESOURCE_DIR/definition.xml"
SERIES_FILE="$RESOURCE_DIR/series_definition.xml"
ECMF_FILE="$RESOURCE_DIR/ecmf.properties"
./scripts/cf_upgrade_config.sh "$DEF_FILE $SERIES_FILE $ECMF_FILE"
./scripts/custom_fields_upgrade.sh
logDoneMsg "$ACTIVITY"
cd ../../..

cd upgrade30/dist/customfields
ACTIVITY="upgrading Content Class Partner Override"
logBeginMsg "begin $ACTIVITY"
./scripts/content_class_partner_upgrade.sh
logDoneMsg "$ACTIVITY"
cd ../../..

cd upgrade25/dist/contentmgmt
ACTIVITY="upgrading Content Class Autofill Provider"
logBeginMsg "begin $ACTIVITY"
AUTOFILL_DIR=$upgradedir/autofill
## create temp autofill dir to store output of convert_autofill_prov.sh
mkdir -p $AUTOFILL_DIR
SPEC_FILE="definition.xml"
PROP_FILE="ecmf.properties"
./scripts/convert_autofill_prov.sh "$RESOURCE_DIR $AUTOFILL_DIR $SPEC_FILE $PROP_FILE"
cp $AUTOFILL_DIR/definition.xml $RESOURCE_DIR/
cp $AUTOFILL_DIR/ecmf.properties $RESOURCE_DIR/
logDoneMsg "$ACTIVITY"
cd ../../..


## Upgrade definition.xml ##
ACTIVITY="upgrading definition.xml, series_definition.xml, ecmf.properties and series_ecmf.properties ##"
if [ ! -f $upgradedir/definition.xml ]; then
	logBeginMsg "begin $ACTIVITY"
	cd upgrade30/dist/contentmgmt
    ./scripts/convert_spec.sh "$RESOURCE_DIR $upgradedir"
	cd ../../..
	logDoneMsg "$ACTIVITY"
else
    logMsg "definition.xml already upgraded, skipping ..."
fi

## Copy upgraded definition.xml
cp $upgradedir/definition.xml $RESOURCE_DIR/
cp $upgradedir/ecmf.properties $RESOURCE_DIR/
cp $upgradedir/series_definition.xml $RESOURCE_DIR/
cp $upgradedir/series_ecmf.properties $RESOURCE_DIR/

# Force change ownership of all to nobody
chown -R nobody:nobody /opt/tandbergtv/
chown -R nobody:nobody /usr/local/n2bb
