set(PACKAGE data_converters-protox)

message(STATUS ${PACKAGE})

include(FindProtobuf)
include(FetchContent)
find_package(Protobuf REQUIRED)
find_package(gRPC REQUIRED)
find_package(vaisala-wr-common-dto REQUIRED)
find_package(vaisala-wr-input-dto REQUIRED)

FetchContent_Declare(
  googletest
  GIT_REPOSITORY https://github.com/google/googletest.git
  GIT_TAG release-1.12.1)
FetchContent_MakeAvailable(googletest)


set(APP_NAMES protox-input-dto protox-common-dto)
set(SCRIPTS protox)

set(PROTOX_INPUT_DTO_SOURCE_FILES protox_input_dto.C protox_input_dto.hpp)
set(PROTOX_COMMON_DTO_SOURCE_FILES protox_common_dto.C protox_common_dto.hpp)

add_subdirectory(templates)

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../lib)

link_libraries(misc-iris himath-iris user-iris dl m bsd boost_regex dspi-iris dsp-iris exthdr-iris irisscan-iris sws-iris wrdata tirpc antenna-iris config-iris)

add_definitions(-DINPUT_DTO_TEST_FILE=\"${CMAKE_CURRENT_SOURCE_DIR}/test_files/test_input_dto.pb\")
add_definitions(-DCOMMON_DTO_TEST_FILE=\"${CMAKE_CURRENT_SOURCE_DIR}/test_files/test_common_dto.pb\")

add_executable(protox-input-dto protox_input_dto_main.C ${PROTOX_INPUT_DTO_SOURCE_FILES})
add_executable(protox-input-dto-tests protox_input_dto_tests.C protox_input_dto_tests.hpp ${PROTOX_INPUT_DTO_SOURCE_FILES})
add_executable(protox-common-dto protox_common_dto_main.C ${PROTOX_COMMON_DTO_SOURCE_FILES})
add_executable(protox-common-dto-tests protox_common_dto_tests.C protox_common_dto_tests.hpp ${PROTOX_COMMON_DTO_SOURCE_FILES})

target_link_libraries(protox-input-dto vaisala-wr-input-dto::input-dto ${PROTOBUF_LIBRARIES} boost_program_options boost_filesystem boost_system boost_date_time lz4 rscan-iris)
target_link_libraries(protox-common-dto vaisala-wr-common-dto::common-dto ${PROTOBUF_LIBRARIES} boost_program_options boost_filesystem boost_system boost_date_time rscan-iris lz4)

target_link_libraries(protox-input-dto-tests vaisala-wr-input-dto::input-dto ${PROTOBUF_LIBRARIES} boost_program_options boost_filesystem boost_system boost_date_time lz4 rscan-iris GTest::gtest_main)
target_link_libraries(protox-common-dto-tests vaisala-wr-common-dto::common-dto ${PROTOBUF_LIBRARIES} boost_program_options boost_filesystem boost_system boost_date_time lz4 rscan-iris GTest::gtest_main)

include(GoogleTest)
gtest_discover_tests(protox-input-dto-tests)
gtest_discover_tests(protox-common-dto-tests)

enable_testing()

file(DOWNLOAD
        ${CMAKE_CURRENT_SOURCE_DIR}/test_files/test_input_dto.pb
        ${CMAKE_CURRENT_BINARY_DIR}/test_input_dto.pb)

file(DOWNLOAD
        ${CMAKE_CURRENT_SOURCE_DIR}/test_files/test_common_dto.pb
        ${CMAKE_CURRENT_BINARY_DIR}/test_common_dto.pb)        

add_custom_target(
  protox-tests ALL
  DEPENDS protox-input-dto-tests protox-common-dto-tests
  COMMENT "Run protox-(input/common)-dto tests"
  COMMAND ctest --test-dir ${CMAKE_CURRENT_BINARY_DIR})

install(
  TARGETS ${APP_NAMES}
  DESTINATION ${IRIS_BIN}
  COMPONENT protobuf)
  
install(
  PROGRAMS ${SCRIPTS}
  DESTINATION ${IRIS_BIN}
  COMPONENT protobuf)
