cmake_minimum_required(VERSION 2.8.12)

project(atomorph-download NONE)

include(ExternalProject)
ExternalProject_Add(opencv
        GIT_REPOSITORY https://github.com/opencv/opencv.git
        GIT_TAG 4.x
        PREFIX "${CMAKE_CURRENT_BINARY_DIR}/opencv"
        CONFIGURE_COMMAND ""
        BUILD_COMMAND ""
        INSTALL_COMMAND ""
        TEST_COMMAND ""
        )

function(include_opencv)
    configure_file(CMakeLists.txt.in opencv/download/CMakeLists.txt)
    execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" . -DBUILD_SHARED_LIBS:BOOL=OFF
            RESULT_VARIABLE result
            WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/opencv/download)
    if (result)
        message(FATAL_ERROR "CMake step for opencv failed: ${result}")
    endif ()
    execute_process(COMMAND ${CMAKE_COMMAND} --build .
            RESULT_VARIABLE result
            WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/opencv/download)
    if (result)
        message(FATAL_ERROR "Build step for opencv failed: ${result}")
    endif ()

    add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/opencv/src/opencv
            ${CMAKE_CURRENT_BINARY_DIR}/opencv/bin
            EXCLUDE_FROM_ALL)

    set( OPENCV_ROOT_DIR ${CMAKE_BINARY_DIR}/INSTALL )
    set( OPENCV_DIR ${CMAKE_BINARY_DIR}/INSTALL )
endfunction()