#!/bin/sh
# Copyright (c) 2026 Dab Co. Limited. All rights reserved.
#
# This software and its documentation are the confidential and
# proprietary information of Dab Co. Limited. Redistribution or use
# in source or binary forms, with or without modification, is not
# permitted without the express written consent of Dab Co. Limited.
#
# /etc/uci-defaults/pps-vpn — seed /etc/config/ppsvpn.
#
# Runs in TWO situations, and must be identical in both:
#   1. Firmware bake / first boot — /etc/init.d/boot calls uci_apply_defaults(),
#      which executes this, deletes it on success, then commits. Confirmed on a
#      G52x device by Lantronix.
#   2. Live `opkg install` — postinst executes this directly, then removes it.
#
# Keeping the seed HERE rather than in postinst means the baked firmware and a
# live install produce the same config from the same source. Every operation is
# idempotent: existing operator values are never overwritten.

UCI_PKG="ppsvpn"
UCI_SECTION="config"

# Ensure the file and named section exist.
[ -f "/etc/config/$UCI_PKG" ] || touch "/etc/config/$UCI_PKG"
# Holds the passphrase in plaintext.
chmod 600 "/etc/config/$UCI_PKG" 2>/dev/null || true
if ! uci -q get "${UCI_PKG}.${UCI_SECTION}" >/dev/null 2>&1; then
    uci -q set "${UCI_PKG}.${UCI_SECTION}=ppsvpn" 2>/dev/null || true
fi

# ── Rename migrations MUST run BEFORE _uci_seed ──
# _uci_seed is "fill if empty". Seeding first would make the new keys
# non-empty by the time the migration checks, silently discarding the
# operator's legacy value in favour of a fresh default.
_migrate() {
    _old="$1"; _new="$2"
    _v=$(uci -q get "${UCI_PKG}.${UCI_SECTION}.${_old}" 2>/dev/null)
    if [ -n "$_v" ] && [ -z "$(uci -q get "${UCI_PKG}.${UCI_SECTION}.${_new}" 2>/dev/null)" ]; then
        uci -q set "${UCI_PKG}.${UCI_SECTION}.${_new}=${_v}" 2>/dev/null || true
    fi
    uci -q delete "${UCI_PKG}.${UCI_SECTION}.${_old}" 2>/dev/null || true
}
_migrate remote    server_address
_migrate port      server_port
_migrate reneg_sec reneg_interval

# Legacy: verb (int) -> log_level (enum).
_legacy_verb=$(uci -q get "${UCI_PKG}.${UCI_SECTION}.verb" 2>/dev/null)
if [ -n "$_legacy_verb" ] && [ -z "$(uci -q get "${UCI_PKG}.${UCI_SECTION}.log_level" 2>/dev/null)" ]; then
    case "$_legacy_verb" in
        1)  _lv="error" ;;
        3)  _lv="warning" ;;
        5)  _lv="info" ;;
        6|7|8|9|10|11) _lv="debug" ;;
        *)  _lv="warning" ;;
    esac
    uci -q set "${UCI_PKG}.${UCI_SECTION}.log_level=${_lv}" 2>/dev/null || true
fi
uci -q delete "${UCI_PKG}.${UCI_SECTION}.verb" 2>/dev/null || true

# Legacy: keepalive "N M" -> keepalive_interval N + keepalive_timeout M.
_legacy_ka=$(uci -q get "${UCI_PKG}.${UCI_SECTION}.keepalive" 2>/dev/null)
if [ -n "$_legacy_ka" ]; then
    _ka_int=$(echo "$_legacy_ka" | awk '{print $1}')
    _ka_to=$( echo "$_legacy_ka" | awk '{print $2}')
    [ -z "$(uci -q get "${UCI_PKG}.${UCI_SECTION}.keepalive_interval" 2>/dev/null)" ] && \
        [ -n "$_ka_int" ] && \
        uci -q set "${UCI_PKG}.${UCI_SECTION}.keepalive_interval=${_ka_int}" 2>/dev/null || true
    [ -z "$(uci -q get "${UCI_PKG}.${UCI_SECTION}.keepalive_timeout" 2>/dev/null)" ] && \
        [ -n "$_ka_to" ] && \
        uci -q set "${UCI_PKG}.${UCI_SECTION}.keepalive_timeout=${_ka_to}" 2>/dev/null || true
fi
uci -q delete "${UCI_PKG}.${UCI_SECTION}.keepalive" 2>/dev/null || true

_uci_seed() {
    key="$1"; val="$2"
    cur=$(uci -q get "${UCI_PKG}.${UCI_SECTION}.${key}" 2>/dev/null)
    [ -z "$cur" ] && uci -q set "${UCI_PKG}.${UCI_SECTION}.${key}=${val}" 2>/dev/null || true
}

# Operator-required fields — LEFT EMPTY on a fresh install (matches NTC-502).
# pre-flight refuses to start until server_address / naf_id / passphrase are set.
_uci_seed server_address       ""
_uci_seed server_port          ""
_uci_seed naf_id               ""
_uci_seed passphrase           ""
# Tuning defaults.
_uci_seed reneg_interval       "86400"
_uci_seed reneg_bytes          "52428800"
_uci_seed keepalive_interval   "20"
_uci_seed keepalive_timeout    "240"
_uci_seed reconnect_delay      "5"
_uci_seed max_reconnect_delay  "300"
_uci_seed log_level            "warning"
_uci_seed key_cache_enable     "0"
_uci_seed mtu                  "1330"
_uci_seed mtu_auto             "1"
# Fleet-managed (not customer-facing, exposed for support).
_uci_seed simtrust_host        "best.pairpoint.net"
_uci_seed simtrust_port        "5056"
_uci_seed serial_port          "/dev/ttyUSB2"
_uci_seed baudrate             "115200"
# G26 netmon knobs.
_uci_seed netmon_enabled       "1"
_uci_seed netmon_interval      "20"
_uci_seed netmon_fails         "3"

# Scrub stale server-contract keys left by very old seeds.
for _k in proto dev cipher auth tls_version_min tls_version_max push_route_lan dns_override; do
    uci -q get "${UCI_PKG}.${UCI_SECTION}.${_k}" >/dev/null 2>&1 && \
        uci -q delete "${UCI_PKG}.${UCI_SECTION}.${_k}" 2>/dev/null || true
done

uci -q commit "$UCI_PKG" 2>/dev/null || true
exit 0
