#!/usr/bin/env bash

: <<'END'
dhnoded
DigiHub AX.25 node/BBS service wrapper - attaches Direwolf's KISS pty to
the kernel AX.25 stack with kissattach, then runs ax25d, which dispatches
incoming connections to uronode

Version 0.1

Steve de Bode - W0FFS - July 2026

Input:	none - runs continuously under systemd (unit: dhnode.service, as
	root: kissattach/ax25d need to create and bind a kernel AX.25
	network interface, which requires root/CAP_NET_ADMIN)
	reads /etc/ax25/axports and /etc/ax25/ax25d.conf, generated by
	nodeconf.py when "node" mode is selected via dhmode; requires
	dhdirewolf.service to already have created /tmp/kisstnc
	(Direwolf's -p KISS pty)
Output:	none - runs ax25d until stopped
END

set -uo pipefail

UI_LIB="/usr/local/lib/digihub/dh.lib"
[ -r "$UI_LIB" ] && source "$UI_LIB" || {
  printf "\nERROR: Missing UI library: %s\n\n" "$UI_LIB" >&2
  exit 1
}

KISS_DEV="/tmp/kisstnc"
PORT_NAME="radio"

if ! command -v kissattach >/dev/null 2>&1 || ! command -v ax25d >/dev/null 2>&1; then
  err_section "DigiHub AX.25 Node"
  err_kv "Message" "kissattach/ax25d are not installed."
  err_kv "Action" "sudo apt-get install ax25-tools"
  printf "\n" >&2
  exit 1
fi

if ! command -v uronode >/dev/null 2>&1; then
  err_section "DigiHub AX.25 Node"
  err_kv "Message" "uronode is not installed."
  err_kv "Action" "sudo apt-get install uronode"
  printf "\n" >&2
  exit 1
fi

if [ ! -f /etc/ax25/axports ] || [ ! -f /etc/ax25/ax25d.conf ]; then
  err_section "DigiHub AX.25 Node"
  err_kv "Message" "AX.25 node configuration not found."
  err_kv "Action" "Select node mode first, e.g.: dhmode node"
  printf "\n" >&2
  exit 1
fi

# Some kernels (notably WSL2's default kernel) are built without AX.25
# protocol support. Fail clearly here rather than let kissattach silently
# fail to create the interface.
modprobe ax25 >/dev/null 2>&1 || true
if [ ! -d /proc/net/ax25 ]; then
  err_section "DigiHub AX.25 Node"
  err_kv "Message" "This kernel has no AX.25 protocol support."
  err_kv "Detail" "modprobe ax25 failed and /proc/net/ax25 does not exist."
  err_kv "Action" "AX.25 node/BBS needs a kernel built with CONFIG_AX25 (not available on WSL2's default kernel); not available on this system."
  printf "\n" >&2
  exit 1
fi

# Direwolf (dhdirewolf.service) creates /tmp/kisstnc when "node" mode
# starts it with -p; give it a few seconds if it hasn't appeared yet
# rather than failing on a benign startup race.
for _ in $(seq 1 15); do
  [ -e "$KISS_DEV" ] && break
  sleep 1
done

if [ ! -e "$KISS_DEV" ]; then
  err_section "DigiHub AX.25 Node"
  err_kv "Message" "$KISS_DEV was not created."
  err_kv "Action" "Check that dhdirewolf.service is running: systemctl status dhdirewolf"
  printf "\n" >&2
  exit 1
fi

section "DigiHub AX.25 Node"
kv "Port"   "$PORT_NAME"
kv "Device" "$KISS_DEV"
printf "\n"

if ! kissattach -l "$KISS_DEV" "$PORT_NAME"; then
  err_section "DigiHub AX.25 Node"
  err_kv "Message" "kissattach failed to attach $KISS_DEV as port $PORT_NAME."
  err_kv "Action" "Check /etc/ax25/axports and: journalctl -u dhnode"
  printf "\n" >&2
  exit 1
fi

# Give the interface a moment to come up before tuning it.
sleep 1
kissparms -p "$PORT_NAME" -f n >/dev/null 2>&1 || true

exec ax25d -l
