# Builds tests/cmake/consumer.cpp with one of the two CMake blocks of
# README.md, cut out of the README here, so that what the README shows is what
# gets configured. Configure with -DMESHSEARCH_README_BLOCK=0 and
# -DCMAKE_PREFIX_PATH=<an install prefix> for the find_package block, or with
# -DMESHSEARCH_README_BLOCK=1 and
# -DFETCHCONTENT_SOURCE_DIR_MESHSEARCH=<the meshsearch checkout> for the
# FetchContent block, which then fetches nothing. The README names the target
# your_target.

cmake_minimum_required(VERSION 3.16)

project(meshsearch_readme_consumer LANGUAGES CXX)

if (NOT DEFINED MESHSEARCH_README_BLOCK)
  message(FATAL_ERROR "set MESHSEARCH_README_BLOCK to 0 or 1")
endif()

file(READ ${CMAKE_CURRENT_SOURCE_DIR}/../../../README.md readme)
string(REGEX MATCHALL "```cmake\n[^`]*```" blocks "${readme}")
list(LENGTH blocks count)
if (NOT count EQUAL 2)
  message(FATAL_ERROR "README.md has ${count} cmake blocks, not 2")
endif()
list(GET blocks ${MESHSEARCH_README_BLOCK} block)
string(REGEX REPLACE "^```cmake\n" "" block "${block}")
string(REGEX REPLACE "```$" "" block "${block}")
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/readme_block.cmake "${block}")

add_executable(your_target ../consumer.cpp)
include(${CMAKE_CURRENT_BINARY_DIR}/readme_block.cmake)
