if(PYTHON_BINDING)

# name of the build shared object and target to build
set(PYCAMITK_LIB_NAME camitk)
set(PYCAMITK_TARGET_LIB_NAME library-${PYCAMITK_LIB_NAME}pythoncore)

set(PYBIND11_FINDPYTHON ON)
find_package(pybind11 CONFIG REQUIRED)

pybind11_add_module(${PYCAMITK_TARGET_LIB_NAME} MODULE 
                             application_bindings.h
                             application_bindings.cpp
                             action_bindings.h 
                             action_bindings.cpp
                             component_bindings.h
                             component_bindings.cpp
                             console_bindings.h
                             console_bindings.cpp
                             core_bindings.h
                             core_bindings.cpp
                             core_utils.h
                             core_utils.cpp
                             extensionmanager_bindings.h
                             extensionmanager_bindings.cpp
                             log_bindings.h
                             log_bindings.cpp
                             numpy_utils.h
                             numpy_utils.cpp
                             qt_bindings.h
                             qt_bindings.cpp
                             transformation_manager_bindings.h
                             transformation_manager_bindings.cpp
                             docstrings.h
                             camitk_python_module.cpp
)

# include directories from build, camitk (local or global install).
target_link_directories(${PYCAMITK_TARGET_LIB_NAME} PRIVATE ${CAMITK_LINK_DIRECTORIES})

target_link_libraries(${PYCAMITK_TARGET_LIB_NAME} PRIVATE ${CAMITK_CORE_LIBRARIES} ${CAMITK_LIBRARIES} ${QT_LIBRARIES} pybind11::module) #Python::Python)

# directly use the include from core source directory (uses modern cmake)
target_include_directories(${PYCAMITK_TARGET_LIB_NAME} PRIVATE
    $<TARGET_PROPERTY:library-${CAMITK_CORE_LIB_NAME},INTERFACE_INCLUDE_DIRECTORIES>
)

add_dependencies(${PYCAMITK_TARGET_LIB_NAME} library-${CAMITK_CORE_LIB_NAME})

# for VTK9, see https://docs.vtk.org/en/latest/advanced/migration_guides/ModuleMigration.html
if("${VTK_VERSION}" VERSION_GREATER_EQUAL 9.0)
    vtk_module_autoinit(TARGETS ${PYCAMITK_TARGET_LIB_NAME}
                        MODULES ${CAMITK_VTK_LIBRARIES}
    )
endif()

if (MSVC)
    set(OUTPUT_DIRECTORY ${CAMITK_BUILD_BIN_DIR})
else()
    set(OUTPUT_DIRECTORY ${CAMITK_BUILD_PUBLIC_LIB_DIR})
endif()

set_target_properties(${PYCAMITK_TARGET_LIB_NAME} PROPERTIES
    PREFIX ""
    OUTPUT_NAME "${PYCAMITK_LIB_NAME}"
    VERSION   "${CAMITK_VERSION_MAJOR}.${CAMITK_VERSION_MINOR}.${CAMITK_VERSION_PATCH}"
    SOVERSION "${CAMITK_VERSION_MAJOR}"
    LIBRARY_OUTPUT_DIRECTORY ${OUTPUT_DIRECTORY}
    LIBRARY_OUTPUT_DIRECTORY ${OUTPUT_DIRECTORY}
    LIBRARY_OUTPUT_DIRECTORY_DEBUG ${OUTPUT_DIRECTORY}
    LIBRARY_OUTPUT_DIRECTORY_RELEASE ${OUTPUT_DIRECTORY}
    RUNTIME_OUTPUT_DIRECTORY ${OUTPUT_DIRECTORY}
    RUNTIME_OUTPUT_DIRECTORY_DEBUG ${OUTPUT_DIRECTORY}
    RUNTIME_OUTPUT_DIRECTORY_RELEASE ${OUTPUT_DIRECTORY}
    ARCHIVE_OUTPUT_DIRECTORY ${OUTPUT_DIRECTORY}
    ARCHIVE_OUTPUT_DIRECTORY_DEBUG ${OUTPUT_DIRECTORY}
    ARCHIVE_OUTPUT_DIRECTORY_RELEASE ${OUTPUT_DIRECTORY}                                                        
)

# Debian detection
set(IS_DEBIAN_LIKE FALSE)
if(EXISTS "/etc/os-release")
    file(READ "/etc/os-release" OS_RELEASE_CONTENTS)
    string(FIND "${OS_RELEASE_CONTENTS}" "ID=debian" DEBIAN_FOUND)
    string(FIND "${OS_RELEASE_CONTENTS}" "ID=ubuntu" UBUNTU_FOUND)
    if(NOT DEBIAN_FOUND EQUAL -1 OR NOT UBUNTU_FOUND EQUAL -1)
        set(IS_DEBIAN_LIKE TRUE)
    endif()
endif()

if(IS_DEBIAN_LIKE)
    message(STATUS "Detected Debian-like system. Installing Python module to: ${PYTHON_SITE_PACKAGES}")
    # Install rule
    install(TARGETS ${PYCAMITK_TARGET_LIB_NAME}
        LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/lib/python${Python_VERSION_MAJOR}/dist-packages/
    )
    # on other unix it might ${CMAKE_INSTALL_PREFIX}/lib/python${Python_VERSION_MAJOR}.${Python_VERSION_MINOR}/site-packages/${PYCAMITK_LIB_NAME}    
endif()

# CDash XML subproject description update
# This is required in order to add the target to the build during CI
# library-${PYCAMITK_LIB_NAME} is a corelib
camitk_register_subproject(CORELIB ${PYCAMITK_TARGET_LIB_NAME})

# -- unit testing
camitk_add_subdirectory(testing)

endif() # PYTHON_BINDING