#!/usr/bin/env bash

SOURCE_DIR="`pwd`"
BUILD_DIR="`pwd`/cppbuild/Release"

ncpus=1
case "`uname`" in
  Darwin* )
    ncpus=`sysctl -n hw.ncpu`
    ;;
  Linux*)
    ncpus=$(lscpu -p | egrep -v '^#' | wc -l)
    ;;
esac

echo "Will make with \"-j $ncpus\"."

if [ -d "$BUILD_DIR" ] ; then
    echo "Build directory ($BUILD_DIR) exists, removing."
    rm -rf $BUILD_DIR
fi

mkdir -p $BUILD_DIR

(cd $BUILD_DIR && cmake -G "Unix Makefiles" $SOURCE_DIR && make clean && make -j "$ncpus" all && ctest -C Release --output-on-failure)
