#!/usr/bin/env bash

# Bash options:
set -e


# ===========================================================================
# This script is for testing HiPerConTracer in a clean environment,
# e.g. a minimal container:
# docker run --rm -it --volume $(pwd)/build-and-run-test:/root/build-and-run-test ubuntu
# docker run --rm -it --volume $(pwd)/build-and-run-test:/root/build-and-run-test debian
# docker run --rm -it --volume $(pwd)/build-and-run-test:/root/build-and-run-test fedora
# docker run --rm -it ...
# ===========================================================================


# ====== Install dependencies ===============================================
if [ -e /etc/os-release ] ; then
   . /etc/os-release
else
   echo >&2 "ERROR: /etc/os-release is not available => unknown system!"
   exit 1
fi
if [ "${ID}" == "debian" ] || [ "${ID}" == "ubuntu" ] ; then
   apt update
   DEBIAN_FRONTEND=noninteractive apt install -y sudo apt install -y build-essential cmake debhelper debian-archive-keyring debian-ports-archive-keyring distro-info pbuilder python3 python3-distro qemu-system qemu-user-static reprepro

elif [ "${ID}" == "fedora" ] ; then
   dnf install -y ...
fi


# ====== Get the HiPerConTracer sources =====================================
cd ~
if [ ! -d build-tools ] ; then
   git clone https://dreibh@github.com/dreibh/build-tools
   cd ~/build-tools
else
   cd ~/build-tools
   git pull
fi


# ====== Set MAKEFLAGS, to utilise all cores ================================
cores="$(getconf _NPROCESSORS_ONLN 2>/dev/null)" || \
   cores="$(sysctl -a | grep 'hw.ncpu' | cut -d ':' -f2 | tr -d ' ')" || \
   cores="1"
echo "This system has ${cores} cores!"
export MAKEFLAGS=-j${cores}


# ====== Build ==============================================================
rm -f CMakeCache.txt
cmake .
make
