# MIT License
# Author: Mark Allyn
#
# CMakeLists.txt — Museum Vintage Radar Exhibit
#
# Build:
#   cd build && cmake .. && make -j$(nproc)
#
# Run (from project root):
#   ./build/radar

cmake_minimum_required(VERSION 3.16)
project(radar LANGUAGES C CXX)

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

# ----------------------------------------------------------------
# Find packages
# ----------------------------------------------------------------
find_package(OpenGL REQUIRED)
find_package(Freetype REQUIRED)
find_package(Threads REQUIRED)

# GLFW — prefer system package; fall back to find_library
find_package(PkgConfig QUIET)
if(PkgConfig_FOUND)
    pkg_check_modules(GLFW glfw3)
endif()
if(NOT GLFW_FOUND)
    find_library(GLFW_LIBRARIES NAMES glfw glfw3 REQUIRED)
    find_path(GLFW_INCLUDE_DIRS GLFW/glfw3.h REQUIRED)
endif()

# ----------------------------------------------------------------
# GLAD (compiled directly from source)
# ----------------------------------------------------------------
set(GLAD_SRC  ${CMAKE_SOURCE_DIR}/glad/src/glad.c)
set(GLAD_INC  ${CMAKE_SOURCE_DIR}/include)

# ----------------------------------------------------------------
# Source files — main radar binary
# ----------------------------------------------------------------
set(SOURCES
    src/main.cpp
    src/shared_render_state.cpp
    src/target_buffer.cpp
    src/phosphor.cpp
    src/graticule.cpp
    src/left_panel.cpp
    src/scope.cpp
    src/scope_manager.cpp
    src/scope_intro.cpp
    src/scope_ppi.cpp
    src/scope_marine_ppi.cpp
    src/simulator.cpp
    src/traffic_cop.cpp
    src/knob_panel.cpp
    src/rpi_receiver.cpp
    ${GLAD_SRC}
)

add_executable(radar ${SOURCES})

target_include_directories(radar PRIVATE
    src/
    ${GLAD_INC}
    ${FREETYPE_INCLUDE_DIRS}
    ${GLFW_INCLUDE_DIRS}
)

target_link_libraries(radar PRIVATE
    OpenGL::GL
    Freetype::Freetype
    Threads::Threads
    ${GLFW_LIBRARIES}
)

# Compiler warnings
target_compile_options(radar PRIVATE
    -Wall -Wextra -Wpedantic
    -Wno-unused-parameter
)

# ----------------------------------------------------------------
# terrain_preprocess — offline tool, links GDAL, NOT part of radar
# Uncomment and install libgdal-dev to build this target.
# ----------------------------------------------------------------
# find_package(GDAL)
# if(GDAL_FOUND)
#     add_executable(terrain_preprocess src/terrain_preprocess.cpp ${GLAD_SRC})
#     target_include_directories(terrain_preprocess PRIVATE src/ ${GLAD_INC} ${GDAL_INCLUDE_DIRS})
#     target_link_libraries(terrain_preprocess PRIVATE ${GDAL_LIBRARIES} Threads::Threads)
# endif()

# ----------------------------------------------------------------
# Copy shaders and data to build directory for in-build-dir running
# ----------------------------------------------------------------
add_custom_target(copy_assets ALL
    COMMAND ${CMAKE_COMMAND} -E copy_directory
        ${CMAKE_SOURCE_DIR}/shaders ${CMAKE_BINARY_DIR}/shaders
    COMMAND ${CMAKE_COMMAND} -E copy_directory
        ${CMAKE_SOURCE_DIR}/data ${CMAKE_BINARY_DIR}/data
    COMMENT "Copying shaders and data to build directory"
)
add_dependencies(radar copy_assets)
