#!/usr/bin/env bash
# The AppImage's entry point: start the bundled orx as the desktop app (see
# src/commands/app.rs). Arguments are ignored; the CLI is `orx`, not this.
set -e

export APPDIR="${APPDIR:-"$(dirname "$(realpath "$0")")"}"

# The session's values of what this script and the hook below override, as
# `=<value>` or `-` for unset, so orx can hand them back to the host programs it
# opens (shell_env::host_gui_env; keep the two lists in step).
for var in GDK_BACKEND GDK_PIXBUF_MODULE_FILE GI_TYPELIB_PATH GIO_EXTRA_MODULES \
  GIO_MODULE_DIR GSETTINGS_SCHEMA_DIR GTK_DATA_PREFIX GTK_EXE_PREFIX GTK_IM_MODULE_FILE GTK_PATH \
  GTK_THEME XDG_DATA_DIRS; do
  if [ -n "${!var+set}" ]; then
    export "ORX_HOST_$var==${!var}"
  else
    export "ORX_HOST_$var=-"
  fi
done

# linuxdeploy-plugin-gtk's hook points GTK at the bundled modules and schemas.
for hook in "$APPDIR"/apprun-hooks/*.sh; do
  if [ -e "$hook" ]; then
    # shellcheck disable=SC1090
    . "$hook"
  fi
done
# Only the bundled GIO modules (TLS): the host's need a newer GLib than the image's.
export GIO_MODULE_DIR="$APPDIR/usr/lib/gio/modules"
unset GIO_EXTRA_MODULES

# scripts/build-linux-appimage.sh rewrote libwebkit2gtk's /usr paths to ././,
# so WebKit finds its bundled helper processes relative to this directory.
cd "$APPDIR/usr"
exec "$APPDIR/usr/bin/orx" app
