#!/usr/bin/with-contenv bash

if [[ ! -f /config/sip-communicator.properties ]]; then
    cp /defaults/sip-communicator.properties /config
    sed -i \
        -e "s,\${XMPP_DOMAIN},$XMPP_DOMAIN,g" \
        -e "s,\${XMPP_AUTH_DOMAIN},$XMPP_AUTH_DOMAIN,g" \
        -e "s,\${JICOFO_AUTH_USER},$JICOFO_AUTH_USER,g" \
        -e "s#\${JVB_STUN_SERVERS}#$JVB_STUN_SERVERS#g" \
        /config/sip-communicator.properties
fi

if [[ ! -f /config/logging.properties ]]; then
    cp /defaults/logging.properties /config
fi

# Do this for every run, since the local IP may change!
if [[ ! -z "$DOCKER_HOST_ADDRESS" ]]; then
    LOCAL_ADDRESS=$(hostname -I | cut -d " " -f1)
    sed -i \
        -e "s,^org.ice4j.ice.harvest.NAT_HARVESTER_LOCAL_ADDRESS=.*,org.ice4j.ice.harvest.NAT_HARVESTER_LOCAL_ADDRESS=$LOCAL_ADDRESS," \
        -e "s,^org.ice4j.ice.harvest.NAT_HARVESTER_PUBLIC_ADDRESS=.*,org.ice4j.ice.harvest.NAT_HARVESTER_PUBLIC_ADDRESS=$DOCKER_HOST_ADDRESS," \
        /config/sip-communicator.properties
fi

