#!/bin/bash
set -euo pipefail

cd "$(dirname "$0")/src"

jobs="$(nproc 2>/dev/null || getconf _NPROCESSORS_ONLN 2>/dev/null || echo 1)"

if [ -z "${CC+x}" ]; then
    export CC="gcc -march=icelake-server -mtune=icelake-server -fomit-frame-pointer"
fi

rm -f src/makefile

if [ -x ./configure ]; then
    ./configure --competition
    make -j"${jobs}"
elif [ -f ./build.sh ]; then
    chmod +x ./build.sh
    ./build.sh
elif [ -f Makefile ] || [ -f makefile ]; then
    make -j"${jobs}"
else
    echo "no supported build entrypoint found in src/" >&2
    echo "expected ./configure, ./build.sh, Makefile, or makefile" >&2
    exit 2
fi
