#!/bin/sh
# XTeleport launcher for Linux.
#
# One package has to run on Ubuntu 20.04 (WebKitGTK 4.0 only), 22.04 (4.0 and
# 4.1) and 24.04 (4.1 only), as well as on Debian 11/12 and Fedora. A single
# GTK/WebKit binary cannot link against both ABIs, so the package ships two
# builds under /usr/lib/xteleport and this launcher picks the one whose
# WebKitGTK library is installed:
#
#   xteleport-wails-webkit41  linked against libwebkit2gtk-4.1.so.0
#   xteleport-wails-webkit40  linked against libwebkit2gtk-4.0.so.37
set -u

LIBDIR="${XTELEPORT_LIBDIR:-/usr/lib/xteleport}"

have_lib() {
  lib="$1"
  for ldconfig in ldconfig /sbin/ldconfig /usr/sbin/ldconfig; do
    if command -v "$ldconfig" >/dev/null 2>&1; then
      if "$ldconfig" -p 2>/dev/null | grep -q "$lib"; then
        return 0
      fi
      break
    fi
  done
  for dir in \
    /usr/lib/x86_64-linux-gnu /usr/lib/aarch64-linux-gnu /usr/lib/arm-linux-gnueabihf \
    /lib/x86_64-linux-gnu /lib/aarch64-linux-gnu \
    /usr/lib64 /usr/lib /usr/local/lib /usr/local/lib64; do
    if [ -e "$dir/$lib" ]; then
      return 0
    fi
  done
  return 1
}

pick=""
if [ -n "${XTELEPORT_WEBKIT:-}" ] && [ -x "$LIBDIR/xteleport-wails-$XTELEPORT_WEBKIT" ]; then
  pick="$LIBDIR/xteleport-wails-$XTELEPORT_WEBKIT"
elif [ -x "$LIBDIR/xteleport-wails-webkit41" ] && have_lib libwebkit2gtk-4.1.so.0; then
  pick="$LIBDIR/xteleport-wails-webkit41"
elif [ -x "$LIBDIR/xteleport-wails-webkit40" ] && have_lib libwebkit2gtk-4.0.so.37; then
  pick="$LIBDIR/xteleport-wails-webkit40"
else
  # Fall back to whatever variant is installed and let the dynamic loader
  # report the exact missing library.
  for candidate in "$LIBDIR/xteleport-wails-webkit41" "$LIBDIR/xteleport-wails-webkit40" "$LIBDIR/xteleport-wails"; do
    if [ -x "$candidate" ]; then
      pick="$candidate"
      break
    fi
  done
fi

if [ -z "$pick" ]; then
  message="XTeleport: no GUI binary found in $LIBDIR. Reinstall the xteleport package."
  echo "$message" >&2
  command -v notify-send >/dev/null 2>&1 && notify-send "XTeleport" "$message" || true
  exit 1
fi

# Diagnostics: `xteleport-wails --print-binary` reports the selected build
# without starting the GUI (used by packaging smoke tests).
if [ "${1:-}" = "--print-binary" ]; then
  printf '%s\n' "$pick"
  exit 0
fi

if ! have_lib libwebkit2gtk-4.1.so.0 && ! have_lib libwebkit2gtk-4.0.so.37; then
  message="XTeleport needs WebKitGTK. Install libwebkit2gtk-4.1-0 (Ubuntu 22.04/24.04, Debian 12) or libwebkit2gtk-4.0-37 (Ubuntu 20.04, Debian 11)."
  echo "$message" >&2
  command -v notify-send >/dev/null 2>&1 && notify-send "XTeleport" "$message" || true
fi

# WebKitGTK 2.42+ (Ubuntu 24.04) renders through DMA-BUF by default, which
# produces a blank or crashing window on many VMs, NVIDIA and remote-desktop
# setups. Disable it unless the user explicitly chose otherwise.
: "${WEBKIT_DISABLE_DMABUF_RENDERER:=1}"
export WEBKIT_DISABLE_DMABUF_RENDERER

# Let the application reference the launcher (not the variant binary) in the
# XDG autostart entry it creates for "Launch at login".
XTELEPORT_LAUNCHER="$(readlink -f "$0" 2>/dev/null || printf '%s' "$0")"
export XTELEPORT_LAUNCHER

exec "$pick" "$@"
