#! /bin/sh
#
# Infers the build directory and returns it.

if [ -n "${SPICY_BUILD_DIRECTORY}" ]; then
    # On Windows (MSYS/Git Bash), env vars passed through Python use C:/
    # paths which break bash PATH splitting. Convert to /c/ format.
    if which cygpath >/dev/null 2>&1; then
        cygpath -u "${SPICY_BUILD_DIRECTORY}"
    else
        echo ${SPICY_BUILD_DIRECTORY}
    fi
    exit 0
fi

base=$(cd $(dirname $0) && pwd)

for i in "${base}/../../build"; do
    if [ -d ${i} -a -e ${i}/CMakeCache.txt ]; then
        cd ${i}
        pwd
        exit 0
    fi
done

# Fail silently if we got an installation directory. We are
# probably running from with the alternative's settings and
# can ignore the initial error.
test -n "${SPICY_INSTALLATION_DIRECTORY}" || echo "cannot determine build directory, set SPICY_BUILD_DIRECTORY" >&2
exit 1
