cmake_minimum_required(VERSION 3.16)
cmake_policy(VERSION 3.16)
{% set project_name %}{{stinger.name|snake_case|replace('_', '-')|replace('--', '-')}}{% endset %}
{% set cmake_name %}{{stinger.name|PascalCase|replace('_', '')|replace('--', '')}}{% endset %}
project ({{project_name}}
	LANGUAGES CXX
	VERSION {{stinger.version}})

include(GNUInstallDirs)
include(CMakePackageConfigHelpers)

# require C++17
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# more secure code
set (CMAKE_POSITION_INDEPENDENT_CODE ON)

find_package(Threads REQUIRED)
find_package(RapidJSON REQUIRED)

# Fetch stinger-cpp-utils dependency
include(FetchContent)
FetchContent_Declare(
	stinger-cpp-utils
	GIT_REPOSITORY https://github.com/stinger-ipc/stinger-cpp-utils.git
	GIT_TAG main  # or specify a specific tag/commit
)
FetchContent_MakeAvailable(stinger-cpp-utils)

set(CLIENT_SOURCE_FILES
	{%for src_file in output_files|match_regex("^src/.*\.cpp$")|exclude_regex("server.cpp$")|sort-%}
		{{src_file}}
	{%endfor%}
)

set(SERVER_SOURCE_FILES
	{%for src_file in output_files|match_regex("^src/.*\.cpp$")|exclude_regex("client.cpp$")|sort-%}
		{{src_file}}
	{%endfor%}
)

# server lib
add_library({{project_name}}-server ${SERVER_SOURCE_FILES})
add_library({{cmake_name}}::Server ALIAS {{project_name}}-server)
target_include_directories({{project_name}}-server
	PUBLIC
		$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
		$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
		${rapidJSON_INCLUDE_DIRS}
)
target_link_libraries({{project_name}}-server
	PUBLIC
		Stinger::Utils
)

# client lib
add_library({{project_name}}-client ${CLIENT_SOURCE_FILES})
add_library({{cmake_name}}::Client ALIAS {{project_name}}-client)
target_include_directories({{project_name}}-client
	PUBLIC
		$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
		$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
		${rapidJSON_INCLUDE_DIRS}
)
target_link_libraries({{project_name}}-client
	PUBLIC
		Stinger::Utils
)

# example client
add_executable({{project_name}}-client-demo examples/client_main.cpp)
target_link_libraries({{project_name}}-client-demo
	PRIVATE
		{{cmake_name}}::Client
		Threads::Threads
)

add_executable({{project_name}}-server-demo examples/server_main.cpp)
target_link_libraries({{project_name}}-server-demo
	PRIVATE
		{{cmake_name}}::Server
		Threads::Threads
)

add_executable({{project_name}}-discovery-example examples/discovery_example.cpp)
target_link_libraries({{project_name}}-discovery-example
	PRIVATE
		{{cmake_name}}::Client
		Threads::Threads
)

add_executable({{project_name}}-discovery-publisher examples/discovery_publisher.cpp)
target_link_libraries({{project_name}}-discovery-publisher
	PRIVATE
		{{cmake_name}}::Client
		Threads::Threads
)

set_target_properties(
	{{project_name}}-client
	{{project_name}}-server
	{{project_name}}-client-demo
	{{project_name}}-server-demo
	{{project_name}}-discovery-publisher
	{{project_name}}-discovery-example
	PROPERTIES
		CXX_STANDARD 17 # use 17, if you can
		CXX_STANDARD_REQUIRED ON # use 17, I insist
		CXX_EXTENSIONS OFF # use c++17 not gnu++17
)

# help cmake find this
configure_package_config_file(
	${CMAKE_CURRENT_SOURCE_DIR}/cmake/config.cmake.in
	${CMAKE_CURRENT_BINARY_DIR}/{{cmake_name}}-config.cmake
	INSTALL_DESTINATION ${CMAKE_INSTALL_DATADIR}/cmake/{{cmake_name}}
	PATH_VARS CMAKE_INSTALL_INCLUDEDIR CMAKE_INSTALL_LIBDIR
)

# help cmake enforce version rules
write_basic_package_version_file(
	${CMAKE_CURRENT_SOURCE_DIR}/cmake/{{cmake_name}}-config-version.cmake
	VERSION {{stinger.version}}
	COMPATIBILITY AnyNewerVersion
)

install(FILES
	${CMAKE_CURRENT_BINARY_DIR}/{{cmake_name}}-config.cmake
	${CMAKE_CURRENT_BINARY_DIR}/{{cmake_name}}-config-version.cmake
	DESTINATION ${CMAKE_INSTALL_DATADIR}/cmake/{{cmake_name}}
)

install(DIRECTORY
	${CMAKE_CURRENT_SOURCE_DIR}/include/{{cmake_name}}
	DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)

install(TARGETS
	{{project_name}}-server
	{{project_name}}-client
	{{project_name}}-server-demo
	{{project_name}}-client-demo
	{{project_name}}-discovery-example
	{{project_name}}-discovery-publisher
	stinger_utils
	EXPORT {{cmake_name}}Targets
	ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} # .a files
	LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} # .so files
	RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} # executables
)

install(EXPORT
	{{cmake_name}}Targets
	FILE {{cmake_name}}-targets.cmake
	DESTINATION ${CMAKE_INSTALL_DATADIR}/cmake/{{cmake_name}}
	NAMESPACE {{cmake_name}}::
)

export (EXPORT {{cmake_name}}Targets
	FILE ${CMAKE_CURRENT_BINARY_DIR}/{{cmake_name}}-targets.cmake
	NAMESPACE {{cmake_name}}::
)
