include(ExternalProject)

set(PTEX_BUILD_SHARED_LIBS ON)
set(PTEX_BUILD_STATIC_LIBS OFF)
if(NOT BUILD_SHARED_LIBS)
    set(PTEX_BUILD_SHARED_LIBS OFF)
    set(PTEX_BUILD_STATIC_LIBS ON)
endif()

set(Ptex_ARGS
    -DCMAKE_MODULE_PATH=${CMAKE_MODULE_PATH}
    -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
    -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
    -DCMAKE_PREFIX_PATH=${CMAKE_INSTALL_PREFIX}
    -DCMAKE_POLICY_DEFAULT_CMP0091:STRING=NEW
    -DCMAKE_FIND_FRAMEWORK=${CMAKE_FIND_FRAMEWORK}
    -DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS}
    -DCMAKE_C_FLAGS=${CMAKE_C_FLAGS}
    -DCMAKE_MSVC_RUNTIME_LIBRARY=${CMAKE_MSVC_RUNTIME_LIBRARY}
    -DBUILD_SHARED_LIBS=${BUILD_SHARED_LIBS}
    -DPTEX_BUILD_SHARED_LIBS=${PTEX_BUILD_SHARED_LIBS}
    -DPTEX_BUILD_STATIC_LIBS=${PTEX_BUILD_STATIC_LIBS})
if(CMAKE_CXX_STANDARD)
    set(Ptex_ARGS ${Ptex_ARGS} -DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD})
endif()

ExternalProject_Add(
    Ptex
    PREFIX ${CMAKE_CURRENT_BINARY_DIR}/Ptex
    DEPENDS ZLIB
    GIT_REPOSITORY https://github.com/wdas/ptex.git
    GIT_TAG v2.3.2
    PATCH_COMMAND
        ${CMAKE_COMMAND} -E tar xf
        ${CMAKE_SOURCE_DIR}/Ptex-patch.tar.gz
    CMAKE_ARGS ${Ptex_ARGS})
