cmake_minimum_required(VERSION 3.13.0)
project(jolt-physics)

set(CMAKE_CXX_STANDARD 17)
# The COMPONENTS part checks that OGRE was built the way we need it
# The CONFIG flag makes sure we get OGRE instead of OGRE-next
find_package(Jolt REQUIRED)
find_package(OGRE REQUIRED COMPONENTS Bites Paging Terrain CONFIG)
find_package(ZLIB)
find_package(SDL2)
find_package(assimp REQUIRED CONFIG)
find_package(OgreProcedural REQUIRED CONFIG)
find_package(pugixml REQUIRED CONFIG)
find_package(flecs REQUIRED CONFIG)

add_library(fix::assimp INTERFACE IMPORTED)
set_target_properties(fix::assimp PROPERTIES
       INTERFACE_LINK_LIBRARIES "${ASSIMP_LIBRARIES};pugixml"
       INTERFACE_LINK_DIRECTORIES "${ASSIMP_LIBRARY_DIRS}"
)

add_library(fix::OgreProcedural INTERFACE IMPORTED)
set_target_properties(fix::OgreProcedural PROPERTIES
       INTERFACE_LINK_LIBRARIES "OgreProcedural"
       INTERFACE_LINK_DIRECTORIES "${CMAKE_PREFIX_PATH}/lib"
)

if(OGRE_STATIC)
add_library(OgreGLSupportStatic INTERFACE IMPORTED)
set_target_properties(OgreGLSupportStatic PROPERTIES
       INTERFACE_LINK_LIBRARIES "OgreGLSupportStatic"
       INTERFACE_LINK_DIRECTORIES "${CMAKE_PREFIX_PATH}/lib"
)
endif()

add_library(fix::pugixml INTERFACE IMPORTED)
set_target_properties(fix::pugixml PROPERTIES
       INTERFACE_LINK_LIBRARIES "pugixml"
       INTERFACE_LINK_DIRECTORIES "${CMAKE_PREFIX_PATH}/lib"
)
set(STAGE_DIRECTORIES
	resources/main
	resources/shaderlib
	resources/terrain
	skybox)
set(STAGE_FILES_OUTPUT)
foreach(STAGE_DIR ${STAGE_DIRECTORIES})
	add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/${STAGE_DIR}
		COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/${STAGE_DIR}
		COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_SOURCE_DIR}/${STAGE_DIR} ${CMAKE_BINARY_DIR}/${STAGE_DIR}
	)
	list(APPEND STAGE_FILES_OUTPUT ${CMAKE_BINARY_DIR}/${STAGE_DIR})
endforeach()

add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/resources.cfg
	COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/resources.cfg ${CMAKE_BINARY_DIR}/resources.cfg
	DEPENDS ${CMAKE_SOURCE_DIR}/resources.cfg
)
add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/resources/world/world.glb
	COMMAND ${CMAKE_COMMAND}
		-E copy
			${CMAKE_SOURCE_DIR}/assets/blender/jolt-test/world.glb
			${CMAKE_BINARY_DIR}/resources/world/world.glb
	DEPENDS ${CMAKE_SOURCE_DIR}/assets/blender/jolt-test/world.glb
)
list(APPEND STAGE_FILES_OUTPUT ${CMAKE_BINARY_DIR}/resources/world/world.glb)

add_custom_target(stage_files ALL DEPENDS ${STAGE_FILES_OUTPUT} ${CMAKE_BINARY_DIR}/resources.cfg)
# add the source files as usual
add_library(physics STATIC physics.cpp)
target_link_libraries(physics PUBLIC OgreMain Jolt::Jolt)
add_executable(jolt-demo main.cpp)
target_link_libraries(jolt-demo physics)
 
# this also sets the includes and pulls third party dependencies
target_link_libraries(jolt-demo OgreBites OgrePaging ${ASSIMP_LIBRARIES}
	-Wl,--as-needed
	)
if(OGRE_STATIC)
target_link_options(jolt-demo PRIVATE -static-libstdc++ -static-libgcc)
endif()
add_dependencies(jolt-demo stage_files)

install(TARGETS jolt-demo DESTINATION bin)

