#!/bin/bash

if ! hash astyle; then
	echo "You do not have astyle installed so your code style is not being checked!"
	exit 1
fi

info=$(make format-check)
if [ -n "$info" ]; then
	echo "Format Error detected:"
	echo
	echo "$info"
	echo
	echo "Please run \`make format\` before next commit."
	echo
	exit 1
fi

cd src/libos
output=$(cargo fmt -- --check 2>&1)
retval=$?

if [[ $retval -eq 0 ]]
then
	exit 0
elif [[ $retval -eq 1 ]]
then
	echo "Rust format suggestsions (generated by \`cd src/libos && cargo fmt -- --check\`):"
	echo
	echo "$output"
	echo
	echo "To get rid of the format warnings above, run \`cargo fmt\` before the next commit."
	exit 1
else
	echo "Warning: \`cargo fmt\` is not available."
	exit 1
fi
