#!/usr/bin/env bash

: <<'END'
dhqsstvd
DigiHub qSSTV VNC session wrapper - starts a TigerVNC desktop + noVNC
bridge and runs qSSTV on it

Version 0.1

Steve de Bode - W0FFS - July 2026

Input:	none - runs continuously under systemd (unit: dhqsstv.service)
	reads $HOME/.config/tigervnc/{xstartup,passwd}, generated by
	install.sh from $HOME/.dhinfo's vncpass
Output:	none - runs qSSTV (via a VNC desktop) 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
}

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

# Deliberately "tigervncserver", not "vncserver" -- see run_vnc_session()
# in dh.lib for why (Raspberry Pi OS ships a conflicting RealVNC binary
# under the bare "vncserver"/"vncpasswd" names).
if ! command -v tigervncserver >/dev/null 2>&1; then
  err_section "DigiHub qSSTV"
  err_kv "Message" "tigervncserver is not installed."
  err_kv "Action" "sudo apt-get install tigervnc-standalone-server tigervnc-tools novnc websockify"
  printf "\n" >&2
  exit 1
fi

section "DigiHub qSSTV"
kv "VNC"   ":1 (port 5901)"
kv "noVNC" "port 6080"
printf "\n"

run_vnc_session qsstv qsstv
