#!/bin/bash

# Trivet
# Copyright (c) 2025 by Stacy Prowell.  All rights reserved.
# https://gitlab.com/binary-tools/trivet

# If you are on *nix-type system like Linux or OS X, then this will run the
# pre-commit pipeline.  If there are *no* warnings or errors, then you are
# (probably) ready to commit.
#
# This is intended to be run from the root of the distribution, so you would
# run it with:
#
# $ . etc/build.sh

cargo install cargo-lichking
cargo install cargo-audit
cargo install cargo-auditable
cargo install cargo-semver-checks --locked
rustup component add clippy

# Depending, this might be necessary for some tests.  In general, it should not be needed.
# It is only preserved here so you can uncomment it if you need it.
#export RUST_MIN_STACK=16777216

function section () {
	echo ""
	echo "=============================================================================="
	echo "$1"
	echo "=============================================================================="
	echo ""
	shift
	yes | "$@"
	return $?
}

# Build everything.  This can take a long time, so be prepared.
# Shellcheck complains about losing the output value of yes below, so we suppress this warning.
# shellcheck disable=SC2312
cargo clean &&
	section "Updating headings" python3 etc/heading_updater.py &&
	section "Formatting code" cargo fmt &&
	section "Linting" cargo clippy -- -D warnings &&
	section "Testing (no_tracking)" cargo test --release --features no_tracking &&
	section "Testing (no_stall_detection)" cargo test --release --features no_stall_detection &&
	section "Testing (no_ucd)" cargo test --release --features no_ucd &&
	section "Testing (uppercase_hex)" cargo test --release --features uppercase_hex &&
	section "Creating auditable build" cargo auditable build --release --future-incompat-report &&
	section "Auditing security" cargo audit -D warnings &&
	section "Checking licenses" cargo lichking check &&
	RUSTDOCFLAGS="" section "Checking API document links" cargo deadlinks --check-intra-doc-links &&
	RUSTDOCFLAGS="" section "Building API documentation" cargo doc --no-deps &&
	section "Checking semantic versioning" cargo semver-checks &&
	{
		echo ""
		echo "Ready for pull request."
		echo ""
		echo "Does the version number need to be updated in Cargo.toml???"
		echo "   + bug fix / minor enhancement?  bump PATCH version"
		echo "   + next stop on roadmap?  bump MINOR version"
		echo "   + planned breaking change?  bump MAJOR version"
		echo ""
		echo "See output of cargo semver-checks above (last item run)."
		echo ""
		echo "Releasing?  See etc/release_checklist.md."
		echo ""
		echo "If you are modifying the version, you must update the CHANGELOG.md!"
		echo ""
	}
