#!/usr/bin/env bash
set -euo pipefail

ROOT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)"
OPENVT_DIR="$ROOT_DIR/vendor/open-vt"

export XDG_RUNTIME_DIR="${XDG_RUNTIME_DIR:-/run/user/$(id -u)}"
export WAYLAND_DISPLAY="${WAYLAND_DISPLAY:-wayland-1}"

if [ ! -S "$XDG_RUNTIME_DIR/$WAYLAND_DISPLAY" ]; then
  echo "Wayland socket not found: $XDG_RUNTIME_DIR/$WAYLAND_DISPLAY"
  echo "Make sure the sway/Wayland session is running before launching OpenVT."
  exit 1
fi

if [ ! -d "$OPENVT_DIR" ]; then
  echo "OpenVT checkout missing at $OPENVT_DIR"
  exit 1
fi

cd "$OPENVT_DIR"

echo "Launching OpenVT in existing Wayland session"
echo "XDG_RUNTIME_DIR=$XDG_RUNTIME_DIR"
echo "WAYLAND_DISPLAY=$WAYLAND_DISPLAY"

exec godot4 --path "$OPENVT_DIR"
