cmake_minimum_required(VERSION 3.16)

project(crate-path-encoding CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# ============================================================================
# Modular Crate Path Encoding Library
# ============================================================================

# Core library - no external dependencies
add_library(crate-encoding STATIC
  src/path_sort.cc
  src/tree_encode.cc
)

target_include_directories(crate-encoding PUBLIC
  ${CMAKE_CURRENT_SOURCE_DIR}/include
)

# Export include directory for downstream users
set(CRATE_ENCODING_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include CACHE PATH "Crate encoding include directory")

# ============================================================================
# Optional: OpenUSD Validation (requires OpenUSD)
# ============================================================================

option(BUILD_VALIDATION_TESTS "Build validation tests against OpenUSD" OFF)

if(BUILD_VALIDATION_TESTS)
  # Find OpenUSD
  find_package(pxr REQUIRED)

  # Path to OpenUSD installation
  set(USD_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/../../aousd/dist")

  if(NOT EXISTS ${USD_ROOT})
    set(USD_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/../../aousd/dist_monolithic")
  endif()

  message(STATUS "USD_ROOT: ${USD_ROOT}")

  # Include OpenUSD headers
  include_directories(BEFORE
    ${USD_ROOT}/include
  )

  # Link directories
  link_directories(${USD_ROOT}/lib)

  # Build validation executable
  add_executable(validate-path-sort
    validate-path-sort.cc
  )

  target_link_libraries(validate-path-sort
    crate-encoding
    ${USD_ROOT}/lib/libusd_sdf.so
    ${USD_ROOT}/lib/libusd_tf.so
    ${USD_ROOT}/lib/libusd_vt.so
    ${USD_ROOT}/lib/libusd_ar.so
    ${USD_ROOT}/lib/libusd_arch.so
    ${USD_ROOT}/lib/libusd_gf.so
    ${USD_ROOT}/lib/libusd_js.so
    ${USD_ROOT}/lib/libusd_kind.so
    ${USD_ROOT}/lib/libusd_plug.so
    ${USD_ROOT}/lib/libusd_trace.so
    ${USD_ROOT}/lib/libusd_work.so
    pthread
    dl
    ${USD_ROOT}/lib/libtbb.so
  )

  set_target_properties(validate-path-sort PROPERTIES
    BUILD_RPATH "${USD_ROOT}/lib"
    INSTALL_RPATH "${USD_ROOT}/lib"
  )
endif()

# ============================================================================
# Standalone Tests (no external dependencies)
# ============================================================================

add_executable(test-tree-encode
  test-tree-encode.cc
)

target_link_libraries(test-tree-encode
  crate-encoding
)

# ============================================================================
# Installation
# ============================================================================
# Use CMAKE_INSTALL_PREFIX to control where files are installed
# Example: cmake -DCMAKE_INSTALL_PREFIX=$HOME/.local ..

# Default install prefix if not specified
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
  set(CMAKE_INSTALL_PREFIX "${CMAKE_CURRENT_SOURCE_DIR}/install" CACHE PATH "Install path" FORCE)
endif()

install(TARGETS crate-encoding
  ARCHIVE DESTINATION lib
  LIBRARY DESTINATION lib
)

install(DIRECTORY include/crate
  DESTINATION include
)

message(STATUS "Install prefix: ${CMAKE_INSTALL_PREFIX}")
message(STATUS "To change: cmake -DCMAKE_INSTALL_PREFIX=/your/path ..")

# ============================================================================
# Summary
# ============================================================================

message(STATUS "")
message(STATUS "============================================================")
message(STATUS "Crate Path Encoding Library Configuration")
message(STATUS "============================================================")
message(STATUS "  Core library: crate-encoding (always built)")
message(STATUS "  Standalone tests: test-tree-encode (always built)")
if(BUILD_VALIDATION_TESTS)
  message(STATUS "  OpenUSD validation: validate-path-sort (enabled)")
else()
  message(STATUS "  OpenUSD validation: (disabled, use -DBUILD_VALIDATION_TESTS=ON)")
endif()
message(STATUS "")
message(STATUS "Build commands:")
message(STATUS "  make                    # Build all enabled targets")
message(STATUS "  make install            # Install library and headers")
message(STATUS "")
message(STATUS "Run tests:")
message(STATUS "  ./test-tree-encode      # Run standalone tests")
if(BUILD_VALIDATION_TESTS)
  message(STATUS "  ./validate-path-sort    # Run OpenUSD validation")
endif()
message(STATUS "============================================================")
message(STATUS "")
