#!/bin/bash
#
# Run automated tests for the project.

set -euo pipefail

quiet=-q
hideok=
offline=

if command -v chronic >/dev/null; then
	hideok=chronic
fi

while [ "$#" -gt 0 ]; do
	case "$1" in
	-v | --verbose)
		quiet=
		hideok=
		shift
		;;
	--offline)
		offline=--offline
		shift
		;;
	esac
done

got_cargo_cmd() {
	cargo --list | grep " $1 " >/dev/null
}

# shellcheck disable=2086
cargo build $offline --all-targets $quiet

# shellcheck disable=2086
got_cargo_cmd clippy && cargo clippy $offline $quiet
got_cargo_cmd fmt && cargo fmt -- --check

subplot docgen summain.subplot -o summain.html

subplot codegen summain.subplot -o test.py
rm -f test.log
if ! $hideok python3 test.py --log test.log "$@"; then
	tail -n500 test.log
	exit 1
fi

echo "Everything seems to be in order."
