#!/bin/sh
set -eu

if [ -z "${APPDIR:-}" ]; then
    echo "TombaRecomp: APPDIR is not set; launch this through the AppImage runtime." >&2
    exit 1
fi

if [ -n "${TOMBA_RECOMP_DATA_DIR:-}" ]; then
    data_dir=$TOMBA_RECOMP_DATA_DIR
else
    if [ -n "${XDG_DATA_HOME:-}" ]; then
        data_home=$XDG_DATA_HOME
    elif [ -n "${HOME:-}" ]; then
        data_home=$HOME/.local/share
    else
        echo "TombaRecomp: HOME is not set; set TOMBA_RECOMP_DATA_DIR explicitly." >&2
        exit 1
    fi
    data_dir=$data_home/TombaRecomp
fi

payload=$APPDIR/usr/share/tombarecomp
runtime=$APPDIR/usr/bin/Tomba__Recompiled

if [ ! -x "$runtime" ] || [ ! -f "$payload/game.toml" ]; then
    echo "TombaRecomp: AppImage payload is incomplete." >&2
    exit 1
fi

mkdir -p "$data_dir" "$data_dir/saves" "$data_dir/cache" "$data_dir/mods"

# These are release-owned resources. Refreshing them on each launch lets an
# AppImage update repair or update its bundled catalog without touching user
# settings, memory cards, installed third-party mods, or generated cache data.
for tree in assets bios licenses docs; do
    if [ -d "$payload/$tree" ]; then
        mkdir -p "$data_dir/$tree"
        cp -a "$payload/$tree/." "$data_dir/$tree/"
        chmod -R u+rwX "$data_dir/$tree"
    fi
done
# Bundled overlay cache: seed WITHOUT clobbering. The running game writes this
# directory too, so a plain copy would wipe shards the player's own sessions
# built, every launch.
if [ -d "$payload/cache" ]; then
    mkdir -p "$data_dir/cache"
    cp -a -n "$payload/cache/." "$data_dir/cache/" 2>/dev/null || true
    chmod -R u+rwX "$data_dir/cache"
fi

if [ -d "$payload/overlay_toolchain" ]; then
    if [ -L "$data_dir/overlay_toolchain" ] || [ ! -e "$data_dir/overlay_toolchain" ]; then
        ln -sfn "$payload/overlay_toolchain" "$data_dir/overlay_toolchain"
    fi
fi

if [ -d "$payload/mods" ]; then
    cp -a "$payload/mods/." "$data_dir/mods/"
    chmod -R u+rwX "$data_dir/mods"
fi

for file in game.toml game_options.toml LICENSE README.md \
            START_HERE.txt AOT_CACHE_AUDIT.json; do
    if [ -f "$payload/$file" ]; then
        cp -f "$payload/$file" "$data_dir/$file"
        chmod u+rw "$data_dir/$file"
    fi
done

# input.ini and keybinds.ini are user-owned after first launch.
if [ ! -f "$data_dir/input.ini" ] && [ -f "$payload/input.ini" ]; then
    cp "$payload/input.ini" "$data_dir/input.ini"
    chmod u+rw "$data_dir/input.ini"
fi

printf '%s\n' "v0.13.0-alpha" > "$data_dir/.appimage-layout-version"

if [ "${TOMBA_RECOMP_SEED_ONLY:-0}" = "1" ]; then
    printf '%s\n' "$data_dir"
    exit 0
fi

# psxrecomp deliberately anchors settings, assets, saves, cache, and mods to
# argv[0]. Invoke through a writable-state symlink and hide APPIMAGE from the
# child so it never resolves those paths inside the read-only squashfs mount.
launcher=$data_dir/.TombaRecomp-runtime
ln -sfn "$runtime" "$launcher"
export TOMBA_RECOMP_APPIMAGE_PATH=${APPIMAGE:-}
unset APPIMAGE

export PATH="$APPDIR/usr/bin${PATH:+:$PATH}"
export XDG_DATA_DIRS="$APPDIR/usr/share${XDG_DATA_DIRS:+:$XDG_DATA_DIRS}"

# Deliberately NOT exported: LD_LIBRARY_PATH=$APPDIR/usr/lib.
#
# The binary carries RUNPATH $ORIGIN/../lib and finds the bundle on its own.
# A global LD_LIBRARY_PATH is inherited by every CHILD process -- including the
# host zenity/kdialog the launcher spawns for Browse -- which then loads this
# bundle glib/pcre2 against the host GTK stack and dies before drawing. The
# launcher read that failure as a plain cancel, so the button just did nothing.
# See recomp-ui launcher_files.c (sanitised spawn environment) and
# tools/package_appimage.sh (prune of libraries only reachable this way).
#
# Record the host value instead, so the launcher can hand a correct
# LD_LIBRARY_PATH to anything it spawns even if a wrapper set one for us.
export RECOMP_HOST_LD_LIBRARY_PATH="${LD_LIBRARY_PATH:-}"

cd "$data_dir"
exec "$launcher" --game "$data_dir/game.toml" "$@"
