cmake_minimum_required(VERSION 3.5...3.31.5)

# Set extension name here
set(TARGET_NAME ui)

# DuckDB's extension distribution supports vcpkg. As such, dependencies can be
# added in ./vcpkg.json and then used in cmake with find_package. Feel free to
# remove or replace with other dependencies. Note that it should also be removed
# from vcpkg.json to prevent needlessly installing it..
find_package(OpenSSL REQUIRED)

include_directories(${OPENSSL_INCLUDE_DIR})

set(EXTENSION_NAME ${TARGET_NAME}_extension)

project(${TARGET_NAME})
include_directories(src/include ${PROJECT_SOURCE_DIR}/third_party/httplib)

set(EXTENSION_SOURCES
    src/event_dispatcher.cpp
    src/http_server.cpp
    src/settings.cpp
    src/state.cpp
    src/ui_extension.cpp
    src/utils/encoding.cpp
    src/utils/env.cpp
    src/utils/helpers.cpp
    src/utils/md_helpers.cpp
    src/utils/serialization.cpp
    src/utils/statement_execution.cpp
    src/watcher.cpp)

# DuckDB 2.x treats the DUCKDB_VERSION environment variable as an explicit
# version. extension-ci-tools sets it to the requested ref (e.g. "main"), which
# doesn't parse as vX.Y.Z and leaves the version variables empty. Fall back to
# the release version file DuckDB uses for dev builds.
if("${DUCKDB_MAJOR_VERSION}" STREQUAL ""
   AND EXISTS "${CMAKE_SOURCE_DIR}/scripts/ci/release_version.txt")
  file(READ "${CMAKE_SOURCE_DIR}/scripts/ci/release_version.txt"
       UI_DUCKDB_RELEASE_VERSION)
  string(STRIP "${UI_DUCKDB_RELEASE_VERSION}" UI_DUCKDB_RELEASE_VERSION)
  if(UI_DUCKDB_RELEASE_VERSION MATCHES "^([0-9]+)[.]([0-9]+)$")
    set(DUCKDB_MAJOR_VERSION "${CMAKE_MATCH_1}")
    set(DUCKDB_MINOR_VERSION "${CMAKE_MATCH_2}")
    set(DUCKDB_PATCH_VERSION "0")
  endif()
endif()
if("${DUCKDB_MAJOR_VERSION}" STREQUAL "")
  message(FATAL_ERROR "Unable to determine the DuckDB version")
endif()

add_definitions(-DDUCKDB_MAJOR_VERSION=${DUCKDB_MAJOR_VERSION})
add_definitions(-DDUCKDB_MINOR_VERSION=${DUCKDB_MINOR_VERSION})
add_definitions(-DDUCKDB_PATCH_VERSION=${DUCKDB_PATCH_VERSION})

# Newer httplib (bundled with DuckDB 2.x) loads root certificates from the macOS
# Keychain by default, which requires linking the Security and CoreFoundation
# frameworks. Keep using OpenSSL's default certificate paths, as before.
add_definitions(-DCPPHTTPLIB_DISABLE_MACOSX_AUTOMATIC_ROOT_CERTIFICATES)

find_package(Git)
if(NOT Git_FOUND)
  message(FATAL_ERROR "Git not found, unable to determine git sha")
endif()

execute_process(
  COMMAND ${GIT_EXECUTABLE} rev-list --count HEAD
  WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
  OUTPUT_VARIABLE UI_EXTENSION_SEQ_NUM
  OUTPUT_STRIP_TRAILING_WHITESPACE)

message(STATUS "UI_EXTENSION_SEQ_NUM=${UI_EXTENSION_SEQ_NUM}")
add_definitions(-DUI_EXTENSION_SEQ_NUM="${UI_EXTENSION_SEQ_NUM}")

execute_process(
  COMMAND ${GIT_EXECUTABLE} rev-parse --short=10 HEAD
  WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
  OUTPUT_VARIABLE UI_EXTENSION_GIT_SHA
  OUTPUT_STRIP_TRAILING_WHITESPACE)

message(STATUS "UI_EXTENSION_GIT_SHA=${UI_EXTENSION_GIT_SHA}")
add_definitions(-DUI_EXTENSION_GIT_SHA="${UI_EXTENSION_GIT_SHA}")

build_static_extension(${TARGET_NAME} ${EXTENSION_SOURCES})
build_loadable_extension(${TARGET_NAME} " " ${EXTENSION_SOURCES})

target_link_libraries(${EXTENSION_NAME} OpenSSL::SSL OpenSSL::Crypto)
target_link_libraries(${TARGET_NAME}_loadable_extension OpenSSL::SSL OpenSSL::Crypto)

install(
  TARGETS ${EXTENSION_NAME}
  EXPORT "${DUCKDB_EXPORT_SET}"
  LIBRARY DESTINATION "${INSTALL_LIB_DIR}"
  ARCHIVE DESTINATION "${INSTALL_LIB_DIR}")
