#!/bin/bash

# Copyright (C) Metaswitch Networks 2015
# If license terms are provided to you in a COPYING file in the root directory
# of the source code repository by which you are accessing this code, then
# the license outlined in that COPYING file applies to your use.
# Otherwise no rights are granted except for those provided to you by
# Metaswitch Networks in a separate written agreement.

set -e

if [ ! -e /etc/clearwater/local_config ]
then
	echo "/etc/clearwater/local_config must exist before this script is run"
	exit 1
fi

if [ ! -e /etc/clearwater/shared_config ]
then
	echo "/etc/clearwater/shared_config must exist before this script is run"
	exit 1
fi

backup=/etc/clearwater/config.`date +%Y%m%d%H%M%S%N`.migration_backup
cp /etc/clearwater/config $backup
echo "Old config backed up to $backup"
cat <<EOF > /etc/clearwater/config
if [ -f /etc/clearwater/shared_config ]
then
  . /etc/clearwater/shared_config
fi

. /etc/clearwater/local_config

if [ -f /etc/clearwater/user_settings ]
then
  . /etc/clearwater/user_settings
fi

EOF
echo "Config migration complete. You should now restart clearwater-infrastructure and any clearwater services on this node."
