#!/bin/sh
# userland/fasadas/launchers/agrun  ->  /bin/agrun  AND
#                                        Applications/AGreitis.aop/Contents/AOS/agrun
#
# The Exec= launcher for AGreitis.aop (the native Fasadas 3D benchmark, #420 /
# #673 / #667 / #674). It seeds the GPU environment the runtime side-load proved
# on the Dell (2026-09-02: on-window "ACCELERATED · Mesa Intel(R) HD Graphics
# 4400 (HSW GT2)", 99 fps) — but from the SHIPPED system locations, not /tmp, so
# a fresh install runs GPU-accelerated straight from the dock/Launchpad and
# survives a reboot.
#
# WHY THE BUNDLE CARRIES ITS OWN LAUNCHER (the ffstart/ffrun reasoning). The
# dock, Launchpad and `aoprun` start a bundle with no environment beyond
# aoprun's LD_LIBRARY_PATH. agreitis' GPU backend (agreitis_render_gl.c) opens a
# DRM render node, brings up surfaceless EGL/GBM and lets Mesa load a hardware
# DRI driver (crocus on Haswell) — and Mesa can only load that driver if
# LIBGL_DRIVERS_PATH names the directory holding *_dri.so. With a bare-binary
# Exec the variable is unset, Mesa finds no crocus, agr_gl_create() returns NULL
# and the dispatcher falls back to OSMesa softpipe — the app runs but says
# SOFTWARE. So a bundle whose Exec is the raw binary is not accelerated; the
# environment IS the acceleration.
#
# Two DRI locations, because the mesa-egl libraries reach the image two ways and
# both are legitimate:
#   * a tree build (`make osimage`) stages ports/mesa-egl/root64/lib/. into the
#     squashfs /lib, so crocus is /lib/dri/crocus_dri.so;
#   * a `make release` installs the mesa-egl PACKAGE at /Apps/mesa-egl/Current,
#     so crocus is /Apps/mesa-egl/Current/lib/dri/crocus_dri.so.
# LIBGL_DRIVERS_PATH is a colon list, so naming both makes one launcher correct
# for either image. crocus_dri.so's own deps (libdrm, libglapi) sit in the same
# lib dirs, so those go on LD_LIBRARY_PATH too.
#
# DELIBERATELY NOT forcing MESA_LOADER_DRIVER_OVERRIDE=crocus. The side-load
# forced it to remove all doubt on the one machine on the bench, but AGreitis
# ships by default on EVERY machine (drivers must be universal). Letting Mesa
# select the driver by PCI id keeps the GPU path correct on non-Haswell / non-
# Intel GPUs; and the backend already refuses a software GLES2 context
# (softpipe/llvmpipe -> NULL -> OSMesa), so a box with no hardware driver still
# falls cleanly to the honest SOFTWARE label. A caller may still export
# MESA_LOADER_DRIVER_OVERRIDE / AGREITIS_GL_NODE / AGREITIS_FORCE_SW to steer it.
set -u

HERE=$(cd "$(dirname "$0")" && pwd)

export FASADAS_SOCKET="${FASADAS_SOCKET:-/run/fasadas-0}"
export HOME="${HOME:-/root}"

# The two crocus/_dri.so locations (release package first, then the tree-build
# squashfs /lib), plus the conventional fallbacks.
DRI="/Apps/mesa-egl/Current/lib/dri:/lib/dri:/usr/lib/dri"
if [ -n "${LIBGL_DRIVERS_PATH:-}" ]; then
    export LIBGL_DRIVERS_PATH="$LIBGL_DRIVERS_PATH:$DRI"
else
    export LIBGL_DRIVERS_PATH="$DRI"
fi

# The mesa-egl shared objects (libEGL/libGLESv2/libgbm/libglapi/libdrm/libOSMesa)
# and the DRI driver's own deps. The binary is linked -Wl,-rpath,/lib, so /lib is
# already searched; adding the release-package lib dir and the system farm makes
# the launcher work under `make release` too.
LIBS="/Apps/mesa-egl/Current/lib:/System/Index/lib:/lib:/usr/lib"
if [ -n "${LD_LIBRARY_PATH:-}" ]; then
    export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$LIBS"
else
    export LD_LIBRARY_PATH="$LIBS"
fi

# The actual binary. In the bundle it is Contents/AOS/agreitis (a symlink to
# /bin/agreitis); on PATH via /bin/agreitis for a plain `agrun` from a shell.
if [ -x "$HERE/agreitis" ]; then
    exec "$HERE/agreitis" "$@"
fi
exec /bin/agreitis "$@"
