#!/bin/bash
# Custom AppRun for the aMule AppImage.
#
# Dispatches to one of the bundled binaries (amule / amuled / amulegui /
# amulecmd / amuleweb / amuleapi / ed2k) based on the name the AppImage was invoked
# as. The AppImage runtime sets $ARGV0 to the path used to launch the
# image; basename of that gives us the symlink name. Standard idiom —
# same pattern Krita and GIMP use to ship multiple tools in one bundle.
#
# Usage:
#   ./aMule-x.y.z-Linux-x64.AppImage         # runs amule (monolithic GUI)
#   ln -s aMule.AppImage amuled
#   ./amuled                              # runs the daemon
#   ln -s aMule.AppImage amulecmd
#   ./amulecmd ...                        # runs the EC client

set -e

HERE="$(dirname "$(readlink -f "${0}")")"

# Source plugin env hooks (linuxdeploy-plugin-gtk drops its env setup
# in apprun-hooks/linuxdeploy-plugin-gtk.sh — GTK_PATH, GIO_MODULE_DIR,
# GDK_PIXBUF_MODULE_FILE, XDG_DATA_DIRS, FONTCONFIG_PATH, etc.).
if [ -d "${HERE}/apprun-hooks" ]; then
    for hook in "${HERE}/apprun-hooks/"*.sh; do
        [ -f "${hook}" ] && . "${hook}"
    done
fi

# The AppImage runtime already exports APPDIR when it mounts the image, but set
# it explicitly so detection also works when AppRun is invoked from an extracted
# tree (--appimage-extract). aMule reads APPDIR both for desktop integration and
# to strip these bundle paths back out when it launches external programs like
# the preview player (#334).
export APPDIR="${HERE}"
export PATH="${HERE}/usr/bin:${PATH}"
export LD_LIBRARY_PATH="${HERE}/usr/lib:${LD_LIBRARY_PATH:-}"

# Keep the host's GLib/GTK plugins out of the bundle. Both of these are set
# after the plugin hooks above deliberately: linuxdeploy-plugin-gtk sets
# neither, so nothing here is being overridden.
#
# GIO_MODULE_DIR: unset, GLib scans the path it was compiled with, which is
# the host's /usr/lib/<triplet>/gio/modules. Those modules are built against
# the host's GLib and get resolved against the older one bundled here, so
# loading them fails --
#
#   /usr/lib/x86_64-linux-gnu/gvfs/libgvfscommon.so: undefined symbol:
#     g_task_set_static_name
#   Failed to load module: /usr/lib/x86_64-linux-gnu/gio/modules/libgvfsdbus.so
#
# (g_task_set_static_name arrived in GLib 2.76.) We bundle no GIO modules, so
# this points at a directory that does not exist, which is the intent: it
# stops the scan rather than redirecting it. aMule uses no GIO module -- the
# failure was only ever noise on stderr (issue #898).
export GIO_MODULE_DIR="${HERE}/usr/lib/gio/modules"

# GTK_MODULES comes from the host session, and the bundled GTK cannot load a
# host module any more than GIO can. Linux Mint exports xapp-gtk3-module,
# which produces `Gtk-Message: Failed to load module "xapp-gtk3-module"` on
# every start. These modules are desktop integration aMule does not use.
unset GTK_MODULES

# argv[0] dispatch. ARGV0 is set by the AppImage runtime; fall back to
# $0 for direct invocation (e.g. when extracted via --appimage-extract).
INVOCATION="${ARGV0:-$0}"
# Bash's own suffix removal, not basename: LD_LIBRARY_PATH already points into
# the bundle, so a host program started here loads our older libraries and can
# fail to resolve its own symbols. Ubuntu 26.04's basename wants LIBSYSTEMD_254,
# the bundled libsystemd stops at 252 (#1463). Keep every step below builtin.
NAME="${INVOCATION##*/}"
# Strip a trailing .AppImage so renaming aMule-2.4.0-Linux-x64.AppImage
# to amuled.AppImage still dispatches.
NAME="${NAME%.AppImage}"
# Tolerate either the release-asset spelling (-x64 / -arm64, #764) or
# the legacy uname spelling (-x86_64 / -aarch64) when stripping the
# arch tail from the invocation name.
NAME="${NAME%-x64}"
NAME="${NAME%-arm64}"
NAME="${NAME%-x86_64}"
NAME="${NAME%-aarch64}"
# Strip the Linux marker introduced by the #764 naming scheme so
# `aMule-3.0.0-Linux-x64.AppImage` collapses to `aMule` for dispatch.
NAME="${NAME%-Linux}"

case "${NAME}" in
    amuled|amulegui|amulecmd|amuleweb|amuleapi|ed2k|cas|wxcas|alc|alcc)
        BIN="${HERE}/usr/bin/${NAME}"
        ;;
    *)
        BIN="${HERE}/usr/bin/amule"
        ;;
esac

if [ ! -x "${BIN}" ]; then
    echo "AppRun: ${BIN} missing or not executable" >&2
    exit 127
fi

exec "${BIN}" "$@"
