#! /usr/bin/env bash

################################################################################

cmake_source_dir="@CMAKE_SOURCE_DIR@"
cmake_binary_dir="@CMAKE_BINARY_DIR@"

panic()
{
	echo "ERROR: $*"
	exit 1
}

run_command()
{
	echo "RUNNING: $*"
	"$@"
	local status=$?
	echo "EXIT STATUS: $status"
	return "$status"
}

source_dir="$cmake_source_dir"
build_dir="$cmake_binary_dir"
data_dir="$source_dir/data"
run_clang_tool="$source_dir/run_clang_tool"

################################################################################

program="$build_dir/dump_cfg"

source_files=()
verbose=0
func=
use_color=0

while getopts vf:c option; do
	case "$option" in
	c)
		use_color=1;;
	f)
		func="$OPTARG";;
	v)
		verbose=$((verbose + 1));;
	*)
		usage;;
	esac
done
shift $((OPTIND - 1))

source_files=("$@")

if [ "${#source_files[@]}" -eq 0 ]; then
	source_files=(
		"$data_dir/example_1.cpp"
		"$data_dir/example_2.cpp"
		#"$data_dir/example_3.cpp"
		#"$data_dir/example_4.cpp"
		#"$data_dir/example_5.cpp"
	)
fi

for source_file in "${source_files[@]}"; do
	python -c 'print("*" * 80)'
	options=()
	if [ -n "$func" ]; then
		options+=(-f "$func")
	fi
	if [ "$use_color" -ne 0 ]; then
		options+=(-c)
	fi
	run_command \
	  "$run_clang_tool" "$program" \
	  "${options[@]}" \
	  -p "$build_dir" \
	  "$source_file" \
	  -extra-arg=-std=c++20 || \
	  panic "tool failed"
	python -c 'print("*" * 80)'
done
