#!/bin/sh
# AppRun — the AppImage's entry point.
#
# Two jobs, and the second one is the whole reason an AppImage is the
# Linux format here rather than a bare binary in a tarball.
#
# 1. Put the bundled libraries first on the loader path.  GLFW is the one
#    that matters: it is not installed on a stock Ubuntu desktop, and
#    "install libglfw3 first" is not a way to ship a program to somebody
#    who wants to watch an engine run.  The image reopens its foreign
#    libraries by soname at startup (see scripts/build-binary.lisp), so
#    putting our copy in front of the loader's search is by itself enough
#    to make it the one that gets used.
#
# 2. Leave the *driver* alone.  libGL and libEGL are emphatically not
#    bundled and must not be: they belong to the user's graphics driver,
#    they have to match the kernel module that is actually loaded, and a
#    bundled Mesa in front of an NVIDIA driver is precisely the libGL trap
#    the project has a file about (concept.md §5.8).  Appending rather
#    than prepending the system directories is not an option either — the
#    driver is found by the loader's own configuration, and we simply do
#    not interfere.
#
# The audio libraries are in the same category as the driver and for a
# related reason: libpulse-simple talks to a daemon that is already
# running on the user's machine, and shipping a client is a good way to
# meet a protocol version we were not built for.  They are looked up by
# absolute path at the moment a sink is wanted (§7.5), and their absence
# is silence rather than a failure.
#
# APPDIR is set by the AppImage runtime, but the AppDir can also be run
# directly (./AppDir/AppRun) during packaging tests, where it is not.

HERE="$(dirname "$(readlink -f "$0")")"
export APPDIR="${APPDIR:-$HERE}"
export LD_LIBRARY_PATH="$HERE/usr/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}"

exec "$HERE/usr/bin/cl-piston" "$@"
