#!/bin/sh
#
# This script is used by CI and build-in-docker.

set -eu

if [ -n "${VERBOSE:-}" ]; then
    set -x
fi

if [ -n "${SPECIAL:-}" ]; then
    exec "ci/$SPECIAL"
else
    [ -z ${JOBS:+x} ] && JOBS=$(getconf _NPROCESSORS_ONLN 2>/dev/null)
    [ -z ${JOBS:+x} ] && JOBS=2

    mkdir -p ${BUILDDIR:-build}
    cd ${BUILDDIR:-build}
    ${CMAKE_PREFIX:-} cmake ${CMAKE_PARAMS:-} ${CCACHE_LOC:-..}
    ${CMAKE_PREFIX:-} cmake --build . ${EXTRA_CMAKE_BUILD_FLAGS:-} -- -j$JOBS
    case "${RUN_TESTS:-all}" in
        all)
            CC=${TEST_CC:-${CC}} ctest --output-on-failure -j$JOBS "$@"
            ;;
        unittest-in-wine)
            wine ccache.exe --version
            wine unittest/unittest.exe
            ;;
        none)
            ;;
    esac
fi
