#! /usr/bin/env bash

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

tmp_dir=/tmp/jasper
in_source_build=0
verbose=0

while getopts vIt: option; do
	case "$option" in
	t)
		tmp_dir="$OPTARG";;
	v)
		verbose=$((verbose + 1));;
	I)
		in_source_build=1;;
	*)
		usage;;
	esac
done
shift $((OPTIND - 1))

source_dir=$(pwd) || panic

if [ "$in_source_build" -ne 0 ]; then

	echo "In-source build"

	cmake_opts=()
	cmake_opts+=(-DJAS_ENABLE_DANGEROUS_INTERNAL_TESTING_MODE=1)
	(cd "$source_dir" && \
	  cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX="$install_dir" \
	  -DALLOW_IN_SOURCE_BUILD=true "${cmake_opts[@]}" .) || \
	  panic "cmake failed"

	(cd "$source_dir" && make clean && make all) || \
	  panic "make clean/all failed"

	(cd "$source_dir" && make install) || panic "make install failed"

	(cd "$source_dir" && \
	  JAS_INTERNAL_TESTING_MODE=1 \
	  CTEST_OUTPUT_ON_FAILURE=TRUE \
	  make test ARGS="-V") || \
	  panic "make test failed"

else

	export JAS_TEST_WRAPPER_VERBOSE=1

	case "$CC" in
	*gcc*)
		C_COMPILER=gcc;;
	*clang*)
		C_COMPILER=clang;;
	*)
		C_COMPILER=unknown;;
	esac

	echo "Compiler: $C_COMPILER"

	build_dir=/tmp/jasper/build

	echo "Out-of-source build"

	for lib_type in static shared; do

		install_dir="$tmp_dir/$lib_type/install"
		test_dir="$tmp_dir/$lib_type/test"
		if [ "$lib_type" = shared ]; then
			enable_shared=true
		else
			enable_shared=false
		fi

		echo "############################################################"
		echo "############################################################"
		echo "Library type: $lib_type"

		if [ "$TRAVIS_OS_NAME" = "osx" -a "$C_COMPILER" = "clang" ]; then
			build_tool="xcode"
		else
			build_tool="make"
		fi

		case "$build_tool" in
		make)
			build_targets=(clean all install)
			cmake_generator="Unix Makefiles"
			build_opts=("VERBOSE=1")
			;;
		xcode)
			build_targets=(clean_all install)
			cmake_generator="Xcode"
			build_opts=()
			;;
		*)
		esac

		echo "########################################"
		echo "cmake"
		echo "build tool: $build_tool"
		echo "########################################"

		cmake_opts=()
		cmake_opts+=(-G "$cmake_generator")
		cmake_opts+=(-DCMAKE_INSTALL_PREFIX="$install_dir")
		cmake_opts+=(-DJAS_ENABLE_SHARED="$enable_shared")
		cmake_opts+=(-DJAS_ENABLE_DANGEROUS_INTERNAL_TESTING_MODE=1)
		cmake_opts+=(-H. -B"$build_dir")
		cmake_opts+=("$@")
		echo "Running cmake ${cmake_opts[@]}"
		(cd "$source_dir" && \
		  cmake "${cmake_opts[@]}") || panic "cmake failed"

		case "$build_tool" in
		xcode)
			echo "Running xcodebuild with -list"
			(cd "$build_dir" && xcodebuild -list)
			;;
		esac
		#echo "Running cmake with --target help"
		#cmake --build "$build_dir" --target help

		echo "########################################"
		echo "build"
		echo "########################################"

		for target in "${build_targets[@]}"; do
			echo "########################################"
			echo "make $target"
			echo "########################################"
			cmake_opts=()
			cmake_opts+=(--build "$build_dir")
			if [ "$target" = "clean_all" ]; then
				cmake_opts+=(--clean-first)
			else
				cmake_opts+=(--target "$target")
			fi
			cmake_opts+=(--)
			cmake_opts+=("${build_opts[@]}")
			echo "Running cmake ${cmake_opts[@]}"
			(cmake "${cmake_opts[@]}") || panic "build of $target failed"
			if [ "$verbose" -ge 1 ]; then
				for dir in src/appl src/libjasper test/bin; do
					echo "Listing directory $build_dir/$dir"
					(cd "$build_dir" && ls -alR "$dir")
				done
			fi
			if [ "$verbose" -ge 1 ]; then
				for dir in "$install_dir"; do
					if [ -d "$dir" ]; then
						echo "Listing directory $dir"
						(cd "$dir" && ls -alR) || \
						  panic "cannot list directory $dir"
					else
						echo "Directory not found $dir"
					fi
				done
			fi
		done

		echo "########################################"
		echo "external test"
		echo "########################################"

		mkdir -p "$test_dir" || panic "cannot make directory $test_dir"
		for file in build/travis/CMakeLists.txt src/appl/jasper.c; do
			cp "$source_dir/$file" "$test_dir" || \
			  panic "cannot copy $file"
		done
		cmake_opts=()
		cmake_opts+=(-H"$test_dir" -B"$test_dir")
		cmake_opts+=(-G "Unix Makefiles")
		cmake_opts+=(-DJASPER_LIBRARIES="-L$install_dir/lib -ljasper")
		cmake_opts+=(-DJASPER_INCLUDE_DIR="$install_dir/include")
		cmake_opts+=(-DJAS_ENABLE_DANGEROUS_INTERNAL_TESTING_MODE=1)
		cmake "${cmake_opts[@]}" || panic "cmake failed"
		for target in clean all; do
			(cd "$test_dir" && make "$target" VERBOSE=1) || \
			  panic "make $target failed"
		done

		if [ "$lib_type" = shared ]; then
			echo "########################################"
			echo "test"
			echo "########################################"

			#(cd "$build_dir" && \
			#  CTEST_OUTPUT_ON_FAILURE=TRUE make test ARGS="-V") || \
			#  panic "make test failed"

			ctest_opts=()
			#ctest_opts+=(--verbose)
			ctest_opts+=(--extra-verbose)
			ctest_opts+=(--output-on-failure)
			echo "Running ctest ${ctest_opts[@]}"
			(cd "$build_dir" && \
			  JAS_INTERNAL_TESTING_MODE=1 \
			  ctest "${ctest_opts[@]}") || panic "ctest failed"
		fi

	done

fi
