#!/bin/bash
set -e

BDIR=build_firmware
BDIR_TEST=build_test

meson . "$BDIR_TEST"
meson compile -C "$BDIR_TEST"

meson . "$BDIR" --cross-file meson_cross_avr.txt

for arg in "$@"; do
	meson configure -D"$arg" "$BDIR"
done

# TODO this is a shit way to do this
if [[ "$FLASH" -ne 0 ]]; then
	meson compile -C "$BDIR" flash
else
	meson compile -C "$BDIR"
fi

echo
echo "########################################################################"
echo "Build complete. Firmware files are at:"
echo "  - $BDIR/poly1.elf"
echo "  - $BDIR/poly1.hex"
echo "Test executables are in $BDIR_TEST."
