include(FetchContent)

FetchContent_Declare(
    procthor2glb_stb
    GIT_REPOSITORY https://github.com/nothings/stb.git
    GIT_TAG        f1c79c02822848a9bed4315b12c8c8f3761e1296
    EXCLUDE_FROM_ALL
)
rl_tools_fetchcontent_makeavailable_quiet(procthor2glb_stb)

FetchContent_Declare(
    procthor2glb_tinygltf
    GIT_REPOSITORY https://github.com/syoyo/tinygltf.git
    GIT_TAG        v3.0.1
    GIT_SHALLOW    TRUE
    EXCLUDE_FROM_ALL
)
rl_tools_fetchcontent_makeavailable_quiet(procthor2glb_tinygltf)

if(POLICY CMP0135)
    cmake_policy(SET CMP0135 NEW)
endif()
FetchContent_Declare(
    procthor2glb_basisu
    URL https://github.com/BinomialLLC/basis_universal/archive/refs/tags/v2_1_0r.tar.gz
    URL_HASH SHA256=0cd45ad268c0f048847f0bd3f2e22c160ca3ab8e9e11e49c354eba80cfc169e3
    EXCLUDE_FROM_ALL
)
rl_tools_fetchcontent_makeavailable_quiet(procthor2glb_basisu)

# the conversion core as a library: shared by the CLI below and the AI2ThorHab dataset source
# (include/rl_tools/hyperlux/datasets/procthor/conversion.h declares the API)
add_library(rl_tools_procthor_conversion STATIC
    conversion.cpp
    ${procthor2glb_basisu_SOURCE_DIR}/transcoder/basisu_transcoder.cpp
)
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
    set_source_files_properties(${procthor2glb_basisu_SOURCE_DIR}/transcoder/basisu_transcoder.cpp
        PROPERTIES COMPILE_OPTIONS "-Wno-stringop-overflow;-Wno-type-limits")
endif()
target_link_libraries(rl_tools_procthor_conversion PUBLIC RLtools::RLtools)
target_include_directories(rl_tools_procthor_conversion SYSTEM PRIVATE
    ${procthor2glb_stb_SOURCE_DIR}
    ${procthor2glb_tinygltf_SOURCE_DIR}/attic # tinygltf >= v3 is a C rewrite; the v2 C++ single-header lives in attic/
    ${procthor2glb_basisu_SOURCE_DIR}/transcoder
)

find_library(PROCTHOR2GLB_ZSTD_LIBRARY NAMES zstd)
if(PROCTHOR2GLB_ZSTD_LIBRARY)
    target_link_libraries(rl_tools_procthor_conversion PRIVATE ${PROCTHOR2GLB_ZSTD_LIBRARY})
else()
    target_compile_definitions(rl_tools_procthor_conversion PRIVATE BASISD_SUPPORT_KTX2_ZSTD=0)
endif()

add_executable(procthor2glb procthor2glb.cpp)
target_link_libraries(procthor2glb PRIVATE rl_tools_procthor_conversion)
