#!/bin/sh
# ASAPDF launcher — installed by the asapdf .deb at /opt/asapdf/asapdf-launcher.
#
# The .deb is a THIN delivery + desktop-integration shim: the artifact that
# actually RUNS (and self-updates in place, exactly as the raw-AppImage install
# always has) is a per-user copy of the AppImage. This script seeds/refreshes
# that copy from the root-owned seed the package ships, then execs it.
#
#   Seed (root-owned, replaced only by dpkg):   /opt/asapdf/ASAPDF.AppImage
#   Live app (user-writable, self-updating):    ~/.local/share/asapdf/ASAPDF.AppImage
#
# Because we exec the per-user AppImage file itself, the AppImage runtime sets
# $APPIMAGE to that user-writable path — so the in-app updater's
# apply-and-restart (AppImage self-replace) keeps working unchanged, and
# ASAPDF's own PDF-handler registration (LinuxDesktopIntegration.cs) sees the
# deb-seeded path and skips writing a duplicate ~/.local/share/applications
# entry (it stays authoritative only for raw-AppImage-only users).
#
# Seeding rules (never fight the in-app updater):
#   * no user copy yet                                  -> seed it
#   * .deb upgraded to a seed NEWER than the last seed  -> re-seed
#   * otherwise                                         -> leave the user copy
#     alone (the in-app updater may have moved it ahead of the seed; it wins)
set -eu

SEED_DIR="/opt/asapdf"
SEED="$SEED_DIR/ASAPDF.AppImage"
SEED_VERSION="$(cat "$SEED_DIR/seed-version" 2>/dev/null || echo 0)"

DATA_HOME="${XDG_DATA_HOME:-$HOME/.local/share}"
APP_DIR="$DATA_HOME/asapdf"
APP="$APP_DIR/ASAPDF.AppImage"
MARKER="$APP_DIR/.seeded-version"

seed_app() {
    mkdir -p "$APP_DIR"
    # Copy to a temp name, then move into place: a crash mid-copy can never
    # leave a half-written AppImage at the exec path, and we never truncate a
    # file another process is currently running (mv swaps the inode).
    tmp="$APP_DIR/.ASAPDF.AppImage.seed.$$"
    cp "$SEED" "$tmp"
    chmod 755 "$tmp"
    mv -f "$tmp" "$APP"
    printf '%s\n' "$SEED_VERSION" > "$MARKER"
}

if [ ! -x "$APP" ]; then
    seed_app
else
    LAST_SEEDED="$(cat "$MARKER" 2>/dev/null || echo 0)"
    # dpkg --compare-versions exits non-zero for "not greater" AND for parse
    # errors; both conflate to "do not re-seed", which is the safe default
    # (never clobber a copy the in-app updater may have advanced).
    if dpkg --compare-versions "$SEED_VERSION" gt "$LAST_SEEDED" 2>/dev/null; then
        seed_app
    fi
fi

# The AppImage self-mounts via libfuse.so.2 (FUSE2). A fresh Mint 22 / Ubuntu
# 24.04 ships WITHOUT libfuse2 (FUSE2 is deprecated), and a double-click install
# through Mint's Captain may not resolve our "libfuse2t64 | libfuse2" dep — so a
# bare "exec $APP" aborts with 'dlopen(): error loading libfuse.so.2 / AppImages
# require FUSE to run' and the desktop shows "There was an error launching the
# application" (the #495 symptom). Probe for a usable FUSE2 (both the kernel
# device and the userspace lib) and, when it is absent, fall back to the AppImage
# runtime's FUSE-less extract-and-run.
#
# Use the APPIMAGE_EXTRACT_AND_RUN env var, NOT the --appimage-extract-and-run
# CLI flag: it leaves argv untouched so app args (%f for the PDF association)
# pass through unchanged, it survives the in-app updater's apply-and-restart
# relaunch on a FUSE-less box, and it is honored by AppImage runtimes that
# predate the flag. Either way the runtime still exports $APPIMAGE to the real
# AppImage path, so the in-app self-update is unaffected.
have_fuse2() {
    [ -e /dev/fuse ] || return 1
    if command -v ldconfig >/dev/null 2>&1 && ldconfig -p 2>/dev/null | grep -q 'libfuse\.so\.2'; then
        return 0
    fi
    for p in /usr/lib/x86_64-linux-gnu/libfuse.so.2 /lib/x86_64-linux-gnu/libfuse.so.2 \
             /usr/lib/libfuse.so.2 /usr/lib64/libfuse.so.2; do
        [ -e "$p" ] && return 0
    done
    return 1
}
have_fuse2 || export APPIMAGE_EXTRACT_AND_RUN=1

exec "$APP" "$@"
