#!/usr/bin/env bash
set -euo pipefail
HERE="$(dirname "$(readlink -f "${0}")")"
# Ensure application can find its data files inside the AppDir
export XDG_DATA_DIRS="$HERE/usr/share:${XDG_DATA_DIRS:-}"
# Point GSettings to the bundled schema directory so preferences can be saved/loaded
export GSETTINGS_SCHEMA_DIR="$HERE/usr/share/glib-2.0/schemas:${GSETTINGS_SCHEMA_DIR:-}"
# Let the runtime know where internal helper binaries live inside the AppDir
export PAPERBOY_LIBEXECDIR="$HERE/usr/libexec"
# Prepend bundled fallback libs so libxml2.so.16 from the AppDir is preferred.
# Keep $HERE/usr/lib in the search path for other bundled libs if present.
# Use a safe expansion under `set -u` so referencing an unset LD_LIBRARY_PATH
# doesn't trigger an unbound variable error inside the AppImage runtime.
if [ -n "${LD_LIBRARY_PATH-}" ]; then
  export LD_LIBRARY_PATH="$HERE/opt/fallback-libs:$LD_LIBRARY_PATH:$HERE/usr/lib"
else
  export LD_LIBRARY_PATH="$HERE/opt/fallback-libs:$HERE/usr/lib"
fi
exec "$HERE/usr/bin/paperboy" "$@"
