#!/bin/sh
true <<'LICENSE'
  Part of AREDN -- Used for creating Amateur Radio Emergency Data Networks
  Copyright (C) 2024 Tim Wilkinson
  See Contributors file for additional contributors

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation version 3 of the License.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.

  Additional Terms:

  Additional use restrictions exist on the AREDN(TM) trademark and logo.
    See AREDNLicense.txt for more info.

  Attributions to the AREDN Project must be retained in the source code.
  If importing this code into a new or existing project attribution
  to the AREDN project must be added to the source code.

  You must not misrepresent the origin of the material contained within.

  Modified versions must be modified to attribute to the original source
  and be marked in reasonable ways as differentiate it from the original
  version.

LICENSE

cat > /tmp/canonical_config << '__EOF__'

import * as fs from "fs";
import * as uci from "uci";
import * as hardware from "aredn.hardware";

const root = "/etc/config.mesh";
const DELETE = "__DELETE__";

function shell(cmd)
{
    const p = fs.popen(cmd);
    if (p) {
        const r = p.read("all");
        p.close();
        return trim(r);
    }
    return null;
}

const config = {
    aredn: {
        alerts: {},
        babel: {},
        beacon: {
            enable: 1
        },
        dmz: DELETE,
        downloads: {
            firmware_aredn: "http://downloads.arednmesh.org",
            packages_default: "http://downloads.arednmesh.org",
            firmwarepath: DELETE,
            pkgs_core: DELETE,
            pkgs_base: DELETE,
            pkgs_arednpackages: DELETE,
            pkgs_packages: DELETE,
            pkgs_luci: DELETE,
            pkgs_routing: DELETE,
            pkgs_telephony: DELETE
        },
        iperf: {
            enable: 1
        },
        location: {
            lat: shell("head -1 /etc/latlon 2>/dev/null"),
            lon: shell("tail -1 /etc/latlon 2>/dev/null; rm -f /etc/latlon"),
            gridsquare: shell("head -1 /etc/gridsquare 2>/dev/null ; rm -f /etc/gridsquare"),
            map: "https://worldmap.arednmesh.org/#12/(lat)/(lon)",
            gps_enable: 1,
            source: DELETE
        },
        lqm: {
            enable: DELETE,
            min_snr: DELETE,
            margin_snr: DELETE,
            rts_threshold: DELETE,
            min_distance: DELETE,
            max_distance: DELETE,
            min_quality: DELETE,
            ping_penalty: DELETE,
            margin_quality: DELETE,
            min_routes: DELETE
        },
        map: DELETE,
        meshstatus: DELETE,
        notes: {},
        ntp: {
            period: "daily",
        },
        poe: {},
        remotelog: {},
        remoterf: {
            enable: 0
        },
        supernode: {
            enable: 0,
            support: 1
        },
        theme: {
            portable: 0
        },
        time: {
            ntp_enable: 1,
            gps_enable: 1,
            military: 0
        },
        tunnel: {
            weight: 1
        },
        usb: {},
        wan: {
            lan_dhcp_route: 1,
            lan_dhcp_44route: 1,
            lan_dhcp_defaultroute: 0,
            olsrd_gw: DELETE,
            mesh_wan_gw: DELETE,
            ssh_access: 1,
            telnet_access: 1,
            web_access: 1,
            mesh_to_local_wan: shell("uci -q -c /etc/config.mesh get aredn.@wan[0].olsrd_gw") || 0,
            lan_to_remote_wan: shell("uci -q -c /etc/config.mesh get aredn.@wan[0].mesh_wan_gw") || 0,
            mesh_internet: 1,
            local_defaultroute: 1,
            monitor1: "1.1.1.1",
            monitor2: "8.8.8.8"
        },
        watchdog: {
            enable: 0,
        },
        wireless_watchdog: {
            enable: 1,
            daily: "03:00",
            lqm: 0
        }
    },
    setup: {
        globals: {
            dtdlink_ip: DELETE,
            wifi_mask: DELETE,
            olsrd_bridge: DELETE
        }
    },
    dhcp: {},
    dropbear: {},
    snmpd: {},
    uhttpd: {},
    wireguard: {},
    xlink: {}
};

const cursor = uci.cursor(root);
for (let fn in config) {
    const f = config[fn];
    if (!fs.access(`${root}/${fn}`)) {
        fs.writeFile(`${root}/${fn}`, "");
    }
    else {
        cursor.add(fn, "__dummy__");
        cursor.delete(fn, "@__dummy__[0]");
        const all = cursor.get_all(fn);
        for (let k in all) {
            let c = 0;
            for (let k1 in all[k]) {
                if (!match(k1, /^\./)) {
                    c++;
                }
            }
            if (c == 0) {
                cursor.delete(fn, k);
            }
        }
    }
    for (let sn in f) {
        const s = f[sn];
        const san = `@${sn}[0]`;
        if (s == DELETE) {
            cursor.delete(fn, san);
        }
        else {
            if (cursor.get(fn, san) === null) {
                cursor.add(fn, sn);
            }
            for (let on in s) {
                const o = s[on];
                if (o == DELETE) {
                    cursor.delete(fn, san, on);
                }
                else if (cursor.get(fn, san, on) === null) {
                    cursor.set(fn, san, on, o);
                }
            }
        }
    }
    cursor.commit(fn);
}
__EOF__
/usr/bin/ucode /tmp/canonical_config
rm -f /tmp/canonical_config
