#!/usr/bin/env bash

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

Version 0.1

Steve de Bode - W0FFS - July 2026

Input:	none - runs continuously under systemd (unit: dhjs8call.service)
	reads $HOME/.config/tigervnc/{xstartup,passwd}, generated by
	install.sh from $HOME/.dhinfo's vncpass
Output:	none - runs JS8Call (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 js8call >/dev/null 2>&1; then
  err_section "DigiHub JS8Call"
  err_kv "Message" "js8call is not installed."
  err_kv "Action" "sudo apt-get install js8call"
  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 JS8Call"
  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 JS8Call"
kv "VNC"   ":1 (port 5901)"
kv "noVNC" "port 6080"
printf "\n"

run_vnc_session js8call js8call
