cmake_minimum_required(VERSION 3.10)
project(Sensor3D_Cpp_Examples CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Required Sensor3D SDK version, read from CORE_VERSION.txt.
file(STRINGS "${CMAKE_SOURCE_DIR}/CORE_VERSION.txt" SENSOR3D_REQUIRED_VERSION)

# Allows building directly from an SDK package, where this examples/ folder
# sits next to the SDK's cmake/ folder, without passing CMAKE_PREFIX_PATH.
list(APPEND CMAKE_PREFIX_PATH "${CMAKE_SOURCE_DIR}/..")

option(SENSOR3D_EXAMPLES_FORCE_FETCH_CORE_REDIST
    "Always fetch the core-redistributable via FetchContent, even if a compatible SDK is found locally" OFF)

if(SENSOR3D_EXAMPLES_FORCE_FETCH_CORE_REDIST)
    set(Sensor3D_FOUND FALSE)
else()
    find_package(Sensor3D ${SENSOR3D_REQUIRED_VERSION} QUIET)
endif()

if(Sensor3D_FOUND)
    message(STATUS "Using locally installed Sensor3D SDK ${Sensor3D_VERSION}")
else()
    message(STATUS "No compatible local Sensor3D SDK found -- fetching the core-redistributable")

    if(WIN32)
        set(_s3d_os "windows")
        set(_s3d_ext "zip")
    else()
        set(_s3d_os "linux")
        set(_s3d_ext "tar.gz")
    endif()
    set(_s3d_arch "x64")
    set(_s3d_asset "sensor3d-core-${_s3d_os}-${_s3d_arch}-${SENSOR3D_REQUIRED_VERSION}.${_s3d_ext}")

    set(SENSOR_3D_CORE_REDIST_URL "" CACHE STRING
        "Base URL of the sensor3d-core-redist repository releases are fetched from")

    if(NOT SENSOR_3D_CORE_REDIST_URL)
        message(FATAL_ERROR
            "SENSOR_3D_CORE_REDIST_URL is not set. Pass "
            "-DSENSOR_3D_CORE_REDIST_URL=<repository-url> pointing "
            "at your sensor3d-core-redist repository, or install the Sensor3D "
            "SDK locally so it can be found by find_package(Sensor3D).")
    endif()

    set(_s3d_release_url "${SENSOR_3D_CORE_REDIST_URL}/releases/download/v${SENSOR3D_REQUIRED_VERSION}")
    set(_s3d_url "${_s3d_release_url}/${_s3d_asset}")

    set(SENSOR3D_EXAMPLES_CORE_REDIST_SHA256 "" CACHE STRING
        "Expected SHA256 of the fetched asset; leave empty to resolve it automatically from the release's <asset>.sha256 sidecar")

    # Resolves the expected checksum from the release asset's <asset>.sha256
    # file (same one-line format as `sha256sum -c`).
    if(NOT SENSOR3D_EXAMPLES_CORE_REDIST_SHA256)
        set(_s3d_sha256_file "${CMAKE_BINARY_DIR}/${_s3d_asset}.sha256")
        file(DOWNLOAD "${_s3d_url}.sha256" "${_s3d_sha256_file}" STATUS _s3d_sha256_status)
        list(GET _s3d_sha256_status 0 _s3d_sha256_code)
        if(_s3d_sha256_code EQUAL 0)
            file(STRINGS "${_s3d_sha256_file}" _s3d_sha256_line LIMIT_COUNT 1)
            string(REGEX MATCH "^[0-9a-fA-F]+" SENSOR3D_EXAMPLES_CORE_REDIST_SHA256 "${_s3d_sha256_line}")
        endif()
        if(NOT SENSOR3D_EXAMPLES_CORE_REDIST_SHA256)
            message(WARNING
                "Could not resolve a SHA256 for ${_s3d_asset} from "
                "${_s3d_url}.sha256 -- proceeding WITHOUT checksum "
                "verification. Set -DSENSOR3D_EXAMPLES_CORE_REDIST_SHA256=<hash> to pin one explicitly.")
        endif()
        unset(_s3d_sha256_file)
        unset(_s3d_sha256_status)
        unset(_s3d_sha256_code)
        unset(_s3d_sha256_line)
    endif()

    include(FetchContent)
    if(SENSOR3D_EXAMPLES_CORE_REDIST_SHA256)
        FetchContent_Declare(sensor3d_core URL "${_s3d_url}" URL_HASH SHA256=${SENSOR3D_EXAMPLES_CORE_REDIST_SHA256})
    else()
        FetchContent_Declare(sensor3d_core URL "${_s3d_url}")
    endif()
    FetchContent_MakeAvailable(sensor3d_core)
    list(APPEND CMAKE_PREFIX_PATH "${sensor3d_core_SOURCE_DIR}")

    unset(_s3d_os)
    unset(_s3d_ext)
    unset(_s3d_arch)
    unset(_s3d_asset)
    unset(_s3d_release_url)
    unset(_s3d_url)

    find_package(Sensor3D ${SENSOR3D_REQUIRED_VERSION} REQUIRED)
endif()

add_subdirectory(examples/01_connect_and_discover)
add_subdirectory(examples/02_configure_settings)
add_subdirectory(examples/03_capture_pointcloud_sync)
add_subdirectory(examples/04_capture_frames_async)
add_subdirectory(examples/05_error_handling)

install(TARGETS
    Sensor3D_Example_ConnectAndDiscover
    Sensor3D_Example_ConfigureSettings
    Sensor3D_Example_CapturePointCloudSync
    Sensor3D_Example_CaptureFramesAsync
    Sensor3D_Example_ErrorHandling
    DESTINATION bin
)
