#!/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/setup_migrate << '__EOF__'

import * as fs from "fs";
import * as uci from "uci";

fs.open("/etc/config.mesh/setup", "a").close();
const c = uci.cursor("/etc/config.mesh");

let dmz_mode = null;
function dmz_file(file)
{
    if (dmz_mode == null) {
        if ((fs.stat(`${file}dmz`, "size")?.size ?? -1) > 0) {
            return `${file}dmz`;
        }
        else if ((fs.stat(`${file}nat`, "size")?.size ?? -1) > 0) {
            return `${file}nat`;
        }
    }
    else {
        if (dmz_mode >= 2 && (fs.stat(`${file}dmz`, "size")?.size ?? -1) > 0) {
            return `${file}dmz`;
        }
        else if ((fs.stat(`${file}nat`, "size")?.size ?? -1) > 0) {
            return `${file}nat`;
        }
    }
    return null;
}

// Migrate the old school _setup file
if (!c.get("setup", "globals") && fs.access("/etc/config.mesh/_setup")) {
    c.set("setup", "globals", "globals");
    const f = fs.open("/etc/config.mesh/_setup");
    if (f) {
        for (let line = f.read("line"); length(line); line = f.read("line")) {
            line = trim(line);
            const m = match(line, /^(.+)*=(.*)$/);
            if (m) {
                const k = trim(m[1]);
                const v = trim(m[2]);
                if (k && v) {
                    c.set("setup", "globals", k, v);
                    if (k == "dmz_mode") {
                        dmz_mode = int(v) || -1;
                    }
                }
            }
        }
        f.close();
    }
}

// Migrate the old school _setup.services.{nat,dmz} files
if (!c.get("setup", "services")) {
    const file = dmz_file("/etc/config.mesh/_setup.services.");
    c.set("setup", "services", "services");
    const f = fs.open(file);
    if (f) {
        const services = [];
        for (let line = f.read("line"); length(line); line = f.read("line")) {
            push(services, trim(line));
        }
        f.close();
        if (length(services) > 0) {
            c.set("setup", "services", "service", services);
        }
    }
}

// Migrate the old school _setup.dhcp.{nat,dmz} files
if (!c.get("setup", "dhcpreservations")) {
    const file = dmz_file("/etc/config.mesh/_setup.dhcp.");
    c.set("setup", "dhcpreservations", "dhcpreservations");
    const f = fs.open(file);
    if (f) {
        const dhcp = [];
        for (let line = f.read("line"); length(line); line = f.read("line")) {
            push(dhcp, trim(line));
        }
        f.close();
        if (length(dhcp) > 0) {
            c.set("setup", "dhcpreservations", "reservation", dhcp);
        }
    }
}

// Migrate the old school _setup.dhcptags.{nat,dmz} files
if (!c.get("setup", "dhcptags")) {
    const file = dmz_file("/etc/config.mesh/_setup.dhcptags.");
    c.set("setup", "dhcptags", "dhcptags");
    const f = fs.open(file);
    if (f) {
        const dhcp = [];
        for (let line = f.read("line"); length(line); line = f.read("line")) {
            push(dhcp, trim(line));
        }
        f.close();
        if (length(dhcp) > 0) {
            c.set("setup", "dhcptags", "tag", dhcp);
        }
    }
}

// Migrate the old school _setup.dhcpoptions.{nat,dmz} files
if (!c.get("setup", "dhcpoptions")) {
    const file = dmz_file("/etc/config.mesh/_setup.dhcpoptions.");
    c.set("setup", "dhcpoptions", "dhcpoptions");
    const f = fs.open(file);
    if (f) {
        const dhcp = [];
        for (let line = f.read("line"); length(line); line = f.read("line")) {
            push(dhcp, trim(line));
        }
        f.close();
        if (length(dhcp) > 0) {
            c.set("setup", "dhcpoptions", "option", dhcp);
        }
    }
}

// Migrate the old school _setup.ports.{nat,dmz} files
if (!c.get("setup", "ports")) {
    const file = dmz_file("/etc/config.mesh/_setup.ports.");
    c.set("setup", "ports", "ports");
    const f = fs.open(file);
    if (f) {
        const ports = [];
        for (let line = f.read("line"); length(line); line = f.read("line")) {
            push(ports, trim(line));
        }
        f.close();
        if (length(ports) > 0) {
            c.set("setup", "ports", "port", ports);
        }
    }
}

// Migrate the old school _setup.aliases.{nat,dmz} files
if (!c.get("setup", "aliases")) {
    const file = dmz_file("/etc/config.mesh/aliases.");
    c.set("setup", "aliases", "aliases");
    const f = fs.open(file);
    if (f) {
        const aliases = [];
        for (let line = f.read("line"); length(line); line = f.read("line")) {
            push(aliases, trim(line));
        }
        f.close();
        if (length(aliases) > 0) {
            c.set("setup", "aliases", "alias", aliases);
        }
    }
}

c.commit("setup");

fs.unlink("/etc/config.mesh/_setup");
fs.unlink("/etc/config.mesh/_setup.services.dmz");
fs.unlink("/etc/config.mesh/_setup.services.nat");
fs.unlink("/etc/config.mesh/_setup.dhcp.dmz");
fs.unlink("/etc/config.mesh/_setup.dhcp.nat");
fs.unlink("/etc/config.mesh/_setup.dhcptags.dmz");
fs.unlink("/etc/config.mesh/_setup.dhcptags.nat");
fs.unlink("/etc/config.mesh/_setup.dhcpoptions.dmz");
fs.unlink("/etc/config.mesh/_setup.dhcpoptions.nat");
fs.unlink("/etc/config.mesh/_setup.ports.dmz");
fs.unlink("/etc/config.mesh/_setup.ports.nat");
fs.unlink("/etc/config.mesh/aliases.dmz");
fs.unlink("/etc/config.mesh/aliases.nat");
fs.unlink("/etc/config.mesh/aliases");

__EOF__
/usr/bin/ucode /tmp/setup_migrate
rm -f /tmp/setup_migrate
