#!/bin/sh

. /usr/share/libubox/jshn.sh

normalize_bool()
{
case $1 in
    true|yes|1)
        echo 1
        ;;
    false|no|0)
        echo 0
        ;;
    *)
        echo 0
        ;;
esac
}

json_load "$(cat ${1:-../cfg/rsmb.json})"

json_get_var IOT_MQTT_ENABLE iot_mqtt_enable
json_get_var IOT_MQTT_PORT iot_mqtt_port
json_get_var IOT_MQTT_BRG_ENABLE iot_mqtt_brg_enable
json_get_var IOT_MQTT_BRG_PORT iot_mqtt_brg_port
json_get_var IOT_MQTT_BRG_ADDR iot_mqtt_brg_addr
json_get_var IOT_MQTT_BRG_USR iot_mqtt_brg_usr
json_get_var IOT_MQTT_BRG_PW iot_mqtt_brg_pw
json_get_var IOT_MQTT_BRG_ID iot_mqtt_brg_id

IOT_MQTT_ENABLE=$(normalize_bool $IOT_MQTT_ENABLE)
IOT_MQTT_BRG_ENABLE=$(normalize_bool $IOT_MQTT_BRG_ENABLE)

echo '# ---- START OF FILE ----'
echo '# Do not modify this file. It is generated by the startup script for IoT Gateway'
echo
echo '# ---- BROKER PROPERTIES ----'
echo 'log_level info'
echo 'max_log_entries 100'
echo 'trace_level maximum'
echo 'trace_output protocol'
echo 'max_trace_entries 400'
echo 'connection_messages true'
echo 'max_inflight_messages 1'
echo 'max_queued_messages 10'
echo 'retry_interval 20'
echo 'ffdc_output off'
echo 'persistence_location /tmp/rsmb/'
echo 'retained_persistence false'
echo 'autosave_on_changes true'
echo 'autosave_interval 48'
echo

[ "$IOT_MQTT_ENABLE" = 1 ] && {
echo '# ---- MQTT LISTENER PROPERTIES ----'
echo "listener $IOT_MQTT_PORT INADDR_ANY mqtt"
echo '  max_connections 117'
echo
}

echo '# ---- MQTT-SN LISTENER PROPERTIES ----'
echo 'listener 1884 127.0.0.1 mqtts'
echo '  max_connections 117'
echo

[ "$IOT_MQTT_BRG_ENABLE" = 1 ] && {
echo '# ---- BRIDGE PROPERTIES ----'

# If the settings file contains invalid bridge values, then comment out the entire bridge section of the
# configuration file.
comment='#'
[ -n "$IOT_MQTT_BRG_ADDR" ] && [ "$IOT_MQTT_BRG_PORT" != 0 ] && {
comment=''
} || {
echo 'ERROR:    MQTT bridging is disabled due to invalid values specified in the settings file.' >2
echo '# MQTT bridging is disabled due to invalid values specified in the settings file.'
}

[ -z "$IOT_MQTT_BRG_ID" ] && {
echo ${comment}"connection Remote$$"
} || {
echo ${comment}"connection $IOT_MQTT_BRG_ID"
}
echo ${comment}'  notifications false'
echo ${comment}'  round_robin false'
echo ${comment}'  try_private false'

[ -n "$IOT_MQTT_BRG_PORT" ] && {
echo ${comment}"  addresses ${IOT_MQTT_BRG_ADDR}:${IOT_MQTT_BRG_PORT}"
} || {
echo ${comment}"  addresses ${IOT_MQTT_BRG_ADDR}"
}

echo ${comment}'  keepalive_interval 60'
echo ${comment}'  start_type automatic'
echo ${comment}'  idle_timeout 60'
echo ${comment}'  threshold 10'
echo ${comment}'  cleansession true'
[ -n "$IOT_MQTT_BRG_ID" ] && {
echo ${comment}"  clientid $IOT_MQTT_BRG_ID"
}
[ -n "$IOT_MQTT_BRG_USR" ] && {
echo ${comment}"  username $IOT_MQTT_BRG_USR"
}
[ -n "$IOT_MQTT_BRG_PW" ] && {
echo ${comment}"  password $IOT_MQTT_BRG_PW"
}
echo ${comment}'  topic +/+/data/# out'
echo ${comment}'  topic +/+/cfg/# out'
echo ${comment}'  topic +/+/ctl/# in'
echo ${comment}'  topic +/+/set/# in'
echo ${comment}'  topic sm/mote/+/# out'
echo
}
echo '# ---- END OF FILE ----'
