#!/bin/bash

# @file homer
#
# Copyright (C) Metaswitch Networks 2016
# 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 default values which will be overwritten if they exist in /etc/clearwater/config
cassandra_hostname="localhost" 
. /etc/clearwater/config

# Only modify local_settings if the home_domain, sprout_hostname and local_ip
# are available (they might not be if shared_config hasn't been propagated
# round the deployment yet). Homer will cyclically restart until this has
# been set up correctly
if [ -n "$home_domain" ] && [ -n "$sprout_hostname" ] && [ -n "$local_ip" ]
then
  function escape { echo $1 | sed -e 's/\//\\\//g' ; }
  sed -e 's/^LOCAL_IP = .*$/LOCAL_IP = "'$(escape $local_ip)'"/g
          s/^SIP_DIGEST_REALM = .*$/SIP_DIGEST_REALM = "'$(escape $home_domain)'"/g
          s/^SPROUT_HOSTNAME = .*$/SPROUT_HOSTNAME = "'$(escape $sprout_hostname)'"/g
          s/^CASS_HOST = .*$/CASS_HOST = "'$(escape $cassandra_hostname)'"/g' < /usr/share/clearwater/homer/templates/local_settings.py >/tmp/local_settings.py.$$
  mv /tmp/local_settings.py.$$ /usr/share/clearwater/homer/local_settings.py
fi
