#!/bin/sh
# SPDX-FileCopyrightText: 2026 Adrian Knagg-Baugh
# SPDX-License-Identifier: GPL-3.0-or-later
# Launcher installed by install.sh. Uses, in order:
#   1. the virtual environment install.sh created, if there is one: the installer
#      makes it only when the system Python can't be used (or with --venv), and it
#      still uses the system PyQt6 when that is new enough;
#   2. the system python3, if it has PyQt6 6.5+ (with QtDBus) and an ffmpeg is
#      available: built against the system Qt, so the KDE platform theme,
#      Breeze style and native KDE file dialogs are used;
#   3. uv, fetching everything from PyPI (for running an uninstalled checkout).
HERE=$(dirname "$(readlink -f "$0")")
ROOT=$(dirname "$HERE")
VENV=${XDG_DATA_HOME:-$HOME/.local/share}/musicdropper/venv
export PYTHONPATH="$ROOT${PYTHONPATH:+:$PYTHONPATH}"
if [ -x "$VENV/bin/python" ]; then
    exec "$VENV/bin/python" -m musicdropper "$@"
elif python3 -c 'import sys, PyQt6.QtWidgets, PyQt6.QtDBus
from PyQt6.QtCore import PYQT_VERSION
sys.exit(sys.version_info < (3, 10) or PYQT_VERSION < 0x060500)' 2>/dev/null &&
   { command -v ffmpeg >/dev/null || python3 -c 'import imageio_ffmpeg' 2>/dev/null; }; then
    exec python3 -m musicdropper "$@"
elif command -v uv >/dev/null 2>&1; then
    exec uv run --quiet --script "$ROOT/run.py" "$@"
else
    msg="MusicDropper's Python packages are missing. Run install.sh from $ROOT."
    command -v kdialog >/dev/null && kdialog --error "$msg" || echo "$msg" >&2
    exit 1
fi
