#!/bin/sh /etc/rc.common
# procd service for the Qetra Rust client, driven by UCI (/etc/config/qetra).
#
# Runs `qetra up` as a long-lived daemon: it reconciles the WireGuard interface
# against the live Management server (Login + streaming Sync) and connects peers
# through the relay. Settings come from UCI; the persistent WireGuard key lives in
# /etc/qetra/config.json (auto-created on first start). The setup key is only
# needed for the first registration — afterwards the peer re-authenticates by its
# wg pubkey with no key.

USE_PROCD=1
# Start late (after network/WAN); stop early.
START=95
STOP=10

# Locate the binary. RUTOS opkg installs under /usr/local (its default dest),
# stock OpenWrt uses /usr/bin — resolve at runtime instead of hardcoding. The
# legacy netbird names are still tried so a partially-migrated device (old
# package binary, new init) keeps working through the rename transition.
find_prog() {
	local p
	for p in /usr/local/usr/bin/qetra /usr/bin/qetra /usr/local/bin/qetra \
		/usr/local/usr/bin/netbird /usr/bin/netbird /usr/local/bin/netbird; do
		[ -x "$p" ] && { echo "$p"; return 0; }
	done
	command -v qetra 2>/dev/null || echo /usr/bin/qetra
}

start_service() {
	config_load qetra

	local enabled management_url setup_key hostname log_level acl lazy ice
	local web_enabled web_port web_password
	config_get_bool enabled main enabled 0
	config_get management_url main management_url ''
	config_get setup_key main setup_key ''
	config_get hostname main hostname ''
	config_get log_level main log_level 'info'
	config_get_bool acl main acl 0
	config_get_bool lazy main lazy 0
	config_get_bool ice main ice 0
	config_get_bool web_enabled main web_enabled 0
	config_get web_port main web_port '8088'
	config_get web_password main web_password ''

	if [ "$enabled" != "1" ]; then
		logger -t qetra "disabled — set 'option enabled 1' in /etc/config/qetra"
		return 1
	fi
	if [ -z "$management_url" ]; then
		logger -t qetra "management_url not set in /etc/config/qetra"
		return 1
	fi
	[ -n "$hostname" ] || hostname="$(cat /proc/sys/kernel/hostname 2>/dev/null || echo qetra-rs)"

	local prog
	prog="$(find_prog)"

	# Optional standalone web UI (config + status) on a LAN port.
	local web_args=""
	if [ "$web_enabled" = "1" ]; then
		web_args="--web --web-port $web_port"
		[ -n "$web_password" ] && web_args="$web_args --web-password $web_password"
	fi

	# Inbound ACL enforcement (opt-in; off by default — see the UCI comment).
	local acl_args=""
	[ "$acl" = "1" ] && acl_args="--acl"

	# Lazy relay connections (opt-in; off by default — for very large networks).
	local lazy_args=""
	[ "$lazy" = "1" ] && lazy_args="--lazy"

	# ICE-lite direct P2P (opt-in; off by default — relay stays the fallback).
	local ice_args=""
	[ "$ice" = "1" ] && ice_args="--ice"

	procd_open_instance
	# --management-url self-initializes the config (fresh key) on first boot, so
	# no separate `qetra init` step is needed.
	procd_set_param command "$prog" up --management-url "$management_url" --hostname "$hostname" $web_args $acl_args $lazy_args $ice_args
	if [ -n "$setup_key" ]; then
		procd_set_param env RUST_LOG="$log_level" QETRA_SETUP_KEY="$setup_key"
	else
		procd_set_param env RUST_LOG="$log_level"
	fi
	# Restart on crash: <=5 respawns within 3600s, else give up (0 = keep trying).
	procd_set_param respawn 3600 5 0
	procd_set_param stdout 1
	procd_set_param stderr 1
	procd_close_instance
}

stop_service() {
	# procd stops the `up` process; tear the interface down too.
	"$(find_prog)" down 2>/dev/null
}

# `/etc/init.d/qetra reload` when the UCI config changes (used by the web UI /
# LuCI to apply settings without a manual restart).
service_triggers() {
	procd_add_reload_trigger "qetra"
}
