#!/bin/sh
# Airtime for Linux: the DOS game in the DOSBox-X in dosbox-x/ beside this
# script, with the settings in dosbox-x.conf. ./airtime from a terminal, or
# "Run as a program" in a file manager.
#
# If that DOSBox-X will not start here, a dosbox-x on your PATH or DOSBox-X
# from Flathub is used instead. DOSBOX_X=/path/to/dosbox-x ./airtime picks one.
here=$(cd "$(dirname "$0")" && pwd) || exit 1
cd "$here" || exit 1
conf="$here/dosbox-x.conf"

if [ -n "${DOSBOX_X:-}" ]; then
    exec "$DOSBOX_X" -conf "$conf" "$@"
fi
bundled="$here/dosbox-x/dosbox-x"
if [ -x "$bundled" ]; then
    # DOSBox-X answers -version without a window; a loader error means it cannot run here
    # it finds dosbox-x/lib itself (RPATH): what it starts gets the system's libraries
    probe=$("$bundled" -version 2>&1)
    case "$probe" in
        *DOSBox-X*) exec "$bundled" -conf "$conf" "$@" ;;
    esac
    echo "Airtime: the DOSBox-X that came with it will not start here:" >&2
    echo "$probe" | head -3 >&2
fi
if command -v dosbox-x >/dev/null 2>&1; then
    echo "Airtime: using $(command -v dosbox-x)" >&2
    exec dosbox-x -conf "$conf" "$@"
fi
if command -v flatpak >/dev/null 2>&1 && flatpak info com.dosbox_x.DOSBox-X >/dev/null 2>&1; then
    echo "Airtime: using DOSBox-X from Flathub" >&2
    exec flatpak run --filesystem="$here" --cwd="$here" com.dosbox_x.DOSBox-X -conf "$conf" "$@"
fi
echo "Airtime: no DOSBox-X to run it in. Install your distro's dosbox-x, or" >&2
echo "  flatpak install flathub com.dosbox_x.DOSBox-X" >&2
echo "and run ./airtime again." >&2
exit 1
