#!/bin/sh
# Takedawn's AppImage entry: the game, with the libraries the image carries
# (SDL and what else the build machine's runtime had that a system may lack)
# ahead of the system's. The Vulkan loader, the graphics drivers, glibc and
# libstdc++ are always the system's.
here=$(dirname "$(readlink -f "$0")")
# What the person had set is kept under the name the AppImage tools use for it
# (the setup window gives its children the image's usr/lib plus this).
if [ -n "${LD_LIBRARY_PATH:-}" ]; then
    APPIMAGE_ORIGINAL_LD_LIBRARY_PATH="$LD_LIBRARY_PATH"
    export APPIMAGE_ORIGINAL_LD_LIBRARY_PATH
else
    unset APPIMAGE_ORIGINAL_LD_LIBRARY_PATH
fi
LD_LIBRARY_PATH="$here/usr/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}"
# libxkbcommon and libxkbcommon-x11 (the setup window's xcb plugin) are the
# system's; on a system that lacks one of them the image's pair is used
# (usr/lib/xkb-fallback, the two always together).
has_lib() {  # <soname>: the system's loader finds a 64-bit copy
    for c in /sbin/ldconfig /usr/sbin/ldconfig /bin/ldconfig /usr/bin/ldconfig; do
        if [ -x "$c" ]; then
            "$c" -p 2>/dev/null | grep -q "^[[:space:]]*$1 (libc6,x86-64)" && return 0
            break
        fi
    done
    for d in /usr/lib/x86_64-linux-gnu /lib/x86_64-linux-gnu /usr/lib64 /lib64 /usr/lib /lib; do
        [ -e "$d/$1" ] && return 0
    done
    return 1
}
if [ -d "$here/usr/lib/xkb-fallback" ] && { ! has_lib libxkbcommon.so.0 || ! has_lib libxkbcommon-x11.so.0; }; then
    LD_LIBRARY_PATH="$here/usr/lib:$here/usr/lib/xkb-fallback${LD_LIBRARY_PATH#"$here/usr/lib"}"
fi
export LD_LIBRARY_PATH
exec "$here/usr/bin/takedawn" "$@"
