# RockyGuard demo - CMakeLists.txt
#
# Builds a single executable, demo_app, that loads and verifies the
# bundled sample_demo_license.json. Modeled on the customer examples/
# CMakeLists.txt that ships in the same package; the only difference
# is one source file and a friendlier intro message.
#
# Usage:
#   cmake -B build
#   cmake --build build --config Release
#   ./build/Release/demo_app          (Windows)
#   ./build/demo_app                  (Linux)

cmake_minimum_required(VERSION 3.16)
project(rockyguard_demo LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# The demo lives at <package_root>/demo, so the package is one level up
if(NOT DEFINED ROCKYGUARD_DIR)
    set(ROCKYGUARD_DIR "${CMAKE_CURRENT_SOURCE_DIR}/..")
endif()

if(NOT EXISTS "${ROCKYGUARD_DIR}/include/rockyguard/rockyguard.h")
    message(FATAL_ERROR
        "RockyGuard headers not found at ${ROCKYGUARD_DIR}/include/rockyguard/\n"
        "This demo expects to live inside the customer package at <package>/demo.")
endif()

set(DEPS_DIR "${ROCKYGUARD_DIR}/deps")
if(EXISTS "${DEPS_DIR}/include/openssl" AND EXISTS "${DEPS_DIR}/lib")
    add_library(openssl_crypto STATIC IMPORTED)
    add_library(openssl_ssl STATIC IMPORTED)
    if(WIN32)
        set_target_properties(openssl_crypto PROPERTIES IMPORTED_LOCATION "${DEPS_DIR}/lib/libcrypto.lib")
        set_target_properties(openssl_ssl    PROPERTIES IMPORTED_LOCATION "${DEPS_DIR}/lib/libssl.lib")
    else()
        set_target_properties(openssl_crypto PROPERTIES IMPORTED_LOCATION "${DEPS_DIR}/lib/libcrypto.a")
        set_target_properties(openssl_ssl    PROPERTIES IMPORTED_LOCATION "${DEPS_DIR}/lib/libssl.a")
    endif()
    target_include_directories(openssl_crypto INTERFACE "${DEPS_DIR}/include")
    target_include_directories(openssl_ssl    INTERFACE "${DEPS_DIR}/include")
else()
    find_package(OpenSSL REQUIRED)
    add_library(openssl_crypto ALIAS OpenSSL::Crypto)
    add_library(openssl_ssl    ALIAS OpenSSL::SSL)
endif()

add_library(rockyguard STATIC IMPORTED)
if(WIN32)
    set_target_properties(rockyguard PROPERTIES IMPORTED_LOCATION "${ROCKYGUARD_DIR}/lib/static/rockyguard.lib")
else()
    set_target_properties(rockyguard PROPERTIES IMPORTED_LOCATION "${ROCKYGUARD_DIR}/lib/static/librockyguard.a")
endif()
target_include_directories(rockyguard INTERFACE "${ROCKYGUARD_DIR}/include")
# Link order matters for static archives on Linux: libssl.a references
# symbols in libcrypto.a, so ssl must precede crypto on the linker
# command line. ld resolves left-to-right and won't revisit ssl after
# pulling crypto. (The Windows MSVC linker is order-insensitive, so
# this same listing works for both platforms.)
target_link_libraries(rockyguard INTERFACE openssl_ssl openssl_crypto)

if(WIN32)
    target_link_libraries(rockyguard INTERFACE
        ws2_32 iphlpapi wbemuuid ole32 oleaut32 crypt32)
else()
    # libssl reaches into libdl/libpthread; spell them out so the
    # static link succeeds on minimal Linux toolchains.
    target_link_libraries(rockyguard INTERFACE pthread dl)
endif()

add_executable(demo_app main.cpp)
target_link_libraries(demo_app PRIVATE rockyguard)

# Copy the sample license next to the binary so the demo runs with no
# arguments. Same trick as the standard customer examples.
add_custom_command(TARGET demo_app POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_if_different
        "${CMAKE_CURRENT_SOURCE_DIR}/sample_demo_license.json"
        "$<TARGET_FILE_DIR:demo_app>/sample_demo_license.json")
