#!/bin/sh
# Copyright 2016 Ghislain Antony Vaillant
#
# This file is part of the packaging testsuite for glbinding.

set -e

pkg=glbinding

if [ "${AUTOPKGTEST_TMP}" = "" ] ; then
  AUTOPKGTEST_TMP=$(mktemp -d /tmp/${pkg}-test.XXXXXX)
  # shellcheck disable=SC2064
#  trap "rm -rf ${AUTOPKGTEST_TMP}" 0 INT QUIT ABRT PIPE TERM
fi

set -x
# Copy the upstream testsuite.
cp -a /usr/share/doc/libglbinding-dev/examples/* "$AUTOPKGTEST_TMP"
cp -a /usr/src/googletest "$AUTOPKGTEST_TMP"

cd "$AUTOPKGTEST_TMP"

# Create a standalone CMake project.
mv CMakeLists.txt CMakeLists.txt.orig

cat << EOF > CMakeLists.txt
project(glbinding-tests)

if (POLICY CMP0063)
  cmake_policy(SET CMP0063 NEW)
endif ()

find_package(glbinding REQUIRED)
set(META_PROJECT_NAME "glbinding")

list(APPEND CMAKE_MODULE_PATH "\${CMAKE_CURRENT_SOURCE_DIR}/cmake")
include(CompileOptions)

set(OPTION_BUILD_TESTS ON)
set(OPTION_BUILD_GPU_TESTS OFF)

EOF

cat CMakeLists.txt.orig >> CMakeLists.txt

# The RingBuffer test is excluded, since it relies on private headers.
sed -e '/RingBuffer/ s/^/#/' -i glbinding-test/CMakeLists.txt

# Configure, build and execute.
mkdir build && cd build
cmake .. && make && make test
