#!/bin/sh
set -eu
appdir=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd -P)
unset APPIMAGE APPIMAGE_EXTRACT_AND_RUN
export APPDIR="$appdir"
# The payload uses host glibc and graphics drivers. Its own RUNPATH resolves
# packaged client libraries without exporting LD_LIBRARY_PATH to CLI children.
if [ ! -e /lib64/ld-linux-x86-64.so.2 ]; then
    echo "Maple needs an x86_64 Linux system with glibc (/lib64/ld-linux-x86-64.so.2). On NixOS, use the Maple flake package or nix-ld." >&2
    exit 127
fi
# ALSA's compiled defaults use the host configuration. Find its distro-specific
# plugin directory while preserving explicit overrides, including empty values.
if [ -z "${ALSA_PLUGIN_DIR+x}" ]; then
    for plugins in /usr/lib/x86_64-linux-gnu/alsa-lib /usr/lib64/alsa-lib /usr/lib/alsa-lib; do
        if [ -d "$plugins" ]; then
            export ALSA_PLUGIN_DIR="$plugins"
            break
        fi
    done
fi
exec "$appdir/usr/bin/maple-agent" "$@"
