#!/bin/sh

# common configure script
SCRIPTS_DIR=$(dirname "$0")
. "${SCRIPTS_DIR}/include-common"

SBUILD=$(pwd)
BUILD=$(readlink -f "$SBUILD")

for last; do true; done

if [ -d "$last" ]; then
	SOURCE=$(readlink -f "$last")
else
	echo "Usage: $SELF [Further CMake Arguments] Elektra-source-dir"
	echo
	echo "Error: You did not provide the source directory to Elektra as argument"
	exit 1
fi

if [ "$SOURCE" = "$BUILD" ]; then
	if echo $* | grep FORCE_IN_SOURCE_BUILD=ON; then
		echo "Warning: in-source build!"
		echo "It is recommended to create a new directory and run $SELF there"
		echo "Proceeding as requested"
	else
		echo "Error: in-source build!"
		echo "It is recommended to create a new directory and run $SELF there:"
		echo 'For example by executing "mkdir build && cd build".'
		echo "Alternatively, you can force an in-source build with -DFORCE_IN_SOURCE_BUILD=ON"
		exit 1
	fi
fi

if [ "$SCRIPTS" = "$BUILD" ]; then
	echo "You seem to be in the scripts directory!"
	echo "Create a new directory,"
	echo 'for example by executing "mkdir ../build && cd ../build",'
	echo "and run $SELF there"
	exit 1
fi
