#!/bin/sh
# The HeyListen AppImage: the builder window (builder/linux/hookshot_builder.py) with everything it builds the game with -
# Python and the tools, clang and lld, a glibc 2.35 sysroot, FFmpeg, SDL2 and the Vulkan back end, built already.
#   HeyListen.AppImage                 the builder window (Play is in the window, once a game is built)
#   HeyListen.AppImage --play          start the game the builder saved, without opening the window
#   HeyListen.AppImage --play <folder> start the game in that folder
#   HeyListen.AppImage <folder>        the same
#   HeyListen.AppImage --cli <args>    nx2/make_game.py itself, e.g. --cli --nup game.nup --out ~/Games/HeyListen
#   HeyListen.AppImage --launch <dir> the game folder's launcher: Play, and Update when a newer release is out
#   HeyListen.AppImage --update <dir> download the new release (only the parts that changed) and build that folder again
HERE="$(dirname "$(readlink -f "$0")")"
LIB="$HERE/usr/lib/hookshot"
export HOOKSHOT_TOOLS="$HERE/usr/share/hookshot"
export EMSDK="$LIB/emsdk"
export HOOKSHOT_SYSROOT="$LIB/sysroot"
export HOOKSHOT_FFMPEG="$LIB/ffmpeg"
export HOOKSHOT_VK_PREBUILT="$LIB/hookshot_vk.so"
export HOOKSHOT_SDL2="$LIB/libSDL2-2.0.so.0"
# (written into the game folder's hookshot_version.txt, so its launcher finds this builder)
if [ -n "${APPIMAGE:-}" ]; then
    export HOOKSHOT_BUILDER_EXE="$APPIMAGE"
else
    export HOOKSHOT_BUILDER_EXE="$HERE/AppRun"
fi
PY="$HERE/usr/python/bin/python3"
if [ "${1:-}" = "--cli" ]; then
    shift
    exec "$PY" -I "$HOOKSHOT_TOOLS/nx2/make_game.py" "$@"
fi
# Play is the same AppImage: the start script drops this image's libraries, then runs the built game.
if [ "${1:-}" = "--play" ]; then
    shift
    exec "$HOOKSHOT_TOOLS/builder/linux/hookshot-launch" "$@"
fi
if [ -n "${1:-}" ] && [ -d "$1" ]; then
    exec "$HOOKSHOT_TOOLS/builder/linux/hookshot-launch" "$1"
fi
exec "$PY" -I "$HOOKSHOT_TOOLS/builder/linux/hookshot_builder.py" "$@"
