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

set -eu

hideok=chronic
offline=

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

require_cmd() {
	if ! command -v "$1" >/dev/null; then
		echo "Need to have $1 installed, but can't find it" 1>&2
		return 1
	fi
}

got_cargo_cmd() {
	cargo "$1" --help >/dev/null
}

require_cmd rustc
require_cmd cc
require_cmd cargo
require_cmd python3
require_cmd subplot
require_cmd chronic

got_cargo_cmd fmt && $hideok cargo fmt -- --check

# Generate the test code with Subplot. It shall get checked with
# clippy. Also generate docs with Subplot.
subplot docgen sshca.subplot -o sshca.html

got_cargo_cmd clippy && cargo clippy --all-targets -q
$hideok cargo build --all-targets $offline
$hideok cargo test $offline

echo "Everything seems to be in order."
