if(NOT DEFINED PYTHON_EXE)
    set(PYTHON_EXE "python")
endif()

option(INSTALL_PYTHON_CLIENT "Install the Python client package")

file(GLOB_RECURSE PYTHON_SOURCES "${CMAKE_SOURCE_DIR}/src/python/*.py")

add_custom_command(
    OUTPUT "${CMAKE_BINARY_DIR}/python_package.stamp"
    COMMAND "${CMAKE_COMMAND}" -E env "${PYTHON_EXE}" ./setup.py sdist
    COMMAND "${CMAKE_COMMAND}" -E touch "${CMAKE_BINARY_DIR}/python_package.stamp"
    DEPENDS "${PYTHON_SOURCES}"
    WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/src/python"
    COMMENT "Building Python client package"
)

add_custom_target(build_python_package ALL
    DEPENDS ${CMAKE_BINARY_DIR}/python_package.stamp
)

if(INSTALL_PYTHON_CLIENT)
    add_custom_target(install_python_package ALL
        COMMAND "${CMAKE_COMMAND}" -E env "${PYTHON_EXE}" -m pip install "${CMAKE_SOURCE_DIR}/src/python/dist/graph*"
        COMMENT "Insatlling Python client package"
    )

    add_dependencies(install_python_package build_python_package)
endif()