#!/bin/bash
# postinst script for jitsi-videobridge

set -e

CONFIG="/etc/jitsi/videobridge/config"

JVB_HOSTNAME="$JVB_HOSTNAME_IN"

# 8-chars random secret, alternative to pwgen 8
JVB_SECRET=`head -c 8 /dev/urandom | tr '\0-\377' 'a-zA-Z0-9a-zA-Z0-9a-zA-Z0-9a-zA-Z0-9@@@@####'`
JVB_PORT=5347

            # storing default for later use by Jitsi Meet and other packages
            echo '# Jitsi Videobridge settings' > $CONFIG
            echo >> $CONFIG
            echo '# sets the XMPP domain (default: none)' >> $CONFIG
            echo "JVB_HOSTNAME=$JVB_HOSTNAME" >> $CONFIG
            echo >> $CONFIG
            echo '# sets the hostname of the XMPP server (default: domain if set, localhost otherwise)' >> $CONFIG
            echo "JVB_HOST=" >> $CONFIG
            echo >> $CONFIG
            echo '# sets the port of the XMPP server (default: 5275)' >> $CONFIG
            echo "JVB_PORT=$JVB_PORT" >> $CONFIG
            echo >> $CONFIG
            echo '# sets the shared secret used to authenticate to the XMPP server' >> $CONFIG
            echo "JVB_SECRET=$JVB_SECRET" >> $CONFIG
            echo >> $CONFIG
            echo '# extra options to pass to the JVB daemon' >> $CONFIG
            echo "JVB_OPTS=\"--apis=,\"" >> $CONFIG
            echo >> $CONFIG

NEW_JITSI_CONFIG="/etc/jitsi/videobridge/sip-communicator.properties"
        if [ ! -f $NEW_JITSI_CONFIG ]; then
            # if sip-communicator.properties file is missing create it
            # as jvb will search for it and if cannot create it will fail starting
            touch $NEW_JITSI_CONFIG
        fi

        # let's check whether there is a setting in the $CONFIG
        # for home folder and logging props file, if missing add it
        if ! grep -q "JAVA_SYS_PROPS" "$CONFIG"; then
            echo >> $CONFIG
            echo '# adds java system props that are passed to jvb (default are for home and logging config file)' >> $CONFIG
            echo "JAVA_SYS_PROPS=\"-Dnet.java.sip.communicator.SC_HOME_DIR_LOCATION=/etc/jitsi\
 -Dnet.java.sip.communicator.SC_HOME_DIR_NAME=videobridge\
 -Dnet.java.sip.communicator.SC_LOG_DIR_LOCATION=/var/log/jitsi\
 -Djava.util.logging.config.file=/etc/jitsi/videobridge/logging.properties\"" >> $CONFIG
        fi

        if ! grep -q "org.ice4j.ice.harvest.STUN_MAPPING_HARVESTER_ADDRESSES" "$NEW_JITSI_CONFIG" \
          && ! grep -q "org.ice4j.ice.harvest.NAT_HARVESTER_PUBLIC_ADDRESS" "$NEW_JITSI_CONFIG" ;then
            echo "org.ice4j.ice.harvest.DISABLE_AWS_HARVESTER=true" >> $NEW_JITSI_CONFIG
            echo "org.ice4j.ice.harvest.STUN_MAPPING_HARVESTER_ADDRESSES=meet-jit-si-turnrelay.jitsi.net:443" >> $NEW_JITSI_CONFIG
        fi

        if ! grep -q "#org.jitsi.videobridge.AUTHORIZED_SOURCE_REGEXP" "$NEW_JITSI_CONFIG" ;then
            sed -i 's/org.jitsi.videobridge.AUTHORIZED_SOURCE_REGEXP/#org.jitsi.videobridge.AUTHORIZED_SOURCE_REGEXP/g' $NEW_JITSI_CONFIG
        fi

        if ! grep -q "org.jitsi.videobridge.xmpp.user.shard.MUC_JIDS" "$NEW_JITSI_CONFIG" ;then
            echo "org.jitsi.videobridge.ENABLE_STATISTICS=true" >> $NEW_JITSI_CONFIG
            echo "org.jitsi.videobridge.STATISTICS_TRANSPORT=muc" >> $NEW_JITSI_CONFIG
            echo "org.jitsi.videobridge.xmpp.user.shard.HOSTNAME=localhost" >> $NEW_JITSI_CONFIG
            echo "org.jitsi.videobridge.xmpp.user.shard.DOMAIN=auth.$JVB_HOSTNAME" >> $NEW_JITSI_CONFIG
            echo "org.jitsi.videobridge.xmpp.user.shard.USERNAME=jvb" >> $NEW_JITSI_CONFIG
            echo "org.jitsi.videobridge.xmpp.user.shard.PASSWORD=$JVB_SECRET" >> $NEW_JITSI_CONFIG
            echo "org.jitsi.videobridge.xmpp.user.shard.MUC_JIDS=JvbBrewery@internal.auth.$JVB_HOSTNAME" >> $NEW_JITSI_CONFIG
            echo "org.jitsi.videobridge.xmpp.user.shard.MUC_NICKNAME=$(uuidgen)" >> $NEW_JITSI_CONFIG
        fi

        APIS_VALUE=$(grep -oPe '--apis=\K(\w*,*\w*)' $CONFIG || true)
        if [[ $APIS_VALUE == *"xmpp"* ]]; then
            APIS_NEW_VALUE=${APIS_VALUE/xmpp/}
            if [ -z "$APIS_NEW_VALUE" ]; then
                APIS_NEW_VALUE=","
            fi
            sed -i "s/--apis=$APIS_VALUE/--apis=$APIS_NEW_VALUE/g" $CONFIG
        fi
        if [ -z "$APIS_VALUE" ]; then
            # no apis setting, component is turned on by default, so let's disable it
            sed -i "s/JVB_OPTS=\"/JVB_OPTS=\"--apis=, /g" $CONFIG
        fi
