#!/bin/bash
set -e

tmpdir="$(mktemp -d)"
trap "rm -rf $tmpdir" EXIT

cp -rv test "$tmpdir"

echo "8<--- snip ----------------------------------"
tee "$tmpdir/test/CMakeLists.txt" << EOF
cmake_minimum_required(VERSION 3.1)
project(build_and_run_test_suite)

find_package(foonathan_memory REQUIRED)
find_package(Catch2 REQUIRED)

add_executable(memory_test
    test_allocator.hpp
    test.cpp
    detail/align.cpp
    detail/debug_helpers.cpp
    detail/free_list.cpp
    detail/free_list_array.cpp
    detail/ilog2.cpp
    detail/memory_stack.cpp
    aligned_allocator.cpp
    allocator_traits.cpp
    default_allocator.cpp
    fallback_allocator.cpp
    iteration_allocator.cpp
    joint_allocator.cpp
    memory_arena.cpp
    memory_pool.cpp
    memory_pool_collection.cpp
    memory_resource_adapter.cpp
    memory_stack.cpp
    segregator.cpp
    smart_ptr.cpp
)
target_include_directories(memory_test PRIVATE /usr/include/catch2)
target_link_libraries(memory_test PRIVATE foonathan_memory Catch2::Catch2)
EOF
echo "8<--- snip ----------------------------------"
cd "$tmpdir"
set -x
mkdir build
cd build
cmake ../test -DCMAKE_BUILD_TYPE=RelWithDebInfo
make VERBOSE=ON -j$(nproc || echo 1)
./memory_test
