if(POLICY CMP0072)
  cmake_policy(SET CMP0072 NEW)
endif()

if(POLICY CMP0074)
  cmake_policy(SET CMP0074 NEW)
endif()

find_package(Threads REQUIRED)

if(UNIX)
    find_package(DL REQUIRED)
endif()

include(FetchContent)

# find a boost install with the libraries unit_test_framework
if(WIN32)
    set(Boost_USE_STATIC_LIBS ON CACHE STRING "Boost_USE_STATIC_LIBS" FORCE)
    set(Boost_USE_STATIC_RUNTIME ON CACHE STRING "Boost_USE_STATIC_RUNTIME" FORCE)
    set(BOOST_PYTHON_STATIC_LIB ON CACHE STRING "BOOST_PYTHON_STATIC_LIB" FORCE)
endif()

find_package(Boost 1.65.1 REQUIRED COMPONENTS serialization thread log log_setup system program_options filesystem coroutine locale regex unit_test_framework)
if(Boost_FOUND)
    message(STATUS "** Boost Include: ${Boost_INCLUDE_DIR}")
    message(STATUS "** Boost Libraries Directory: ${Boost_LIBRARY_DIRS}")
    message(STATUS "** Boost Libraries: ${Boost_LIBRARIES}")
    include_directories(${Boost_INCLUDE_DIRS})
else()
    if(WIN32)
        message(FATAL_ERROR "Plase check your vcpkg settings or global environment variables for the boost library.")
    else()
        FetchContent_Declare(boost_cmake
            GIT_REPOSITORY https://github.com/Orphis/boost-cmake.git
            GIT_TAG d3951bc7f0b9d09005f92aedcf6acfc595f050ea)

        FetchContent_GetProperties(boost_cmake)
        if(NOT boost_cmake_POPULATED)
            FetchContent_Populate(boost_cmake)
            add_subdirectory(${boost_cmake_SOURCE_DIR} ${boost_cmake_BINARY_DIR} EXCLUDE_FROM_ALL)
        endif()
    endif()
endif()

# Eigen3
find_package(Eigen3 QUIET)
if(NOT EIGEN3_FOUND)
    FetchContent_Declare(eigen
        GIT_REPOSITORY https://gitlab.com/libeigen/eigen.git
        GIT_TAG dc252fbf00079ccab57948a164b1421703fe4361)

    FetchContent_GetProperties(eigen)
    if(NOT eigen_POPULATED)
        FetchContent_Populate(eigen)
        add_subdirectory(${eigen_SOURCE_DIR} ${eigen_BINARY_DIR} EXCLUDE_FROM_ALL)
        set(EIGEN3_INCLUDE_DIR "${eigen_SOURCE_DIR}" CACHE STRING "EIGEN3_INCLUDE_DIR" FORCE)
    endif()
endif()

# DyNet
find_package(DyNet QUIET)
if(NOT DYNET_FOUND)
    FetchContent_Declare(dynet
        GIT_REPOSITORY https://github.com/clab/dynet.git
        GIT_TAG 363a8cc60d3907d4d01d7f05f18c076b3fe79b92)

    FetchContent_GetProperties(dynet)
    if(NOT dynet_POPULATED)
        FetchContent_Populate(dynet)
        add_subdirectory(${dynet_SOURCE_DIR} ${dynet_BINARY_DIR} EXCLUDE_FROM_ALL)
        set(TH_INCLUDE_DIR "${dynet_SOURCE_DIR}" CACHE STRING "TH_INCLUDE_DIR" FORCE)

        if(BACKEND MATCHES "^eigen$")
            set(DYNET_LIB "dynet" CACHE STRING "DYNET_LIB" FORCE)
            set(WITH_CUDA_BACKEND 0 CACHE STRING "WITH_CUDA_BACKEND" FORCE)
        elseif(BACKEND MATCHES "^cuda$")
            set(DYNET_LIB "gdynet" CACHE STRING "DYNET_LIB" FORCE)
            set(WITH_CUDA_BACKEND 1 CACHE STRING "WITH_CUDA_BACKEND" FORCE)
        else()
            message(SEND_ERROR "BACKEND must be eigen or cuda. Current value is: ${BACKEND}.")
        endif()
    endif()
else()
    set(TH_INCLUDE_DIR "${DYNET_INCLUDE_DIRS}" CACHE STRING "TH_INCLUDE_DIR" FORCE)

    if(BACKEND MATCHES "^eigen$")
        set(DYNET_LIB "${DYNET_LIBRARIES}" CACHE STRING "DYNET_LIB" FORCE)
        set(WITH_CUDA_BACKEND 0 CACHE STRING "WITH_CUDA_BACKEND" FORCE)
    elseif(BACKEND MATCHES "^cuda$")
        set(DYNET_LIB "${DYNET_LIBRARIES}" CACHE STRING "DYNET_LIB" FORCE)
        set(WITH_CUDA_BACKEND 1 CACHE STRING "WITH_CUDA_BACKEND" FORCE)
    else()
        message(SEND_ERROR "BACKEND must be eigen or cuda. Current value is: ${BACKEND}.")
    endif()
endif()

if(WIN32)
    # unistd_h
    FetchContent_Declare(unistd_h
        GIT_REPOSITORY https://github.com/win32ports/unistd_h.git
        GIT_TAG master)

    FetchContent_GetProperties(unistd_h)
    if(NOT unistd_h_POPULATED)
        FetchContent_Populate(unistd_h)
        # add_subdirectory(${unistd_h_SOURCE_DIR} ${unistd_h_BINARY_DIR} EXCLUDE_FROM_ALL)
        include_directories(${unistd_h_SOURCE_DIR})
    endif()
endif()