set(DORA_ROOT_DIR "" CACHE FILEPATH "Path to the root of dora")

set(dora_c_include_dir "${CMAKE_CURRENT_BINARY_DIR}/include/c")

set(dora_cxx_include_dir "${CMAKE_CURRENT_BINARY_DIR}/include/cxx")
set(node_bridge "${CMAKE_CURRENT_BINARY_DIR}/node_bridge.cc")

if(DORA_ROOT_DIR AND EXISTS "${DORA_ROOT_DIR}/Cargo.toml")
    include(ExternalProject)
    ExternalProject_Add(Dora
        SOURCE_DIR ${DORA_ROOT_DIR}
        BUILD_IN_SOURCE True
        CONFIGURE_COMMAND ""
        BUILD_COMMAND
            cargo build
            --package dora-node-api-c
            &&
            cargo build
            --package dora-node-api-cxx
        INSTALL_COMMAND ""
    )

    add_custom_command(OUTPUT ${node_bridge} ${dora_cxx_include_dir} ${dora_c_include_dir}
        WORKING_DIRECTORY ${DORA_ROOT_DIR}
        DEPENDS Dora
        COMMAND
            mkdir ${dora_cxx_include_dir} -p
            &&
            mkdir ${CMAKE_CURRENT_BINARY_DIR}/include/c -p
            &&
            cp target/cxxbridge/dora-node-api-cxx/src/lib.rs.cc ${node_bridge}
            &&
            cp target/cxxbridge/dora-node-api-cxx/src/lib.rs.h ${dora_cxx_include_dir}/dora-node-api.h
            &&
            cp apis/c/node ${CMAKE_CURRENT_BINARY_DIR}/include/c -r
    )

    add_custom_target(Dora_c DEPENDS ${dora_c_include_dir})
    add_custom_target(Dora_cxx DEPENDS ${node_bridge} ${dora_cxx_include_dir})
    set(dora_link_dirs ${DORA_ROOT_DIR}/target/debug)
else()
    include(ExternalProject)
    set(DORA_GIT_TAG "main")
    find_program(DORA_CLI dora)
    if(DORA_CLI)
        execute_process(
            COMMAND ${DORA_CLI} --version
            RESULT_VARIABLE _dora_version_result
            OUTPUT_VARIABLE _dora_version_out
            ERROR_VARIABLE _dora_version_err
            OUTPUT_STRIP_TRAILING_WHITESPACE
        )
        if(_dora_version_result EQUAL 0)
            string(REGEX MATCH "([0-9]+\\.[0-9]+\\.[0-9]+)" _dora_version_match "${_dora_version_out}")
            if(_dora_version_match)
                set(DORA_GIT_TAG "v${CMAKE_MATCH_1}")
            endif()
        endif()
    endif()

    ExternalProject_Add(Dora
        PREFIX ${CMAKE_CURRENT_BINARY_DIR}/dora
        GIT_REPOSITORY https://github.com/dora-rs/dora.git
        GIT_TAG ${DORA_GIT_TAG}
        BUILD_IN_SOURCE True
        CONFIGURE_COMMAND ""
        BUILD_COMMAND
            cargo build
            --package dora-node-api-c
            --target-dir ${CMAKE_CURRENT_BINARY_DIR}/dora/src/Dora/target
            &&
            cargo build
            --package dora-node-api-cxx
            --target-dir ${CMAKE_CURRENT_BINARY_DIR}/dora/src/Dora/target
        INSTALL_COMMAND ""
    )

    add_custom_command(OUTPUT ${node_bridge} ${dora_cxx_include_dir} ${dora_c_include_dir}
        WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/dora/src/Dora/target
        DEPENDS Dora
        COMMAND
            mkdir ${CMAKE_CURRENT_BINARY_DIR}/include/c -p
            &&
            mkdir ${dora_cxx_include_dir} -p
            &&
            cp cxxbridge/dora-node-api-cxx/src/lib.rs.cc ${node_bridge}
            &&
            cp cxxbridge/dora-node-api-cxx/src/lib.rs.h ${dora_cxx_include_dir}/dora-node-api.h
            &&
            cp ../apis/c/node ${CMAKE_CURRENT_BINARY_DIR}/include/c -r
    )

    set(dora_link_dirs ${CMAKE_CURRENT_BINARY_DIR}/dora/src/Dora/target/debug)

    add_custom_target(Dora_c DEPENDS ${dora_c_include_dir})
    add_custom_target(Dora_cxx DEPENDS ${node_bridge} ${dora_cxx_include_dir})
endif()
