#!/usr/bin/env bash
set -euo pipefail
here=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)
export MAME_BIN="$here/bin/mpc"
export MAME_ROM_DIR="$here/roms"
export MAME_RUNTIME_DIR="$here/runtime"
# See mpcpi for why the CPU range comes from the kernel, not nproc.
all_cpus=$(cat /sys/devices/system/cpu/online)
all_cpus=${all_cpus#0-}
export MAME_CPUSET="${MAME_CPUSET:-0-${all_cpus:-0}}"
# Audio setup and rate matching, as in mpcpi.
"$here/scripts/mpcpi-audio-setup"
export PIPEWIRE_RATE_HZ="${PIPEWIRE_RATE_HZ:-48000}"
"$here/scripts/run-mpc.sh" "${1:-mpc2000xl}" 64 "${@:2}" &
fast_pid=$!
# The audio-thread starvation fix applies to the accurate path too.
if "$here/scripts/mpc-audio-thread-priority.sh"; then :; else
    printf 'warning: could not raise audio thread priority (chrt); audio may underrun\n' >&2
fi
wait "$fast_pid"
