#!/bin/sh
# SyslogWatch without a display — for servers.
#
# The packaged `syslogwatch` binary is Electron, and Electron is also a complete
# Node.js. With ELECTRON_RUN_AS_NODE=1 it skips the browser side entirely and
# runs a plain script, so no X server is contacted, no GPU is opened and no
# window is ever created. The script it runs is the headless entry point inside
# app.asar.
#
#   /opt/SyslogWatch/syslogwatch-headless            # foreground
#   /opt/SyslogWatch/syslogwatch-headless --help     # options
#   /opt/SyslogWatch/syslogwatch-headless --show-url # the web UI address again
#
# To run it as a service, see syslogwatch-headless.service next to this file.
#
# One caveat, and it is the reason for the check below: not touching the desktop
# libraries is not the same as not needing them. The dynamic loader resolves
# every library the Electron binary is linked against — GTK, X11, NSS, ALSA —
# before a single line of our code runs. The .deb pulls them in as dependencies,
# but the .tar.gz cannot, and a minimal server does not have them. All the
# loader says then is "error while loading shared libraries: <one name>", which
# tells an administrator nothing about what to install.
here=$(cd "$(dirname "$0")" && pwd)
app="$here/syslogwatch"

if [ ! -x "$app" ]; then
  echo "syslogwatch-headless: cannot find the SyslogWatch binary next to this script ($app)." >&2
  echo "Run this script from the folder it was installed into." >&2
  exit 1
fi

# Cheap: one process that starts and exits immediately (~0.1s), once per start.
probe=$(ELECTRON_RUN_AS_NODE=1 "$app" -e "" 2>&1)
probe_status=$?
if [ $probe_status -ne 0 ]; then
  case "$probe" in
    *"error while loading shared libraries"*|*"cannot open shared object file"*)
      echo "syslogwatch-headless: this server is missing libraries that the SyslogWatch" >&2
      echo "runtime is linked against. Headless mode opens no window, but the libraries" >&2
      echo "still have to be present for the program to start at all." >&2
      echo "" >&2
      if command -v ldd >/dev/null 2>&1; then
        echo "Missing here:" >&2
        ldd "$app" 2>/dev/null | grep "not found" | awk '{print "    " $1}' | sort -u >&2
        echo "" >&2
      fi
      echo "Debian / Ubuntu:" >&2
      echo "    sudo apt-get install -y libgtk-3-0 libnotify4 libnss3 libxss1 libxtst6 \\" >&2
      echo "        xdg-utils libatspi2.0-0 libuuid1 libsecret-1-0 libgbm1 libasound2" >&2
      echo "    (on Ubuntu 24.04 and Debian trixie or newer, libasound2 is libasound2t64)" >&2
      echo "" >&2
      echo "RHEL / Rocky / Alma / Fedora:" >&2
      echo "    sudo dnf install -y gtk3 libnotify nss libXScrnSaver libXtst xdg-utils \\" >&2
      echo "        at-spi2-atk libuuid libsecret mesa-libgbm alsa-lib" >&2
      echo "" >&2
      echo "The .deb package installs these for you — it is the easier route on Debian" >&2
      echo "and Ubuntu. See README-headless.txt next to this script." >&2
      exit 1
      ;;
    *)
      echo "syslogwatch-headless: the SyslogWatch runtime would not start." >&2
      echo "$probe" >&2
      exit $probe_status
      ;;
  esac
fi

ELECTRON_RUN_AS_NODE=1 exec "$app" "$here/resources/app.asar/src/headless.js" "$@"
