#!/bin/sh
set -e

cp tests/*.cpp "$AUTOPKGTEST_TMP"

cd "$AUTOPKGTEST_TMP"
cat > CMakeLists.txt << EOF
cmake_minimum_required(VERSION 3.14)
project(tl-optional-autopkgtest)
find_package(tl-optional REQUIRED)
find_package(Catch2 REQUIRED)

enable_testing()
file(GLOB test-sources CONFIGURE_DEPENDS *.cpp)
foreach (source IN LISTS test-sources)
    get_filename_component(name "\${source}" NAME_WE)
    set(test "\${PROJECT_NAME}-test-\${name}")
    add_executable(\${test}
	  "\${source}")
    target_link_libraries(\${test}
      PRIVATE
        Catch2::Catch2WithMain tl::optional)
    add_test(NAME \${PROJECT_NAME}::\${name} COMMAND \${test})
endforeach()
EOF

mkdir _build
cd _build
cmake .. -DCMAKE_BUILD_TYPE=RelWithDebInfo
cmake --build . --verbose
ctest --output-on-failure

