#!/usr/bin/env bash
# Run GUI-adjacent CI commands safely on headless Jenkins agents.
set -euo pipefail

lesavka_ci_run_headless() {
  local cmd=("$@")

  if [[ -z "${DISPLAY:-}" && "${LESAVKA_CI_HEADLESS_GTK:-1}" != "0" ]] && command -v xvfb-run >/dev/null 2>&1; then
    cmd=(xvfb-run -a -s "-screen 0 1920x1080x24" "${cmd[@]}")
  fi
  if command -v dbus-run-session >/dev/null 2>&1; then
    cmd=(dbus-run-session -- "${cmd[@]}")
  fi

  "${cmd[@]}"
}
