#!/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 "--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 "--sdk=<dirname> : path to the Openvibe SDK"
		echo "--oem-distribution=<name of the oem distribution> : distribution name, can be openvibe or mensia"
		echo "--userdata-subdir=<dirname> name of the userdata sub directory"
}

# Script starting point

# As options are not mandatory, set some default values for each target
# variables
ov_script_dir="${PWD}"
ov_build_type=Release
ov_test_data_dir="${ov_script_dir}/../dependencies/test-data"
ov_oem_distribution="openvibe"
verbose="OFF"
package_option="FALSE"
display_error_location="ON"

# Parse arguments
for i in "$@"
do
	case $i in
		-h | --help)
			usage
			exit
			;;
		-v | --verbose)
			verbose="ON"
			shift
			;;
		--build-type=*)
			ov_build_type="${i#*=}"
			shift
			;;
		--build-dir=*)
			ov_build_dir="${i#*=}"
			shift
			;;
		--install-dir=*)
			ov_install_dir="${i#*=}"
			shift
			;;
		--make-package)
			package_option="TRUE"
			display_error_location="OFF"
			shift
			;;
		--hide-error-location)
			display_error_location="OFF"
			;;
		--test-data-dir=*)
			ov_test_data_dir="${i#*=}"
			shift
			;;
		--sdk=*)
			ov_sdk_dir="${i#*=}"
			shift
			;;
		--oem-distribution=*)
			ov_oem_distribution="${i#*=}"
			shift
			;;
		--userdata-subdir=*)
			UserDataSubdir="-DOV_CONFIG_SUBDIR=${i#*=}"
			shift
			;;
		*)
			echo "ERROR: Unknown parameter $i"
			exit 1
			;;
	esac
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

if [[ ${ov_build_type} == "Debug" ]]; then
	sdk_affix="debug"
elif [[ ${ov_build_type} == "Release" ]] || [[ ${ov_build_type} == "RelWithDebInfo" ]]; then
	sdk_affix="release"
fi
if [[ ! -v ov_sdk_dir ]]; then
	ov_sdk_dir="${ov_script_dir}/../dependencies/openvibe-sdk-${sdk_affix}"
fi
if [[ ! -v ov_build_dir ]]; then
	ov_build_dir="${ov_script_dir}/../../openvibe-designer-build/build-${ov_build_type}"
fi
if [[ ! -v ov_install_dir ]]; then
	ov_install_dir="${ov_script_dir}/../../openvibe-designer-build/dist-${ov_build_type}"
fi
	
echo "**************************************"
echo "Build with parameters:"
echo "[build-type] = ${ov_build_type}"
echo "[build-dir] = ${ov_build_dir}"
echo "[install-dir] = ${ov_install_dir}"
echo "[test-data-dir] = ${ov_test_data_dir}"
echo "[sdk] = ${ov_sdk_dir}"
echo "[oem-distribution] = ${ov_oem_distribution}"
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} \
		-DOV_PACKAGE=${package_option} \
		-DOV_DISPLAY_ERROR_LOCATION=${display_error_location} \
		-DOVT_TEST_DATA_DIR=${ov_test_data_dir} \
		-DFlag_VerboseOutput=${verbose} \
		-DOPENVIBE_SDK_PATH=${ov_sdk_dir} \
		-DOEM_DISTRIBUTION=${ov_oem_distribution} \
		${UserDataSubdir}



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

echo "Building project..."

if [[ $verbose == "ON" ]]; 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