option (ENABLE_GRPC "Use gRPC" ${ENABLE_LIBRARIES})

if (NOT ENABLE_GRPC)
    if (USE_INTERNAL_GRPC_LIBRARY)
        message (${RECONFIGURE_MESSAGE_LEVEL} "Cannot use internal gRPC library with ENABLE_GRPC=OFF")
    endif()
    return()
endif()

option (USE_INTERNAL_GRPC_LIBRARY "Set to FALSE to use system gRPC library instead of bundled" ${NOT_UNBUNDLED})

if (NOT USE_INTERNAL_GRPC_LIBRARY)
    find_package(grpc)
    if (GRPC_INCLUDE_DIR AND GRPC_LIBRARY)
        set (USE_GRPC ON)
    else()
        message (${RECONFIGURE_MESSAGE_LEVEL} "Can't find system gRPC")
    endif()
endif()

if (NOT USE_GRPC)
    if (NOT EXISTS "${ClickHouse_SOURCE_DIR}/contrib/grpc/include/grpc++/grpc++.h")
        message (WARNING "submodule contrib/grpc is missing. To fix try run: \n git submodule update --init --recursive")
        message (${RECONFIGURE_MESSAGE_LEVEL} "Can't find internal gRPC")
        set (USE_INTERNAL_GRPC_LIBRARY OFF)
    elseif (NOT USE_PROTOBUF)
        message (WARNING "gRPC requires protobuf which is disabled")
        message (${RECONFIGURE_MESSAGE_LEVEL} "Will not use internal gRPC without protobuf")
        set (USE_INTERNAL_GRPC_LIBRARY OFF)
    else()
        set (GRPC_INCLUDE_DIR "${ClickHouse_SOURCE_DIR}/contrib/grpc/include")
        set (GRPC_LIBRARY "libgrpc++")
        set (USE_GRPC ON)
        set (USE_INTERNAL_GRPC_LIBRARY ON)
    endif()
endif()

message (STATUS "Using gRPC=${USE_GRPC}: ${GRPC_INCLUDE_DIR} : ${GRPC_LIBRARY}")
