#!/bin/sh

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

NAME=chronos
DEFAULT_PATH=/etc/default/$NAME


. /etc/clearwater/config

[ -z $signaling_namespace ] || namespace_prefix="ip netns exec $signaling_namespace"


# Update default configuration with $namespace_prefix if needed

DEFAULT_TEMP=$(mktemp -t $NAME-default.XXXXXX) || exit 1

if [ -f $DEFAULT_PATH ]
then
  cat $DEFAULT_PATH | grep -v "# clearwater-infrastructure" > $DEFAULT_TEMP
else
  touch $DEFAULT_TEMP
  chmod +r $DEFAULT_TEMP
fi

echo "start_prefix=\"$namespace_prefix\" # clearwater-infrastructure" >> $DEFAULT_TEMP

# Compare files to see if action is needed. If the source file does
# not exist cmp will return non-zero and we will create it.
if ! cmp -s $DEFAULT_PATH $DEFAULT_TEMP
then
  # default configuration update needed, so copy the change and
  # restart the service
  cp $DEFAULT_TEMP $DEFAULT_PATH
  service $NAME stop
fi

rm $DEFAULT_TEMP

