#!/bin/sh

#
#   This script parses all important values into a json file. With this
#   file one can restore the complete basic configuration of a freifunk
#   berlin router.
#
#   It originates from Freifunk Berlin and is licensed under
#   GNU General Public License v3.0 or later
#   Copyright (C) 2023   Martin Hübner
#

# set -e
# shellcheck shell=dash
# shellcheck disable=SC2086
# shellcheck disable=SC2154

# shellcheck source=/dev/null
. /lib/functions.sh
# shellcheck source=/dev/null
. /usr/share/libubox/jshn.sh

# get contact and community data
uci_load freifunk
name="$(uci_get freifunk contact name)"
nick="$(uci_get freifunk contact nickname)"
mail="$(uci_get freifunk contact mail)"
phone="$(uci_get freifunk contact phone)"
homepage="$(uci_get freifunk contact homepage)" # whitespace-separated, with single quotes, if string contains whitspace
loc_description="$(uci_get freifunk contact location)"
com_name="$(uci_get freifunk community name)"

# collect nodes location
uci_load system
longitude="$(uci_get system @system[-1] longitude)"
latitude="$(uci_get system @system[-1] latitude)"

# if contact field contains a config-wizard URL, write it into URL-field
if case "$mail" in
    https://config.berlin.freifunk.net/contact*) true ;;
    *) false ;;
esac then
    url="$mail"
    mail=""
fi

# load IP-Addresses
uci_load ffwizard
share="$(uci_get ffwizard settings sharenet)"
usersBandwidthDown=$(uci_get ffwizard settings usersBandwidthDown)
download="$((usersBandwidthDown * 1000))"
usersBandwidthUp=$(uci_get ffwizard settings usersBandwidthUp)
upload="$((usersBandwidthUp * 1000))"
monitoring="$(uci_get ffwizard settings enableStats)"
mesh1="$(uci_get ffwizard settings meship_radio0)"
mesh2="$(uci_get ffwizard settings meship_radio1)"
dhcp="$(uci_get ffwizard settings dhcpmesh)"

# autoupdate-stuff
uci_load autoupdate
# swap value, as we have different properties here.
autoupdate_enabled="$((!$(uci_get autoupdate cfg disabled)))"
autoupdate_url="$(uci_get autoupdate cfg url)"

# tunnel-stuff
uci_load ffberlin-uplink
uplink_type=$(uci_get ffberlin-uplink preset current)
if [ "$uplink_type" = tunnelberlin_tunneldigger ]; then
    is_tunneldigger="tunneldigger"
else
    is_tunneldigger=""
fi

has_bbbdigger="$(uci_get tunneldigger bbbdigger interface)"

# get hostname
json_load "$(ubus call system board)"
json_get_var hostname hostname

#############################
#                           #
#   construct json-string   #
#                           #
#############################

json_init
{
    json_add_string "created" "$(date -u +"%Y-%m-%dT%H:%M:%S+00:00")"
    json_add_string "version" "1.0"

    json_add_array contact
    {
        json_add_object
        {
            if [ -n "$nick" ]; then json_add_string nickname "$nick"; fi
            if [ -n "$name" ]; then json_add_string realname "$name"; fi
            if [ -n "$mail" ]; then json_add_string email "$mail"; fi
            if [ -n "$homepage" ]; then json_add_string homepage "$homepage"; fi
            if [ -n "$phone" ]; then json_add_string phone "$phone"; fi
            json_add_string url "$url"
        }
        json_close_object
    }
    json_close_array

    json_add_object node
    {
        json_add_string name "$hostname"
        json_add_string community "$com_name"

        json_add_object location
        {
            json_add_double latitude $latitude
            json_add_double longitude $longitude
            json_add_string description "$loc_description"
        }
        json_close_object

        json_add_object internet
        {
            json_add_boolean share $share
            json_add_string uplink-tunnel "$is_tunneldigger"
            json_add_string mesh-tunnel "$has_bbbdigger"
            json_add_int download $download
            json_add_int upload $upload
        }
        json_close_object

        json_add_boolean monitoring $monitoring

        json_add_object autoupdate
        {
            json_add_boolean enabled $autoupdate_enabled
            json_add_string url "$autoupdate_url"
        }
        json_close_object

        json_add_object ips
        {
            json_add_array mesh
            {
                if [ -n "$mesh1" ]; then json_add_string "" "$mesh1/32"; fi
                if [ -n "$mesh2" ]; then json_add_string "" "$mesh2/32"; fi
            }
            json_close_array
            json_add_string dhcp "$dhcp"
            json_add_string ipv6 ""
        }
        json_close_object
    }
    json_close_object

    json_add_array sshkeys
    {
        while IFS= read -r line; do
            json_add_string "" "$line"
        done </etc/dropbear/authorized_keys
    }
    json_close_array
}
json_dump
