#!/usr/bin/env bash
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
QEMU_REPO="$("$SCRIPT_DIR/variable.sh" --get XTEINK_QEMU_REPO)"
EMU="$QEMU_REPO/scripts/xteink-emu.py"
QEMU="$QEMU_REPO/dist/qemu-native/bin/qemu-system-riscv32"

if [[ ! -f "$EMU" ]]; then
    echo >&2 "$QEMU_REPO is not a qemu-xteink checkout (missing scripts/xteink-emu.py)"
    exit 2
fi

if [[ ! -x "$QEMU" ]]; then
    make -C "$QEMU_REPO" xteink
fi

exec "$EMU" "$@"
