#!/usr/bin/env bash

set -euo pipefail

script_path="${0//\\//}"
case "$script_path" in
	*/*) script_dir="${script_path%/*}" ;;
	*) script_dir="." ;;
esac

root="$(CDPATH= cd -- "$script_dir" && pwd)"
NXDK_DIR="$root/nxdk"
PATH="$root/toolchain/msys2/usr/bin:$root/bin:$NXDK_DIR/bin:$PATH"
CLANG="$NXDK_DIR/bin/nxdk-cc"
GCC="$NXDK_DIR/bin/nxdk-cc"
AS="$NXDK_DIR/bin/nxdk-cc"
LD="$NXDK_DIR/bin/nxdk-link"
AR="$root/toolchain/msys2/usr/bin/llvm-ar.exe"
RANLIB="$root/toolchain/msys2/usr/bin/llvm-ranlib.exe"
CXBE="$NXDK_DIR/tools/cxbe/cxbe.exe"
if [ ! -f "$CXBE" ]; then
	CXBE="$NXDK_DIR/tools/cxbe/cxbe"
fi
export NXDK_DIR PATH CLANG GCC AS LD AR RANLIB CXBE

exec "$root/bin/fbc.exe" -target xbox -prefix "$root" -i "$root/inc" "$@"
