#!/bin/bash

THIS_SCRIPT_PATH=$(dirname $(readlink -f $0))

OPENXGUDSF_DIR=${THIS_SCRIPT_PATH%/*}
OPENXGUDSF_DIR=${OPENXGUDSF_DIR%/*}

echo "${OPENXGUDSF_DIR}"

###############################
## echo and  family
###############################
black='\E[30m'
red='\E[31m'
green='\E[32m'
yellow='\E[33m'
blue='\E[1;34m'
magenta='\E[35m'
cyan='\E[36m'
white='\E[37m'
reset_color='\E[00m'
COLORIZE=1

#-------------------------------------------------------------------------------
cecho()
{
    # Color-echo
    # arg1 = message
    # arg2 = color
    local default_msg="No Message."
    message=${1:-$default_msg}
    color=${2:-$green}
    [ "$COLORIZE" = "1" ] && message="$color$message$reset_color"
    echo -e "$message"
    return
}

echo_error()   { cecho "$*" $red          ;}
echo_fatal()   { cecho "$*" $red; exit -1 ;}
echo_warning() { cecho "$*" $yellow       ;}
echo_success() { cecho "$*" $green        ;}
echo_info()    { cecho "$*" $blue         ;}

function help()
{
	echo_error " "
   	echo_error "Usage: build_udsf [OPTION]..."
    echo_error "Build the UDSF executable."
    echo_error " "
    echo_error "Options:"
    echo_error "Mandatory arguments to long options are mandatory for short options too."
    echo_error "  -b, --build-type                          Build type as defined in cmake, allowed values are: Debug Release"
    echo_error "  -c, --clean                               Clean the build generated files: config, object, executable files (build from scratch)"
	echo_error "  -h, --help                                Print this help."
	echo_error "  -j, --jobs                                Multiple jobs for compiling."
}
function main()
{
	local -i install_deps=0
	local -i debug=0
	local -i clean=0
	local -i jobs=0

	until [ -z "$1" ]
	do
		case "$1" in
			-b | --build-type)
				if [[ "$2" -eq "Debug" ]]; then
					debug=1
				elif [[ "$2" -eq "Release" ]]; then
					debug=2
				else
					help
					return 0
				fi
				shift 2;
		;;
			-c | --clean)
				clean=1
				echo "clean generated files ($OPENXGUDSF_DIR/build/UDSF)"
				shift;
		;;
			-j | --jobs)
				jobs=1
				shift;
		;;
			-I | --install-deps)
				install_deps=1
				shift;
		;;
			-h | --help)
				help
				shift;
				return 0
		;;
			*)
				echo "Unknown option $1"
				help
				return 1
		;;
		esac
	done

	if [ ! -d "$OPENXGUDSF_DIR/build" ]; then
	  	echo "Unknown path $OPENXGUDSF_DIR/build"
	  	return 0
	fi

	if [[ $install_deps -ne 0 ]]; then
		apt-get install cmake make curl libconfig++-dev libspdlog-dev libcurl4-gnutls-dev libmysqlclient-dev -y
		read -p "Do you want to install Nlohmann Json ? <y/N> " prompt
		if [[ $prompt =~ [yY](es)* ]]; then
			if [ ! -d "$OPENXGUDSF_DIR/build/ext/json/build" ]; then
				mkdir $OPENXGUDSF_DIR/build/ext/json/build
			fi
			cd $OPENXGUDSF_DIR/build/ext/json/build
			cmake ..
			make
			make install
		fi
		echo "install successful!"
		return 0
	fi

	if [[ $clean -ne 0 ]]; then
		rm -rf $OPENXGUDSF_DIR/build/UDSF
	fi
	
	if [ ! -d "$OPENXGUDSF_DIR/build/UDSF" ]; then
	  	mkdir $OPENXGUDSF_DIR/build/UDSF
		cd $OPENXGUDSF_DIR/build/UDSF
		if [[ $debug -eq 1 ]]; then
			cmake $OPENXGUDSF_DIR/src/udsf_app -DCMAKE_OPENXGUDSF_DIR=$OPENXGUDSF_DIR -DCMAKE_OPENXGUDSF_BUILD_TYPE=Debug
		else
			cmake $OPENXGUDSF_DIR/src/udsf_app -DCMAKE_OPENXGUDSF_DIR=$OPENXGUDSF_DIR
		fi
	fi

	cd $OPENXGUDSF_DIR/build/UDSF
	if [[ $jobs -ne 0 ]]; then
		make -j`nproc`
	else
		make
	fi
}

main $@
