#!/usr/bin/env bash
set -euo pipefail

APPDIR="${APPDIR:-$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)}"
RUNTIME="$APPDIR/usr/lib/localbooru/native-svp"

prepend_path() {
  local name="$1" value="$2" current="${!1:-}"
  if [[ -n "$current" ]]; then
    export "$name=$value:$current"
  else
    export "$name=$value"
  fi
}

# Preserve whether the caller supplied the variable, including an empty value.
if [[ ${LD_LIBRARY_PATH+x} ]]; then
  export DMC_APPIMAGE_ORIGINAL_LD_LIBRARY_PATH="$LD_LIBRARY_PATH"
  export DMC_APPIMAGE_LD_LIBRARY_PATH_STATE=set
else
  unset DMC_APPIMAGE_ORIGINAL_LD_LIBRARY_PATH
  export DMC_APPIMAGE_LD_LIBRARY_PATH_STATE=unset
fi

prepend_path LD_LIBRARY_PATH "$RUNTIME/lib"
prepend_path LD_LIBRARY_PATH "$RUNTIME/python-home/lib"
prepend_path PYTHONPATH "$RUNTIME/python-home/lib/python3.12/site-packages"
prepend_path GST_PLUGIN_PATH "$RUNTIME/gstreamer"
export PYTHONHOME="$RUNTIME/python-home"
export LOCALBOORU_GSTREAMER_SVP_DIR="$RUNTIME/gstreamer"
export LOCALBOORU_WEB_PROCESS_PATH="$RUNTIME/bin/mpv"
export WEBKIT_INJECTED_BUNDLE_PATH="$RUNTIME/injected-bundle"
export LOCALBOORU_WEBKIT_PROCESS_DIR="$RUNTIME/bin"
# Compatibility with the older helper locator; both select this bundled cohort.
export LOCALBOORU_WEBKIT_EXEC_DIR="$RUNTIME/bin"
export LOCALBOORU_ENABLE_NATIVE_SVP="${LOCALBOORU_ENABLE_NATIVE_SVP:-1}"

exec "$APPDIR/AppRun.tauri" "$@"
