cmake_minimum_required(VERSION 3.18)
project(pDynamoModernized LANGUAGES C)

if(CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR)
    message(FATAL_ERROR
        "In-source builds are not supported. "
        "Create a build directory and run CMake from there, for example: "
        "mkdir build && cd build && cmake .."
    )
endif()

enable_testing()

list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
find_package(Python3 REQUIRED COMPONENTS Interpreter)
include(PDynamoBuildOptions)
include(PDynamoInstall)

pd_configure_build_options()
pd_configure_install_layout()

# -----------------------------------------------------------------------------
# Runtime parameter directory.
# Source tree layout:
#   ${CMAKE_SOURCE_DIR}/pCore-2.0.0
#   ${CMAKE_SOURCE_DIR}/pMolecule-2.0.0
#   ${CMAKE_SOURCE_DIR}/parameters
# -----------------------------------------------------------------------------
set(PDYNAMO_PARAMETERS_SOURCE_DIR "${CMAKE_SOURCE_DIR}/parameters")
set(PDYNAMO_PARAMETERS_BUILD_DIR  "${CMAKE_BINARY_DIR}/parameters")

if(NOT EXISTS "${PDYNAMO_PARAMETERS_SOURCE_DIR}")
    message(FATAL_ERROR "parameters directory not found: ${PDYNAMO_PARAMETERS_SOURCE_DIR}")
endif()

# Copy parameters into build runtime tree.
file(COPY "${PDYNAMO_PARAMETERS_SOURCE_DIR}/"
     DESTINATION "${PDYNAMO_PARAMETERS_BUILD_DIR}")

file(MAKE_DIRECTORY "${CMAKE_BINARY_DIR}/python")
configure_file(
    "${CMAKE_SOURCE_DIR}/pDynamoBootstrap.py"
    "${CMAKE_BINARY_DIR}/python/pDynamoBootstrap.py"
    COPYONLY
)

message(STATUS "PDYNAMO_PARAMETERS source: ${PDYNAMO_PARAMETERS_SOURCE_DIR}")
message(STATUS "PDYNAMO_PARAMETERS build : ${PDYNAMO_PARAMETERS_BUILD_DIR}")

# -----------------------------------------------------------------------------
# Subdirectories.
# -----------------------------------------------------------------------------
add_subdirectory(pCore-2.0.0)
add_subdirectory(pGraph-2.0.0)
set(PCORE_LIBS_FOR_PMolecule "${PCORE_LIBS}")
add_subdirectory(pMolecule-2.0.0)
add_subdirectory(pBabel-2.0.0)
add_subdirectory(pMoleculeScripts-2.0.0)

# -----------------------------------------------------------------------------
# Test environment.
# -----------------------------------------------------------------------------

install(
    DIRECTORY "${PDYNAMO_PARAMETERS_SOURCE_DIR}/"
    DESTINATION "${PDYNAMO_INSTALL_PARAMETERS_DIR}"
)

pd_install_root_python_files(
    "${CMAKE_SOURCE_DIR}/pDynamoBootstrap.py"
)

if(NOT PDYNAMO_INSTALL_LAYOUT STREQUAL "wheel")
    install(
        FILES "${CMAKE_SOURCE_DIR}/cmake/pdynamo-activate.sh"
        DESTINATION "${PDYNAMO_INSTALL_CONDA_ACTIVATE_DIR}"
        RENAME "pdynamo.sh"
    )

    install(
        FILES "${CMAKE_SOURCE_DIR}/cmake/pdynamo-deactivate.sh"
        DESTINATION "${PDYNAMO_INSTALL_CONDA_DEACTIVATE_DIR}"
        RENAME "pdynamo.sh"
    )
endif()
