# ============================================================================
# Copyright 2017-2022, Schlumberger
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================

# HTTP REQUEST TEST
add_executable(http_request http_request.cc ../shared/utils.cc)
target_link_libraries(http_request ${CURL_LIBRARY} ${CMAKE_DL_LIBS})

# GCS ACCESSOR TEST
add_executable(gcs_accessor gcs_accessor.cc ../shared/utils.cc)
if (NOT CRC32C_FROM_VCPKG)
    add_dependencies(gcs_accessor crc32c)
endif()
target_include_directories(gcs_accessor PRIVATE ${CRC32CLIB_DIR}/include)
target_link_libraries(gcs_accessor ${CURL_LIBRARY} ${OPENSSL_LIBRARY} ${CRC32C_LIBRARY} ${CMAKE_DL_LIBS})


if (IBM_PROVIDER_ENABLED)

    find_package(Crc32c)
    set(SOURCES ibm_storage.cc
                ../shared/utils.cc)

    add_executable(ibm_storage ${SOURCES})

    target_include_directories(ibm_storage PRIVATE ${AWSSDK_INCLUDE_DIR} Crc32c::crc32c)

    # explictly remove all other providers from this test...
    # this is a hack, I should be retrieving and removing all the -D from the target compile definitions
    target_compile_options(ibm_storage PRIVATE -UHAS_GCS_STORAGE_PROVIDER -UHAS_AZURE_BLOB_STORAGE_PROVIDER_CURL -UHAS_AZURE_BLOB_STORAGE_PROVIDER -UHAS_AWS_S3_STORAGE_PROVIDER)
    target_link_libraries(ibm_storage ${AWSSDK_LINK_LIBRARIES} ${AWSSDK_PLATFORM_DEPS}
                                      ${CURL_LIBRARY} ${OPENSSL_LIBRARY} ${CMAKE_DL_LIBS}
                                      Crc32c::crc32c)

    add_executable(ibm_storage_static ibm_storage.cc)
    target_link_libraries(ibm_storage_static ${AWSSDK_LINK_LIBRARIES} ${AWSSDK_PLATFORM_DEPS})

endif(IBM_PROVIDER_ENABLED)


if (ANTHOS_PROVIDER_ENABLED)

    find_package(Crc32c)
    set(SOURCES anthos_storage.cc
                ../shared/utils.cc)

    add_executable(anthos_storage ${SOURCES})

    target_include_directories(anthos_storage PRIVATE ${AWSSDK_INCLUDE_DIR} Crc32c::crc32c)

    # explictly remove all other providers from this test...
    # this is a hack, I should be retrieving and removing all the -D from the target compile definitions
    target_compile_options(anthos_storage PRIVATE -UHAS_GCS_STORAGE_PROVIDER -UHAS_AZURE_BLOB_STORAGE_PROVIDER_CURL -UHAS_AZURE_BLOB_STORAGE_PROVIDER -UHAS_AWS_S3_STORAGE_PROVIDER)
    target_link_libraries(anthos_storage ${AWSSDK_LINK_LIBRARIES} ${AWSSDK_PLATFORM_DEPS}
                                      ${CURL_LIBRARY} ${OPENSSL_LIBRARY} ${CMAKE_DL_LIBS}
                                      Crc32c::crc32c)

    add_executable(anthos_storage_static anthos_storage.cc)
    target_link_libraries(anthos_storage_static ${AWSSDK_LINK_LIBRARIES} ${AWSSDK_PLATFORM_DEPS})

endif(ANTHOS_PROVIDER_ENABLED)


# AZURE STORAGE TEST
if (AZURE_PROVIDER_ENABLED OR AZURE_CURL_PROVIDER_ENABLED)
    set(SOURCES azure_storage.cc ../shared/utils.cc)

    add_executable(azure_storage ${SOURCES})

    target_compile_options(azure_storage PRIVATE -UHAS_GCS_STORAGE_PROVIDER -UHAS_AWS_S3_STORAGE_PROVIDER -UHAS_IBM_STORAGE_PROVIDER)

    # COMMON LINKING DEPENDENCIES
    target_link_libraries(azure_storage ${CURL_LIBRARY}
                                        ${OPENSSL_LIBRARY}
                                        ${CRC32C_LIBRARY})

    if (NOT WIN32)
        # LINUX

        # COMMON LINKING DEPENDENCIES
        target_link_libraries(azure_storage ${CMAKE_DL_LIBS})

        # AZURE CPPREST SPECIFIC DEPENDENCIES
        if (AZURE_PROVIDER_ENABLED)
            target_link_libraries(azure_storage ${WASTORAGE_LIBRARY}
                                                ${BOOST_LOG_LIBRARY}
                                                ${BOOST_THREAD_LIBRARY}
                                                ${SSL_LIBRARY}
                                                ${CRYPTO_LIBRARY}
                                                ${XML2_LIBRARY}
                                                ${LZMA_LIBRARY}
                                                ${UUID_LIBRARY}
                                                ${Z_LIBRARY})
            if (cpprestsdk_FOUND)
                target_include_directories(azure_storage PRIVATE cpprestsdk::cpprest)
                target_link_libraries(azure_storage cpprestsdk::cpprest)
            else()
                target_link_libraries(azure_storage ${CPPREST_LIBRARY})
            endif()
        endif()

        # AZURE CURL SPECIFIC DEPENDENCIES
        if (AZURE_CURL_PROVIDER_ENABLED)
            target_link_libraries(azure_storage Azure::azure-storage-blobs
                                                LibXml2::LibXml2)
        endif()

    else()
        # WINDOWS

        # AZURE CPPREST SPECIFIC DEPENDENCIES
        if (AZURE_PROVIDER_ENABLED)
            target_link_libraries(azure_storage ${WASTORAGE_LIBRARY}
                                                ${CPPREST_LIBRARY})
        endif()

        # AZURE CURL SPECIFIC DEPENDENCIES
        if (AZURE_CURL_PROVIDER_ENABLED)
            target_link_libraries(azure_storage Azure::azure-storage-blobs)
        endif()
    endif()
endif()

if (AWS_PROVIDER_ENABLED)
    find_package(Crc32c)
    set(SOURCES aws_storage.cc)

    add_executable(aws_storage ${SOURCES})

    target_include_directories(aws_storage PRIVATE ${AWSSDK_INCLUDE_DIR} Crc32c::crc32c)

    # explictly remove all other providers from this test...
    # this is a hack, I should be retrieving and removing all the -D from the target compile definitions
    target_compile_options(aws_storage PRIVATE -UHAS_GCS_STORAGE_PROVIDER -UHAS_AZURE_BLOB_STORAGE_PROVIDER_CURL -UHAS_AZURE_BLOB_STORAGE_PROVIDER -UHAS_IBM_STORAGE_PROVIDER)
    target_link_libraries(aws_storage ${AWSSDK_LINK_LIBRARIES} ${AWSSDK_PLATFORM_DEPS}
                                      ${CURL_LIBRARY} ${OPENSSL_LIBRARY} ${CMAKE_DL_LIBS}
                                      Crc32c::crc32c)

    add_executable(aws_storage_static aws_storage.cc)
    target_link_libraries(aws_storage_static ${AWSSDK_LINK_LIBRARIES} ${AWSSDK_PLATFORM_DEPS})

endif(AWS_PROVIDER_ENABLED)

if (GCS_PROVIDER_ENABLED)

    add_executable(gcp_storage gcp_storage.cc)

    target_compile_options(gcp_storage PRIVATE -UHAS_AWS_S3_STORAGE_PROVIDER -UHAS_AZURE_BLOB_STORAGE_PROVIDER_CURL -UHAS_AZURE_BLOB_STORAGE_PROVIDER -UHAS_IBM_STORAGE_PROVIDER)


    target_include_directories(gcp_storage PRIVATE storage_client)
    # FIXME: this should be a static library (with pic) built as a CMake package.
    # instead of a
    #add_dependencies(gcp_storage)
    target_link_libraries(gcp_storage storage_client)

endif(GCS_PROVIDER_ENABLED)

# GCS XML 2 JSON
add_executable(gcs_xml_vs_json gcs_xml_vs_json.cc ../shared/utils.cc)
if (NOT CRC32C_FROM_VCPKG)
    add_dependencies(gcs_xml_vs_json crc32c)
endif()
target_include_directories(gcs_xml_vs_json PRIVATE ${CRC32CLIB_DIR}/include)
target_link_libraries(gcs_xml_vs_json ${CURL_LIBRARY} ${OPENSSL_LIBRARY} ${CRC32C_LIBRARY} ${CMAKE_DL_LIBS})

# GCS BATCH MULTITHREAD
add_executable(gcs_batch_multithread gcs_batch_multithread.cc ../shared/utils.cc)
if (NOT CRC32C_FROM_VCPKG)
  add_dependencies(gcs_batch_multithread crc32c)
endif()
if(NOT WIN32)
    target_link_libraries(gcs_batch_multithread ${CURL_LIBRARY} ${OPENSSL_LIBRARY} ${CRC32C_LIBRARY} ${CMAKE_DL_LIBS} pthread)
else()
    target_link_libraries(gcs_batch_multithread ${CURL_LIBRARY} ${OPENSSL_LIBRARY} ${CRC32C_LIBRARY})
endif()

# AUTH PROVIDER TEST
add_executable(auth_provider auth_provider.cc ../shared/utils.cc)
target_link_libraries(auth_provider ${CURL_LIBRARY} ${OPENSSL_LIBRARY} ${CMAKE_DL_LIBS})

# All targets
get_property(targets DIRECTORY ${dir} PROPERTY BUILDSYSTEM_TARGETS)
foreach(target ${targets})
    target_link_libraries(${target} sdapi_static)
    set_target_properties(${target} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin/seismic-store-lib)
endforeach()
