#!/bin/sh /etc/rc.common
# MeshFlow node supervisor (procd). Keeps the agent + MediaMTX alive across
# crashes AND reboots -- replaces the one-shot setup_pi_node.sh launcher, which
# left the node down after every reboot (no supervisor).
#
# Settings live in /etc/config/meshflow (all optional; with no file the agent runs on
# the public default network with both backbone levers — cloud_egress, cot_bridge — off):
#
#   uci set meshflow.main=meshflow
#   uci set meshflow.main.network_name='recon-alpha'   # the network name + password
#   uci set meshflow.main.network_pass='s3cret'        #   your team uses in the app
#   uci set meshflow.main.cloud_egress='1'             # publish this mesh's streams to the
#                                                     #   cloud network — on ONE node per mesh:
#                                                     #   the gateway, by default
#   uci set meshflow.main.egress_designated='1'        # only if the gateway runs no MeshFlow
#                                                     #   (a plain OpenMANET GL.iNet): lets this
#                                                     #   Pi behind it egress instead
#   uci set meshflow.main.cot_bridge='1'               # share this mesh's ATAK CoT with the network,
#                                                     #   both ways (its own lever, independent of
#                                                     #   cloud_egress; see cot_mesh_bridge.go)
#   uci commit meshflow
#   /etc/init.d/meshflow restart

USE_PROCD=1
START=95
STOP=10

BASE=/root
[ -x "$BASE/meshflow" ] || BASE=/tmp

apply_mtu() {
    # bat0 + br-ahwlan 1460 is the network policy: it keeps a batman-encapsulated frame inside a
    # 1500-byte radio MTU. wlan0 1532 is the older (OpenMANET 1.6) belt-and-braces for a 1500 bat0;
    # on 1.8 the HaLow interface is wlh0 and the line is a harmless no-op.
    ip link set wlan0 mtu 1532 2>/dev/null
    ip link set bat0 mtu 1460 2>/dev/null
    ip link set br-ahwlan mtu 1460 2>/dev/null
}

start_service() {
    local name network_name network_pass cloud_egress egress_designated cot_bridge env
    name="$(cat /proc/sys/kernel/hostname 2>/dev/null)"
    apply_mtu

    config_load meshflow
    config_get network_name main network_name ''
    config_get network_pass main network_pass ''
    config_get_bool cloud_egress main cloud_egress 0
    config_get_bool egress_designated main egress_designated 0
    config_get_bool cot_bridge main cot_bridge 0

    procd_open_instance meshflow
    procd_set_param command "$BASE/meshflow" -debug -ui-path "$BASE/ui" -name "$name"
    [ -n "$network_name" ] && procd_append_param command -network-name "$network_name"
    [ -n "$network_pass" ] && procd_append_param command -network-pass "$network_pass"
    # Two independent backbone levers, both off by default. cloud_egress: advertise this mesh's streams to
    # the cloud network and serve cloud viewers from the local MediaMTX. cot_bridge: share this mesh's
    # ATAK CoT with the network, both ways. Either only acts while the node is the egress point (WAN
    # gateway: batctl gw server + internet, or egress_designated).
    env=""
    [ "$cloud_egress" = 1 ] && env="MESHFLOW_CLOUD_EGRESS=1 MESHFLOW_EGRESS_WHEP_BASE=http://127.0.0.1:8889"
    [ "$cot_bridge" = 1 ] && env="$env MESHFLOW_COT_BRIDGE=1"
    [ -n "$env" ] && [ "$egress_designated" = 1 ] && env="$env MESHFLOW_EGRESS_DESIGNATED=1"
    [ -n "$env" ] && procd_set_param env $env
    procd_set_param respawn 3600 5 0
    procd_set_param stdout 1
    procd_set_param stderr 1
    procd_close_instance

    procd_open_instance mediamtx
    if [ -f "$BASE/mediamtx.yml" ]; then
        procd_set_param command "$BASE/mediamtx" "$BASE/mediamtx.yml"
    else
        procd_set_param command "$BASE/mediamtx"
    fi
    procd_set_param respawn 3600 5 0
    procd_set_param stdout 1
    procd_set_param stderr 1
    procd_close_instance
}

service_triggers() {
    procd_add_reload_trigger meshflow
}
