#!/usr/bin/env bash

set -e
set -u

apt update -qq
apt install -y --no-install-recommends ca-certificates curl locales-all ccache
echo 'deb https://packages.megami.fr/debian/ buster main' > /etc/apt/sources.list.d/megami.list
curl -sSL https://packages.megami.fr/pubkey.asc > /etc/apt/trusted.gpg.d/megami.asc
apt update -qq
yes | (cd /tmp; DEBIAN_FRONTEND=noninteractive mk-build-deps --install "${CI_PROJECT_DIR}/debian/control" )
apt install -y --no-install-recommends googletest google-mock
cp -ra /usr/src/googletest "${CI_PROJECT_DIR}/"
(
  cd "${CI_PROJECT_DIR}/"
  export CCACHE_DIR="${CCACHE_DIR:-${CI_PROJECT_DIR}/.ccache}"
  export CCACHE_BASEDIR="${CCACHE_BASEDIR:-${CI_PROJECT_DIR}}"
  export CCACHE_COMPILERCHECK="${CCACHE_COMPILERCHECK:-content}"
  export PATH="/usr/lib/ccache:${PATH}"
  ccache -z
  cmake -Werror=dev -S . -B out/build -DCMAKE_C_COMPILER_LAUNCHER=ccache -DCMAKE_CXX_COMPILER_LAUNCHER=ccache
  cmake --build out/build -t check
  mkdir -p out/build/test-results
  ctest --test-dir out/build --output-on-failure --output-junit out/build/test-results/junit.xml
  ccache -s
)
