#!/bin/bash

BUILD_DIR="build/"
MAKEFILE="./Makefile"

OPTIONS="-DCMAKE_VERBOSE_MAKEFILE=1"
DEBUG=0

while [[ $# -gt 0 ]]; do
  flag="${1}"
  case "${flag}" in
    --cc=*)
      OPTIONS="${OPTIONS} -DCMAKE_C_COMPILER=${flag#*=}"
      ;;
    --cxx=*)
      OPTIONS="${OPTIONS} -DCMAKE_CXX_COMPILER=${flag#*=}"
      ;;
    --devel)
      OPTIONS="${OPTIONS} -DDEVEL=1"
      ;;
    --debug)
      DEBUG=1
      ;;
    --trtis)
      OPTIONS="${OPTIONS} -DBUILD_TRTIS=1"
      ;;
    *)
      echo "Unknown argument '${flag}'."
      exit 1
      ;;
  esac
  shift
done

if [[ "${DEBUG}" == "1" ]]; then
  OPTIONS="${OPTIONS} -DCMAKE_BUILD_TYPE=Debug"
else
  OPTIONS="${OPTIONS} -DCMAKE_BUILD_TYPE=Release"
fi

if [[ -d "${BUILD_DIR}" ]]; then
  rm -rf "${BUILD_DIR}"
fi

mkdir -p "${BUILD_DIR}"

pushd "${BUILD_DIR}"
cmake --version
echo "cmake .. ${OPTIONS}"
cmake .. ${OPTIONS}
popd

echo "# Auto generated makefile" > "${MAKEFILE}"
echo "" >> "${MAKEFILE}"
echo "all test:" >> "${MAKEFILE}"
echo "	make -C \"${BUILD_DIR}\" \$@" >> "${MAKEFILE}"
echo "" >> "${MAKEFILE}"
echo ".PHONY: all" >> "${MAKEFILE}"
echo "" >> "${MAKEFILE}"

