#!/bin/sh
# AppRun: PL:A Recomp from the AppImage. The runtime keeps everything it writes in its data folder (runtime/paths.c):
# $LARECOMP_DATA_ROOT, else $XDG_DATA_HOME/plarecomp, else ~/.local/share/plarecomp. It reads plugins/ and mods/ only
# from there, so the ones shipped here are copied in first - each switched off until turned on in the Mods page
# (larecomp.ini is never touched here). The bundled libraries are found by RPATH, not LD_LIBRARY_PATH, so the
# programs the runtime starts (the generator's Python, zig, a browser) see the system's libraries as usual.
HERE=$(dirname "$(readlink -f "$0")")

if [ -n "$LARECOMP_DATA_ROOT" ]; then D=$LARECOMP_DATA_ROOT
else
    case "$XDG_DATA_HOME" in /*) D=$XDG_DATA_HOME/plarecomp ;; *) D=${HOME:-/tmp}/.local/share/plarecomp ;; esac
fi
if mkdir -p "$D/plugins" "$D/mods" "$D/lua-examples" 2>/dev/null; then
    # plugins: copied when missing or different (a newer AppImage brings newer plugins)
    for f in "$HERE"/plugins/*; do
        [ -f "$f" ] || continue
        t=$D/plugins/$(basename "$f")
        cmp -s "$f" "$t" 2>/dev/null || { cp -f "$f" "$t.new" && chmod 755 "$t.new" && mv -f "$t.new" "$t"; }
    done
    # mods: copied only when not there (a mod builds files into its own folder: Lush Foliage's trees)
    for m in "$HERE"/mods/*/; do
        [ -d "$m" ] || continue
        n=$(basename "$m")
        [ -e "$D/mods/$n" ] || cp -R "$m" "$D/mods/$n"
    done
    # the bundled cheats: copied once (cheats/.bundled marks it) and only where the player has no file of their own - an
    # import is never replaced, and cheats the player deleted don't come back
    mkdir -p "$D/cheats" 2>/dev/null
    if [ ! -e "$D/cheats/.bundled" ]; then
        for f in "$HERE"/cheats/*; do
            [ -f "$f" ] && [ ! -e "$D/cheats/$(basename "$f")" ] && cp "$f" "$D/cheats/"
        done
        : > "$D/cheats/.bundled"
    fi
    for f in "$HERE"/lua-examples/*; do
        [ -f "$f" ] && [ ! -e "$D/lua-examples/$(basename "$f")" ] && cp "$f" "$D/lua-examples/"
    done
fi

# The runtime is told the same data folder: left to itself it would take an extracted/ in the folder it was started
# from (a source checkout's layout) as its data folder - a player's own extracted dump, say.
export LARECOMP_DATA_ROOT="$D"
unset PYTHONHOME PYTHONPATH  # (the bundled Python, which builds the game code, uses only its own modules)
echo "PL:A Recomp: data and log in $D (lasrecomp.log)" >&2
exec "$HERE/lasrecomp" "$@"
