#!/usr/bin/env bash
# Resilient launcher for the big-ddcgui GTK4/Adwaita application.
#
# The package ships as a wheel installed under
# /usr/lib/pythonX.Y/site-packages/big_ddcgui (the recommended, Nix-compatible
# layout). That path is tied to the Python minor version present at build time.
# If the system Python is later upgraded (e.g. 3.14 -> 3.15) WITHOUT rebuilding
# this package, `python3 -m big_ddcgui` would fail: the new interpreter no
# longer searches the old site-packages directory.
#
# Strategy:
#   1. Fast path - if the current interpreter can import the module, run it.
#   2. Fallback  - locate the installed package tree under any
#      pythonX.Y/site-packages and run it with the current interpreter via
#      PYTHONPATH. The code is pure Python, so it runs unchanged across minor
#      versions; the GI bindings come from the system `python-gobject`, which
#      pacman rebuilds for the new interpreter.
#
# Net effect: a Python upgrade keeps the app working without repackaging.

set -euo pipefail
module="big_ddcgui"

# 1. Fast path: importable under the current python3.
if python3 -c "import ${module}" 2>/dev/null; then
	exec python3 -m "${module}" "$@"
fi

# 2. Fallback: find the installed tree and inject it into PYTHONPATH.
for dir in /usr/lib/python3.*/site-packages \
	/usr/lib/python3/dist-packages \
	/usr/lib64/python3.*/site-packages; do
	if [ -d "${dir}/${module}" ]; then
		exec env PYTHONPATH="${dir}${PYTHONPATH:+:${PYTHONPATH}}" \
			python3 -m "${module}" "$@"
	fi
done

printf 'big-ddcgui: cannot locate the %s package in any site-packages\n' "${module}" >&2
exit 1
