#!/bin/sh
# Launcher for nilorea-client (installed by the nilorea-client package).
#
# The binary resolves assets/ config/ docs/ relative to its working directory
# and writes runtime state there, so it cannot run directly out of the
# read-only /usr/share payload. Set up a writable per-user data dir, seed the
# config defaults into it once, symlink the read-only trees, then exec.
SHARE="/usr/share/nilorea-client"
DATA="${GAME_DATA_DIR:-${XDG_DATA_HOME:-$HOME/.local/share}/nilorea-client}"
mkdir -p "$DATA" || exit 1
cd "$DATA" || exit 1
[ -e config ] || { cp -r "$SHARE/config" config && chmod -R u+w config; }
# The package ships the key world-readable (a .deb's /usr content always is);
# tighten it once it lands in the user's own data dir, where it is the file the
# server listener actually reads.
if [ -f config/certs/server.key ]; then chmod 600 config/certs/server.key; fi
[ -e assets ] || { [ -d "$SHARE/assets" ] && ln -s "$SHARE/assets" assets; }
[ -e docs ]   || { [ -d "$SHARE/docs" ]   && ln -s "$SHARE/docs"   docs; }
exec "/usr/lib/nilorea-client/nilorea-client" "$@"
