cmake_minimum_required(VERSION 3.28 FATAL_ERROR)

set(BASH_EXECUTABLE "" CACHE FILEPATH "Path to bash executable")
if(WIN32)
    # prefer using git's bash over WSL's(if it exists) under System32 since it leads to wrong shader generation
    find_program(GIT_PROGRAM git NO_CACHE REQUIRED)
    get_filename_component(GIT_DIR ${GIT_PROGRAM} DIRECTORY)
    get_filename_component(GIT_BASE_DIR ${GIT_DIR} DIRECTORY)
    find_program(BASH_PROGRAM bash HINTS "${GIT_BASE_DIR}/bin" NO_CACHE NO_DEFAULT_PATH QUIET)
    if (BASH_PROGRAM)
        set(BASH_EXECUTABLE ${BASH_PROGRAM} CACHE FILEPATH "Path to bash executable" FORCE)
    endif()
endif()
if ("${BASH_EXECUTABLE}" STREQUAL "")
    find_program(BASH_PROGRAM bash NO_CACHE REQUIRED)
    set(BASH_EXECUTABLE ${BASH_PROGRAM} CACHE FILEPATH "Path to bash executable" FORCE)
endif()
# go from openVulkano/OpenVulkanoCpp to openVulkano
cmake_path(GET CMAKE_CURRENT_SOURCE_DIR PARENT_PATH ROOT_FOLDER)
set(SCRIPT_FOLDER "${ROOT_FOLDER}/Scripts")
set(SHADER_OUTPUT_DEST "${CMAKE_CURRENT_BINARY_DIR}/GeneratedShaderData")
set(SHADER_FILES_LOCATION "${CMAKE_CURRENT_SOURCE_DIR}/Shader")
set(UNIQUE_ENTRY_NAME "OpenVulkano")
configure_file(${ROOT_FOLDER}/cmake/TryCompileShaders.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/TryCompileShaders.cmake @ONLY)
execute_process(COMMAND ${BASH_EXECUTABLE} CompileShaders.sh ${SHADER_OUTPUT_DEST} "${CMAKE_CURRENT_SOURCE_DIR}/Shader" ${UNIQUE_ENTRY_NAME} WORKING_DIRECTORY ${SCRIPT_FOLDER})
file(GLOB GENERATED_SHADER_SOURCES "${SHADER_OUTPUT_DEST}/*")

file(GLOB_RECURSE sources CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/*.h" "${CMAKE_CURRENT_SOURCE_DIR}/*.c" "${CMAKE_CURRENT_SOURCE_DIR}/*.hpp" "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp")

if (APPLE)
    file(GLOB_RECURSE sources_obj CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/*.mm" "${CMAKE_CURRENT_SOURCE_DIR}/*.m")
    list(APPEND sources ${sources_obj})
endif ()

file(GLOB SHADER_SRC_FILES ${CMAKE_CURRENT_SOURCE_DIR}/Shader/*)
list(FILTER SHADER_SRC_FILES EXCLUDE REGEX ".*\\.(hpp|cpp)$")

FilterPlatformPaths(sources)
add_library(openVulkanoCpp ${MAIN_FILE} ${SHADER_SRC_FILES} ${GENERATED_SHADER_SOURCES} ${sources})
set_property(TARGET openVulkanoCpp PROPERTY CXX_STANDARD 20)
source_group("Shaders" FILES ${SHADER_SRC_FILES})
source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}" FILES ${sources})

SetWarningSettings(openVulkanoCpp)
SetGlmDefines(openVulkanoCpp)

target_include_directories(openVulkanoCpp PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ${SHADER_OUTPUT_DEST})

if(APPLE)
    LinkAppleFrameworks(openVulkanoCpp)
endif ()

if (WIN32)
    target_link_libraries(openVulkanoCpp PRIVATE Ws2_32)
endif ()

SetupVulkan(openVulkanoCpp)
SetShaderDependency(openVulkanoCpp 
                    ${CMAKE_CURRENT_SOURCE_DIR}/Shader
                    ${SHADER_OUTPUT_DEST})

if (NOT ANDROID AND NOT IOS)
    if (LINUX)
        target_link_libraries(openVulkanoCpp PUBLIC fontconfig)
    endif()
    target_link_libraries(openVulkanoCpp PUBLIC glfw pugixml ftxui::screen ftxui::dom ftxui::component)
    if (ENABLE_CURL)
        LinkCurl(openVulkanoCpp)
    endif()
endif()

target_link_libraries(openVulkanoCpp PUBLIC magic_enum yaml-cpp fmt spdlog glm pugixml stb eigen utf8cpp imgui_internal TracyClient stud-uuid ryml unordered_dense concurrentqueue units dds_image)
if (ENABLE_KTX)
    target_link_libraries(openVulkanoCpp PUBLIC ktx)
endif ()
LinkAssimp(openVulkanoCpp)
LinkLibArchive(openVulkanoCpp)
LinkLibJpegTurbo(openVulkanoCpp)
LinkMsdf(openVulkanoCpp)

SetGlmDefines(openVulkanoCpp)
add_compile_definitions(LIBARCHIVE_STATIC)
add_compile_definitions("$<$<CONFIG:Debug>:DEBUG>")
