# Copyright 2024 Proyectos y Sistemas de Mantenimiento SL (eProsima).
#
# 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.

message(STATUS "Configuring docker based communication tests")

# Find docker
find_program(DOCKER_EXECUTABLE docker)
if(NOT DOCKER_EXECUTABLE)
    message(WARNING "Docker executable not found. Docker based communication tests won't be compiled.")
    return()
endif()

set(SHELL_EXECUTABLE "")

# Linux configurations
if(UNIX AND NOT(APPLE) AND NOT(QNXNTO) AND NOT(ANDROID))
    # Find bash
    find_program(BASH_EXECUTABLE bash)
    if(NOT BASH_EXECUTABLE)
        message(WARNING "bash executable not found. Docker based communication tests won't be compiled.")
        return()
    endif()

    set(SHELL_EXECUTABLE ${BASH_EXECUTABLE})

# Windows configurations
elseif(WIN32)
    # We don't know which docker image to use for Windows yet
    message(WARNING "Windows not supported yet. Docker based communication tests won't be compiled.")
    return()
# Unsupported platform
else()
    message(WARNING "Unsupported platform. Docker based communication tests won't be compiled.")
    return()
endif()

configure_file(cli.Dockerfile
               ${CMAKE_CURRENT_BINARY_DIR}/cli.Dockerfile @ONLY)
configure_file(ds_init_no_interfaces.compose.yml
         ${CMAKE_CURRENT_BINARY_DIR}/ds_init_no_interfaces.compose.yml @ONLY)
configure_file(launch_ds_no_interfaces.bash
         ${CMAKE_CURRENT_BINARY_DIR}/launch_ds_no_interfaces.bash @ONLY)
add_test(NAME dds.communication.ds_init_with_no_interfaces
         COMMAND ${DOCKER_EXECUTABLE} compose -f ${CMAKE_CURRENT_BINARY_DIR}/ds_init_no_interfaces.compose.yml up --exit-code-from server-pub-sub
         WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
