#!/bin/sh
set -eu

APPDIR="$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)"
export APPDIR
export PATH="$APPDIR/usr/bin:$PATH"
export PYTHONHOME="$APPDIR/usr"

PYTHON_DIR="$(find "$APPDIR/usr/lib" -maxdepth 1 -type d -name 'python3.*' | sort | head -n 1)"
PYTHON_VERSION_DIR="$(basename "$PYTHON_DIR")"
PYTHON_SITE="$APPDIR/usr/lib/$PYTHON_VERSION_DIR/site-packages"
PYTHON_DIST="$APPDIR/usr/lib/$PYTHON_VERSION_DIR/dist-packages"

export PYTHONPATH="$PYTHON_SITE:$PYTHON_DIST:$APPDIR/usr/share/meshy${PYTHONPATH:+:$PYTHONPATH}"
export LD_LIBRARY_PATH="$APPDIR/usr/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}"
export GI_TYPELIB_PATH="$APPDIR/usr/lib/girepository-1.0${GI_TYPELIB_PATH:+:$GI_TYPELIB_PATH}"
export GSETTINGS_SCHEMA_DIR="$APPDIR/usr/share/glib-2.0/schemas"
export XDG_DATA_DIRS="$APPDIR/usr/share:${XDG_DATA_DIRS:-/usr/local/share:/usr/share}"

if [ -d "$APPDIR/usr/lib/gstreamer-1.0" ]; then
    export GST_PLUGIN_SYSTEM_PATH="$APPDIR/usr/lib/gstreamer-1.0${GST_PLUGIN_SYSTEM_PATH:+:$GST_PLUGIN_SYSTEM_PATH}"
fi

if [ -f "$APPDIR/usr/lib/gdk-pixbuf-2.0/2.10.0/loaders.cache" ]; then
    export GDK_PIXBUF_MODULEDIR="$APPDIR/usr/lib/gdk-pixbuf-2.0/2.10.0/loaders"
    export GDK_PIXBUF_MODULE_FILE="$APPDIR/usr/lib/gdk-pixbuf-2.0/2.10.0/loaders.cache"
fi

exec "$APPDIR/usr/bin/python3" "$APPDIR/usr/bin/meshy" "$@"
