#  Copyright (C) GridGain Systems. All Rights Reserved.
#  _________        _____ __________________        _____
#  __  ____/___________(_)______  /__  ____/______ ____(_)_______
#  _  / __  __  ___/__  / _  __  / _  / __  _  __ `/__  / __  __ \
#  / /_/ /  _  /    _  /  / /_/ /  / /_/ /  / /_/ / _  /  _  / / /
#  \____/   /_/     /_/   \_,__/   \____/   \__,_/  /_/   /_/ /_/

cmake_minimum_required(VERSION 3.18)

project(gridgain_package_test)

set(CMAKE_CXX_STANDARD 17)

option(ENABLE_CLIENT "Build GridGain.C++ Client module" OFF)
option(ENABLE_ODBC "Build GridGain ODBC driver module" OFF)

set(IGNITE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../")
set(IGNITE_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/cmake_package")

execute_process(
    COMMAND "${CMAKE_COMMAND}"
    "-H${IGNITE_SOURCE_DIR}"
    "-B${IGNITE_BINARY_DIR}"
    "-DENABLE_ODBC=${ENABLE_ODBC}"
    "-DENABLE_CLIENT=${ENABLE_CLIENT}"
    RESULT_VARIABLE CONFIGURE_RESULT
    ERROR_VARIABLE CONFIGURE_ERROR
)

if(NOT CONFIGURE_RESULT EQUAL 0)
    message(FATAL_ERROR "CMake configure step failed: ${CONFIGURE_ERROR}")
endif()

execute_process(
    COMMAND "${CMAKE_COMMAND}"
        --build "${IGNITE_BINARY_DIR}"
    RESULT_VARIABLE BUILD_RESULT
    ERROR_VARIABLE BUILD_ERROR
)

if(NOT BUILD_RESULT EQUAL 0)
    message(FATAL_ERROR "CMake build step failed: ${BUILD_ERROR}")
endif()

set(GRIDGAIN_COMPONENTS "")
if(ENABLE_CLIENT)
    list(APPEND GRIDGAIN_COMPONENTS client)
endif()
if(ENABLE_ODBC)
    list(APPEND GRIDGAIN_COMPONENTS odbc)
endif()

set(gridgain_DIR "${IGNITE_BINARY_DIR}/cmake/")
find_package(gridgain REQUIRED COMPONENTS ${GRIDGAIN_COMPONENTS})

if(NOT gridgain_FOUND)
    message(FATAL_ERROR "GridGain is not found!")
endif()

if(ENABLE_CLIENT)
    if(NOT gridgain_client_FOUND)
        message(FATAL_ERROR "GridGain client component is not found!")
    endif()
    add_executable(test_client ${CMAKE_CURRENT_LIST_DIR}/../test_client.cpp)
    target_link_libraries(test_client gridgain::client)
endif()

if(ENABLE_ODBC)
    if(NOT gridgain_odbc_FOUND)
        message(FATAL_ERROR "GridGain odbc component is not found!")
    endif()
    add_executable(test_odbc ${CMAKE_CURRENT_LIST_DIR}/../test_odbc.cpp)
    target_link_libraries(test_odbc gridgain::odbc)
endif()
