#!/bin/bash

# This is the build script to configure and build OpenViBE on
# unix-based platform.

# Functions definition

function usage()
{
		echo "usage: ./unix-build [options]"
		echo ""
		echo "-h | --help : usage"
		echo "-v | --verbose : verbose output at building step"
		echo "--build-type <Debug|Release|RelWithDebInfo> : build type"
		echo "--build-dir <dirname> : build directory"
		echo "--install-dir <dirname> : binaries deployment directory"
		echo "--dependencies-dir <dirname> : directory where dependencies are located"
		echo "--test-data-dir <dirname> : test data directory"
		echo "--make-package : a package will be created in build directory"
		echo "--hide-error-location : do not display complete error locations"
		echo "--userdata-subdir <dirname> name of the userdata sub directory"
		echo "--brand-name <brand-name> name of the brand to prefix titles and documentation"
		echo "--build-unit : if set, unit tests will be built"
		echo "--build-validation : if set, validation tests will be builts"
		echo "--gtest-lib-dir <dirname> : gtest libraries directory (default: \$\{dependencies_dir\}/libgtest)"
		echo "--gtest-include-dir <dirname> : gtest include directory (default: /usr/src/gtest/src)"
		echo "--test-output-dir <dirname> : test output files directory"
		echo "--python-exec <path> : path to python executable to use"
}

# Script starting point

# As options are not mandatory, set some default values for each target
# variables
ov_script_dir="$(readlink -m $(dirname $0))"
ov_build_type=Release
verbose=0
package_option="FALSE"
display_error_location="ON"
BrandName=OpenViBE
ov_build_unit=0
ov_build_validation=0
gtest_include_dir="/usr/src/gtest/src"

# Parse arguments
while [[ $# -gt 0 ]]; do
	key="$1"
	case $key in
		-h | --help)
			usage
			exit
			;;
		-v | --verbose)
			verbose=1
			;;
		--build-type)
			ov_build_type="$2"
			shift
			;;
		--build-dir)
			ov_build_dir="$2"
			shift
			;;
		--install-dir)
			ov_install_dir="$2"
			shift
			;;
		--dependencies-dir)
			dependencies_dir_token="-DOV_CUSTOM_DEPENDENCIES_PATH=$2"
			dependencies_dir=$2
			shift
			;;
		--test-data-dir)
			ov_test_data_dir_token="-DOVT_TEST_DATA_DIR=$2"
			shift
			;;
		--make-package)
			package_option="TRUE"
			display_error_location="OFF"
			;;
		--hide-error-location)
			display_error_location=OFF
			;;
		--userdata-subdir)
			UserDataSubdir="-DOV_CONFIG_SUBDIR=$2"
			shift
			;;
		--brand-name)
			BrandName="$2"
			shift
			;;
		--build-unit)
			ov_build_unit=1
			;;
		--build-validation)
			ov_build_validation=1
			;;
		--gtest-lib-dir)
			gtest_lib_dir_token="-DGTEST_LIBRARY=$2/libgtest.a"
			gtest_mainlib_dir_token="-DGTEST_MAIN_LIBRARY=$2/libgtest_main.a"
			shift
			;;
		--gtest-include-dir)
			gtest_include_dir="$2"
			shift
			;;
		--test-output-dir)
			ov_cmake_test_output="$2"
			shift
			;;
		--python-exec)
			python_exec="-DPYTHON_EXECUTABLE=$2"
			shift;;
		*)
			echo "ERROR: Unknown parameter $key"
			exit 1
			;;
	esac
	shift # past argument or value
done

# Check parameters validity
if [[ ${ov_build_type} != "Debug" ]] \
	&& [[ ${ov_build_type} != "Release" ]] \
	&& [[ ${ov_build_type} != "RelWithDebInfo" ]]
then
	echo "ERROR: ${ov_build_type} not handled as build type"
	exit 1
fi

# Update directories only if they point to the default one
if [[ ! -v ov_build_dir ]]; then
	ov_build_dir="${ov_script_dir}/../../openvibe-sdk-build/build-${ov_build_type}"
fi

if [[ ! -v ov_install_dir ]]; then
	ov_install_dir="${ov_script_dir}/../../openvibe-sdk-build/dist-${ov_build_type}"
fi
if [[ ! -v ov_cmake_test_output ]]; then
	ov_cmake_test_output="${ov_build_dir}/validation-test-output/"
fi

echo "**************************************"
echo "Build with parameters:"
echo "[build-type] = ${ov_build_type}"
echo "[build-dir] = ${ov_build_dir}"
echo "[dependencies-dir token] = ${dependencies_dir_token}"
echo "[install-dir] = ${ov_install_dir}"
echo "[test data directory token] = ${ov_test_data_dir_token}"
echo "[test output files directory] = ${ov_cmake_test_output}"
echo "[build unit] = ${ov_build_unit}"
echo "[build validation] = ${ov_build_validation}"
echo "[brand name] = ${BrandName}"
echo "[user data subdirectory] = ${UserDataSubdir}"
echo "[display-error-location] = ${display_error_location}"
echo "**************************************"
echo ""
echo ""

# Special configuration for MacOS
uname_string=$(uname)

if [[ "$uname_string" == 'Darwin' ]]; then
  export PKG_CONFIG_PATH="/opt/X11/lib/pkgconfig:$PKG_CONFIG_PATH"
fi

# Create necessary directories
mkdir -p "${ov_build_dir}" &> /dev/null

# Change directory to build directory as cmake has to be run from build
# directory
pushd "${ov_build_dir}" &> /dev/null

echo "Generating build system files with cmake..."

cmake ${ov_script_dir}/.. \
		-G Ninja \
		-DCMAKE_BUILD_TYPE=${ov_build_type} \
		-DCMAKE_INSTALL_PREFIX=${ov_install_dir} \
		${ov_test_data_dir_token} \
		${dependencies_dir_token} \
		-DOV_PACKAGE=${package_option} \
		-DOV_DISPLAY_ERROR_LOCATION=${display_error_location} \
		-DBUILD_UNIT_TEST=${ov_build_unit} \
		-DBUILD_VALIDATION_TEST=${ov_build_validation} \
		-DGTEST_INCLUDE_DIR=${gtest_include_dir} \
		${gtest_lib_dir_token} \
		${gtest_mainlib_dir_token} \
		-DOVT_VALIDATION_TEST_OUTPUT_DIR=${ov_cmake_test_output} \
		${UserDataSubdir} \
		-DBRAND_NAME=${BrandName} \
		${python_exec}

if [[ $? == 0 ]]; then
	echo "Generation succeeded!"
else
	echo "ERROR: Generation failed"
	exit 1
fi

echo "Building project..."

if [[ $verbose == 1 ]]; then
	ninja -v install
else
	ninja install
fi

if [[ $? == 0 ]]; then
	echo "Build succeeded!"
else
	echo "ERROR: Build failed"
	exit 1
fi


if [[ ${package_option} == "TRUE" ]]; then
	cmake --build . --target package
	if [[ $? == 0 ]]; then
		echo "Package succeeded!"
	else
		echo "ERROR: Package failed"
		exit 1
	fi
fi

popd &> /dev/null

exit
