cmake_minimum_required(VERSION 3.20)

project(location_plotter VERSION 0.3.0 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

set(CMAKE_AUTOMOC ON)

find_package(Qt6 REQUIRED COMPONENTS Core Widgets Network)
find_package(ZLIB REQUIRED)

add_executable(location_plotter
    src/LocationParser.cpp
    src/LocationParser.h
    src/GlobeWidget.cpp
    src/GlobeWidget.h
    src/MainWindow.cpp
    src/MainWindow.h
    src/MapWidget.cpp
    src/MapWidget.h
    src/SerialReader.cpp
    src/SerialReader.h
    src/UartReplay.cpp
    src/UartReplay.h
    src/RinexNavigation.cpp
    src/RinexNavigation.h
    src/SatelliteData.h
    src/TelemetryPoint.h
    src/main.cpp
)

target_link_libraries(location_plotter PRIVATE Qt6::Core Qt6::Widgets Qt6::Network ZLIB::ZLIB)
target_compile_definitions(location_plotter PRIVATE
    LOCATION_PLOTTER_SOURCE_DIR="${CMAKE_CURRENT_SOURCE_DIR}"
)

if(APPLE)
    set_target_properties(location_plotter PROPERTIES
        MACOSX_BUNDLE TRUE
        MACOSX_BUNDLE_BUNDLE_NAME "Location Plotter"
        MACOSX_BUNDLE_GUI_IDENTIFIER "com.ankasys.location-plotter"
        MACOSX_BUNDLE_BUNDLE_VERSION "${PROJECT_VERSION}"
        MACOSX_BUNDLE_SHORT_VERSION_STRING "${PROJECT_VERSION}"
    )
endif()

qt_add_resources(location_plotter "app_resources"
    PREFIX "/"
    FILES resources/ankasys-logo.png resources/land-110m.json
)

if(UNIX AND NOT APPLE)
    target_compile_definitions(location_plotter PRIVATE LOCATION_PLOTTER_LINUX_SERIAL=1)
endif()

enable_testing()

add_executable(location_parser_tests
    tests/parser_tests.cpp
    src/LocationParser.cpp
    src/LocationParser.h
    src/RinexNavigation.cpp
    src/RinexNavigation.h
    src/SatelliteData.h
    src/TelemetryPoint.h
)
target_link_libraries(location_parser_tests PRIVATE Qt6::Core Qt6::Network ZLIB::ZLIB)
target_compile_options(location_parser_tests PRIVATE
    $<$<CXX_COMPILER_ID:GNU,Clang,AppleClang>:-UNDEBUG>
)
add_test(NAME location_parser_tests COMMAND location_parser_tests)

add_executable(location_main_window_tests
    tests/main_window_tests.cpp
    src/LocationParser.cpp
    src/LocationParser.h
    src/MainWindow.cpp
    src/MainWindow.h
    src/MapWidget.cpp
    src/MapWidget.h
    src/GlobeWidget.cpp
    src/GlobeWidget.h
    src/SerialReader.cpp
    src/SerialReader.h
    src/UartReplay.cpp
    src/UartReplay.h
    src/RinexNavigation.cpp
    src/RinexNavigation.h
    src/SatelliteData.h
    src/TelemetryPoint.h
)
target_link_libraries(location_main_window_tests PRIVATE Qt6::Core Qt6::Widgets Qt6::Network ZLIB::ZLIB)
target_compile_definitions(location_main_window_tests PRIVATE
    LOCATION_PLOTTER_SOURCE_DIR="${CMAKE_CURRENT_SOURCE_DIR}"
)
target_compile_options(location_main_window_tests PRIVATE
    $<$<CXX_COMPILER_ID:GNU,Clang,AppleClang>:-UNDEBUG>
)
qt_add_resources(location_main_window_tests "test_app_resources"
    PREFIX "/"
    FILES resources/ankasys-logo.png resources/land-110m.json
)
if(UNIX AND NOT APPLE)
    target_compile_definitions(location_main_window_tests PRIVATE LOCATION_PLOTTER_LINUX_SERIAL=1)
endif()
add_test(NAME location_main_window_tests COMMAND location_main_window_tests)
set_tests_properties(location_main_window_tests PROPERTIES
    ENVIRONMENT "QT_QPA_PLATFORM=offscreen;XDG_CACHE_HOME=/tmp/location-plotter-test-cache")

add_executable(location_uart_replay_tests
    tests/uart_replay_tests.cpp
    src/UartReplay.cpp
    src/UartReplay.h
    src/LocationParser.cpp
    src/LocationParser.h
    src/TelemetryPoint.h
)
target_link_libraries(location_uart_replay_tests PRIVATE Qt6::Core)
target_compile_options(location_uart_replay_tests PRIVATE
    $<$<CXX_COMPILER_ID:GNU,Clang,AppleClang>:-UNDEBUG>
)
add_test(NAME location_uart_replay_tests COMMAND location_uart_replay_tests)

install(TARGETS location_plotter
    BUNDLE DESTINATION .
    RUNTIME DESTINATION bin
)
