#!/bin/sh
# EmeraldRecomp AppImage entry point.
#
# The runtime keeps its state next to its executable (config.ini, keybinds.ini,
# rom.cfg / bios.cfg, saves, mod selections) - the Windows zip is a portable
# folder. An AppImage is mounted read-only, so this launcher gives it a
# writable per-user home and runs the game from there:
#
#   ${EMERALDRECOMP_HOME:-${XDG_DATA_HOME:-~/.local/share}/EmeraldRecomp}
#
# The (read-only) executable and launcher assets are linked in on every start
# (the AppImage mount point changes per run); the mod catalog is copied once
# per package version so the player's mod choices stay writable. Your ROM and
# BIOS are never bundled: the launcher asks for them on first run.
set -eu
HERE="$(dirname "$(readlink -f "$0")")"
DATA="${EMERALDRECOMP_HOME:-${XDG_DATA_HOME:-$HOME/.local/share}/EmeraldRecomp}"
SHARE="$HERE/usr/share/emeraldrecomp"

mkdir -p "$DATA/mods/packages"
mkdir -p "$DATA/variants/emerald"
cp -n "$SHARE/runtime-game.toml" "$DATA/variants/emerald/game.toml" 2>/dev/null || true
ln -sfn "$HERE/usr/bin/EmeraldRecomp" "$DATA/EmeraldRecomp"
ln -sfn "$HERE/usr/bin/assets" "$DATA/assets"
# New package versions arrive as new <id>/<version> directories; never
# overwrite anything already in the player's catalog.
cp -Rn "$SHARE/mods/packages/." "$DATA/mods/packages/" 2>/dev/null || true
cp -n "$SHARE/LICENSE" "$SHARE/README.md" "$DATA/" 2>/dev/null || true

export RECOMP_HOST_LD_LIBRARY_PATH="${LD_LIBRARY_PATH-}"
export LD_LIBRARY_PATH="$HERE/usr/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}"
cd "$DATA"
exec "$DATA/EmeraldRecomp" "$@"
