#! /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/app"

options=()
source_files=()
functionDecl=0
varDecl=0

while getopts vi:IFV option; do
	case "$option" in
	i)
		source_files+=("$OPTARG");;
	I)
		options+=(-I);;
	F)
		functionDecl=1;;
	V)
		varDecl=1;;
	esac
done
shift $((OPTIND - 1))

if [ "$functionDecl" -eq 0 -a "$varDecl" -eq 0 ]; then
	functionDecl=1
fi

if [ "$functionDecl" -ne 0 ]; then
	options+=(-functionDecl)
fi
if [ "$varDecl" -ne 0 ]; then
	options+=(-varDecl)
fi
options+=(-p "$build_dir")

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

python -c 'print("*" * 80)'
run_command \
  "$run_clang_tool" "$program" "${options[@]}" "${source_files[@]}" || \
  panic "unexpected tool failure"
python -c 'print("*" * 80)'
