#!/bin/sh
# AoC Recomp launcher (Linux). Runs aoc-launcher.bin; the libraries its SDL3 needs that this system lacks come from
# lib-fallback/ (copies from the Steam Runtime), only those.
here="$(cd "$(dirname "$0")" && pwd)"
saved="${LD_LIBRARY_PATH-}"
fb_bin="$here/aoc-launcher.bin"; fb_dir="$here/lib-fallback"
fb_add=""; fb_used=""
if command -v ldd >/dev/null; then
    for fb_round in 1 2 3 4 5 6 7 8; do
        fb_miss=$(LD_LIBRARY_PATH="$fb_add${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}" ldd "$fb_bin" 2>/dev/null)
        fb_miss=$(echo "$fb_miss" | awk '/not found/ { print $1 }')
        fb_new=""
        for fb_lib in $fb_miss; do
            [ -f "$fb_dir/$fb_lib/$fb_lib" ] || continue
            case ":$fb_add:" in *":$fb_dir/$fb_lib:"*) ;; *) fb_new="$fb_new:$fb_dir/$fb_lib" ;; esac
        done
        [ -n "$fb_new" ] || break
        fb_add="${fb_add}${fb_new}"; fb_add="${fb_add#:}"
    done
fi
if [ -n "$fb_add" ]; then
    fb_used=1
    export LD_LIBRARY_PATH="$fb_add${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}"
fi
[ -n "$fb_used" ] && export AOC_LD_SAVED="$saved" AOC_LD_FALLBACK=1
exec "$here/aoc-launcher.bin" "$@"
