#!/bin/bash -ex
#
# generate coverage report and upload to codacy

# extra ubuntu bionic packages: gcovr, openjdk-8-jre-headless, /usr/lib/jvm/java-8-openjdk-amd64/bin/java
# as of 6/18/2018 you must use java 8, see issue #76, #83 at https://github.com/codacy/codacy-coverage-reporter/issues
# as of 3/16/2019 with coverage reporter 4.0.3 java 8 is not required.

SOURCE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}" )/.." && pwd)"
BUILD_DIR="$(pwd)/bld-coverage"

rm -rf "${BUILD_DIR}"
mkdir "${BUILD_DIR}"
cd "${BUILD_DIR}"

cmake -DCMAKE_BUILD_TYPE=Release -DGPSBABEL_EXTRA_COMPILE_OPTIONS="--coverage" -DGPSBABEL_EXTRA_LINK_OPTIONS="--coverage" -G Ninja "${SOURCE_DIR}"
cmake --build . --target check
lcov --capture --directory "${BUILD_DIR}" --base-directory "${SOURCE_DIR}" --no-external --output-file lcov.info
genhtml lcov.info --demangle-cpp --output-directory coverage_report

# debug tokens
"${SOURCE_DIR}"/tools/ci_tokens

#!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# don't leak unhashed tokens!
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!
set +x
# this shell parameter expansion also protects the token from accidental exposure
# by substituting x for the token if the token is set and not null.
if [ -n "${CODACY_PROJECT_TOKEN:+x}" ] ; then
  # upload coverate report to codacy.
  bash <(curl -Ls https://coverage.codacy.com/get.sh) report -l CPP -r lcov.info
else
  echo "Skipping codacy coverage upload as CODACY_PROJECT_TOKEN is not set."
fi
