#!/bin/bash

# @file ellis
#
# 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.

. /etc/clearwater/config

if [ -n "$hs_provisioning_hostname" ] && [ -n "$home_domain" ] && [ -n "$xdms_hostname" ] && [ -n "$local_ip" ]
then
  function escape { echo $1 | sed -e 's/\//\\\//g' ; }
  sed -e 's/^LOCAL_IP = .*$/LOCAL_IP = "'$(escape $local_ip)'"/g' \
      -e 's/^\(SIP_DIGEST_REALM\) = .*$/\1 = "'$(escape $home_domain)'"/g' \
      -e 's/^\(HOMESTEAD_URL\) = .*$/\1 = "'$(escape $hs_provisioning_hostname)'"/g' \
      -e 's/^\(XDM_URL\) = .*$/\1 = "'$(escape $xdms_hostname)'"/g' \
      -e 's/^\(SMTP_SMARTHOST\) = .*$/\1 = "'$(escape $smtp_smarthost)'"/g' \
      -e 's/^\(SMTP_USERNAME\) = .*$/\1 = "'$(escape $smtp_username)'"/g' \
      -e 's/^\(SMTP_PASSWORD\) = .*$/\1 = "'$(escape $smtp_password)'"/g' \
      -e 's/^\(EMAIL_RECOVERY_SENDER\) = .*$/\1 = "'$(escape $email_recovery_sender)'"/g' \
      -e 's/^\(SIGNUP_CODE\) = .*$/\1 = "'$(escape $signup_key)'"/g' \
      -e 's/^\(COOKIE_SECRET\) = .*$/\1 = "'$(escape $ellis_cookie_key)'"/g' \
      -e 's/^\(API_KEY\) = .*$/\1 = "'$(escape $ellis_api_key)'"/g' \
      </usr/share/clearwater/ellis/local_settings.py >/tmp/local_settings.py.$$
  mv /tmp/local_settings.py.$$ /usr/share/clearwater/ellis/env/lib/python2.7/site-packages/metaswitch/ellis/local_settings.py
fi
