#!/usr/bin/env bash

# Description:
#   Build downstream example projects 
#   each individually with make/install
#
# Usage:
#   test-individual.sh [CMAKE_ARGUMENTS]

SOURCE=@PROJECT_SOURCE_DIR@/doc/fctest_examples
BUILD=@CMAKE_CURRENT_BINARY_DIR@/build

# Error handling
function test_failed {
  EXIT_CODE=$?
  { set +ex; } 2>/dev/null
  if [ $EXIT_CODE -ne 0 ]; then
    echo "+++++++++++++++++"
    echo "Test failed"
    echo "+++++++++++++++++"
  fi
  exit $EXIT_CODE
}
trap test_failed EXIT
set -e -o pipefail
set -x

# Start with clean build
rm -rf $BUILD

export fckit_DIR=@PROJECT_BINARY_DIR@
export ecbuild_DIR=@ecbuild_DIR@

# Build
mkdir -p $BUILD && cd $BUILD
cmake $SOURCE \
  -DCMAKE_BUILD_TYPE=RelWithDebInfo \
  -DECBUILD_2_COMPAT=OFF \
  "$@"

make VERBOSE=1

ctest -VV

{ set +ex; } 2>/dev/null
echo "+++++++++++++++++"
echo "Test passed"
echo "+++++++++++++++++"
