#!/bin/sh
# AppRun for the SSH Pilot AppImage.
#
# The bundled binaries find their libraries through an $ORIGIN RPATH written by
# packaging/appimage/build-appimage.sh, so this script deliberately does NOT
# export LD_LIBRARY_PATH, PYTHONHOME or PYTHONPATH. That matters more here than
# in most apps: sshpilot spawns the user's shell inside a VTE terminal, plus
# ssh/scp/sshpass, and a leaked library or Python path would break those host
# binaries. The bundled interpreter finds the bundled modules on its own,
# because it sits in the usual $prefix/bin + $prefix/lib layout under usr/.
#
# What is left below is the data GTK can only find through the environment.

set -eu

SELF=$(readlink -f "$0")
APPDIR=${APPDIR:-$(dirname "$SELF")}
export APPDIR

export XDG_DATA_DIRS="$APPDIR/usr/share:${XDG_DATA_DIRS:-/usr/local/share:/usr/share}"
export GI_TYPELIB_PATH="$APPDIR/usr/lib/girepository-1.0"
# Additive in GLib: host schemas stay reachable through XDG_DATA_DIRS.
export GSETTINGS_SCHEMA_DIR="$APPDIR/usr/share/glib-2.0/schemas"
export GIO_MODULE_DIR="$APPDIR/usr/lib/gio/modules"

# WebKitGTK renders out of process and has the helper paths compiled in, which
# point at the build host's /usr. Without these two a WebView just never
# finishes loading: no PyXterm.js terminal and no Host Info tab.
export WEBKIT_EXEC_PATH="$APPDIR/usr/lib/webkitgtk-6.0"
export WEBKIT_INJECTED_BUNDLE_PATH="$APPDIR/usr/lib/webkitgtk-6.0/injected-bundle"

# xterm.js assets: the app prefers the host's /usr/share/javascript/xterm when
# it exists, which for a self-contained bundle is the wrong copy. The env
# override the app already supports points it back at our own.
for xterm_dir in "$APPDIR"/usr/lib/python3*/*-packages/sshpilot/vendor/pyxtermjs/xterm; do
    if [ -f "$xterm_dir/xterm.js" ]; then
        export PYXTERMJS_ASSETS_DIR="$xterm_dir"
        break
    fi
done

# The loaders.cache has to name the loaders by absolute path, which is only
# known once the AppImage is mounted. Materialise it per user rather than
# writing into the read-only mount; a fixed path keeps this from piling up.
pixbuf_template="$APPDIR/usr/lib/gdk-pixbuf-2.0/2.10.0/loaders.cache.in"
if [ -f "$pixbuf_template" ]; then
    pixbuf_cache_dir="${XDG_CACHE_HOME:-${HOME:-/tmp}/.cache}/sshpilot/appimage"
    if mkdir -p "$pixbuf_cache_dir" 2>/dev/null &&
       sed "s|@APPDIR@|$APPDIR|g" "$pixbuf_template" \
           > "$pixbuf_cache_dir/loaders.cache" 2>/dev/null; then
        export GDK_PIXBUF_MODULEDIR="$APPDIR/usr/lib/gdk-pixbuf-2.0/2.10.0/loaders"
        export GDK_PIXBUF_MODULE_FILE="$pixbuf_cache_dir/loaders.cache"
    fi
fi

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