#!/bin/bash -e
#
# check-consistency verifies that ATS is consistent and well formed. It does
# not modify any files.

errors=0

# error reports and counts errors
function error() {
	errors=$((errors+1))
	echo 1>&2 "error: $@"
}

while IFS= read -r -d $'\0' file; do
	name=$(basename $file .ttcn)

	: Verify that the file path only contains valid characters.
	if ! [[ ${file%.ttcn} =~ [A-Za-z0-9_/]+ ]]; then
		error "invalid characters in file path: $file"
	fi

	: Verify that the module prefix is valid.
	if ! [[ $name =~ ^(Sem_|NegSem_|Syn_|NegSyn_|Pos_|Neg_) ]]; then
		error "invalid module prefix: $file"
	fi

	# TODO(simonm): Verify that the file name is the same as the module name.
	# TODO(simonm): Verify that required tags are present and well formed.
	# TODO(simonm): Verify that the sections are correct in path and @purpose tags.

done < <(find ATS -type f -name \*.ttcn -print0)

if [[ $errors -gt 0 ]]; then
	echo "Found $errors errors"
	exit 1
fi
