#!/bin/sh /etc/rc.common
# nomad boot-time hooks: tmpfs over the client-DB and one-shot
# randomization of WiFi MACs + hostname / DHCP client-id. IMEI is NOT
# rotated here – that's an explicit user action.

START=10
STOP=89
USE_PROCD=1

NOMAD="/etc/nomad"
CLIENT_DB="/etc/oui-tertf"

start_service() {
    # 1) Mount tmpfs over the GL.iNet client-MAC database so it resets
    #    on every reboot. Equivalent to blue-merle's `volatile-client-macs`
    #    but without the `shred` antipattern on flash.
    # Guard via /proc/mounts, NOT `mountpoint` — that binary is absent on this
    # firmware, so `! mountpoint -q` always succeeded and stacked a fresh tmpfs
    # on every start/reload.
    if [ -d "$CLIENT_DB" ] && ! grep -q " $CLIENT_DB " /proc/mounts; then
        mount -t tmpfs -o size=2M tmpfs "$CLIENT_DB"
    fi

    # 2) Run the one-shot boot action (MAC/hostname randomization + sticky-
    #    state restore). It runs to completion and exits, so it must NOT be a
    #    procd `command` instance: procd RESTARTS a command whenever it exits,
    #    which turned this one-shot into an unkillable respawn loop (procd's
    #    `respawn 0 0 0` means *infinite* retries with 0s backoff — it re-imported
    #    the whole package every few seconds and could not be stopped, since each
    #    SIGTERM was followed by an instant respawn). Fire-and-forget in the
    #    background is the correct shape for a one-shot boot task.
    ( PYTHONPATH="$NOMAD" python3 -m nomad.cli boot >/dev/null 2>&1 ) &
}

stop_service() {
    if mountpoint -q "$CLIENT_DB"; then
        umount "$CLIENT_DB" 2>/dev/null || true
    fi
}
