#!/bin/bash
###############################################################################
# License: BSD Zero Clause License file
# Copyright:
#   (C) 2012 - 2021 Alexander Shaduri <ashaduri@gmail.com>
###############################################################################

# Configure a build directory for development


function print_usage() {
	echo -e "Usage:"
	echo -e "    ${0} [-t <toolchain_name>] [-c <compiler>] [-b <build_type>]"
	echo -e "    [-s <source_dir>] [-g <generator>] [-o <cmake_options>]"
	echo -e "\nDetails:"
	echo -e "-t <toolchain_name> - toolchain_name can be e.g. \"win32-mingw64\"."
	echo -e "    A toolchain file \"toolchains/<toolchain_name>.cmake\" will be used."
	echo -e "-c <compiler> - compiler is one of:"
	echo -e "    gcc, gcc-<version>, clang, intel."
	echo -e "    The gcc version is the suffix of gcc and g++ executables, e.g. \"4.8\" in"
	echo -e "    \"gcc-4.8\"."
	echo -e "    If unspecified, CC and CXX environment variables are used."
	echo -e "-b <build_type> - build_type is one of:"
	echo -e "    Debug, Release, RelWithDebInfo, MinSizeRel, none. The default is Debug."
	echo -e "-s <source_dir> - source_dir is \"..\" by default."
	echo -e "-g <cmake_generator> - cmake uses \"Unix Makefiles\" by default."
	echo -e "-o <cmake_options> - options to pass directly to cmake."
}

compiler=""
build_type="";
source_dir=".."
toolchain_name=""
generator=""
cmake_options=""

while getopts "t:c:b:s:g:o:h:" opt; do
	case $opt in
		t)
			echo "Requested toolchain: $OPTARG"
			toolchain_name="$OPTARG";
			;;
		c)
			echo "Requested compiler: $OPTARG"
			compiler="$OPTARG";
			;;
		b)
			echo "Requested build type: $OPTARG"
			build_type="$OPTARG";
			;;
		s)
			echo "Requested source directory type: $OPTARG"
			source_dir="$OPTARG";
			;;
		g)
			echo "Requested generator: $OPTARG"
			generator="$OPTARG";
			;;
		o)
			echo "Requested cmake options: $OPTARG"
			cmake_options="$OPTARG";
			;;
		h)
			print_usage;
			exit 1;
			;;
		\?)
			echo "Invalid option: $opt";
			print_usage;
			exit 1;
			;;
	esac
done


if [ "$compiler" != "" ] && [ "$toolchain_name" != "" ]; then
	echo "Error: Conflicting options -c and -t specified."
	exit 1;
fi


cmake_flags="-Wdev";

if [ "$build_type" = "none" ]; then
	build_type="";
fi
if [ "$build_type" != "" ]; then
  cmake_flags="$cmake_flags -DCMAKE_BUILD_TYPE=$build_type";
fi

if [ "$generator" != "" ]; then
	cmake_flags="$cmake_flags -G${generator}";
fi


status=1


# Toolchain mode
if [ "$toolchain_name" != "" ]; then
	cmake_flags="$cmake_flags -DCMAKE_TOOLCHAIN_FILE='${source_dir}/data/cmake/toolchains/${toolchain_name}.cmake'"

	echo "Running:"
	echo "cmake $cmake_flags $source_dir";

	cmake $cmake_flags $cmake_options $source_dir
	status=$?

else  # Compiler mode
	c_compiler="";
	cxx_compiler="";

	# Detect gcc suffix like "-4.7"
	if [ "${compiler:0:4}" = "gcc-" ]; then
		suffix=${compiler:4};
		c_compiler="gcc-${suffix}";
		cxx_compiler="g++-${suffix}";
		compiler="gcc";
	fi

	case $compiler in
		gcc)
			if [ "$c_compiler" = "" ]; then
				c_compiler="gcc${machine_bits_switch}";
				cxx_compiler="g++${machine_bits_switch}";
			fi
			;;
		intel)
			if [ $machine_bits_precise = "64" ]; then
				c_compiler="icc64";
				cxx_compiler="icpc64";
			else
				c_compiler="icc32";
				cxx_compiler="icpc32";
			fi
			;;
		clang)
			c_compiler="clang${machine_bits_switch}";
			cxx_compiler="clang++${machine_bits_switch}";
			;;
		"")
			c_compiler="$CC"
			cxx_compiler="$CXX"
			;;
		*)
			echo "Unsupported compiler given; use CC and CXX instead."
			exit 1;
			;;
	esac

	echo "Running:"
	echo "CC=\"$c_compiler\" CXX=\"$cxx_compiler\" cmake $cmake_flags $source_dir";

	CC="$c_compiler" CXX="$cxx_compiler" cmake $cmake_flags $cmake_options $source_dir
	status=$?
fi


exit $status

