#!/usr/bin/env bash

mkdir -p bin

py() {
	python3 $@
}

success() {
	echo -e "\033[92m\033[1m\033[7m${@}\033[0m"
}

success_lite() {
	echo -e "\033[92m${@}\033[0m"
}

error() {
	echo -e "\033[91m\033[7m\033[1m${@}\033[0m"
}

warn() {
	echo -e "\033[93m\033[1m\033[7m${@}\033[0m"
}

warn_lite() {
	echo -e "\033[93m${@}\033[0m"
}

run() {
	warn_lite "python3 ${@}"
	py $@
	code=$?
	if [ ${code} -ne 0 ]
	then
		error "Error in test '${@}'. Exiting..."
		exit ${code}
	else
		success_lite "Success in test '${@}'."
	fi
	echo
}

compile_c() {
	warn_lite "gcc ${@}"
	gcc $@
	code=$?
	if [ ${code} -ne 0 ]
	then
		error "Error in test '${@}'. Exiting..."
		exit ${code}
	else
		success_lite "Success in test '${@}'."
	fi
	echo
	warn_lite "bin/c"
	bin/c
	code=$?
	if [ ${code} -ne 0 ]
	then
		error "Error in test 'bin/c'. Exiting..."
		exit ${code}
	else
		success_lite "Success in test 'bin/c'."
	fi
	echo
}

run_tests() {
	# Test prep (generate log files)
	warn "Undergoing test preparations"
	run ./tests/write.py ./bin/w1.lnm
	run ./tests/write_many.py ./bin/w2.lnm 1000
	echo

	# Unit tests
	warn "Beginning unit tests"
	compile_c ./tests/main.c -o ./bin/c -Wall -Wextra -Werror -pedantic -I"./src/c"
	run ./tests/unit_parserargs.py
	echo

	success "Finished! Exiting with code 0"
}

run_tests

echo -e "Time elapsed: ${SECONDS} second(s)"