cmake_minimum_required(VERSION 3.16)

# Project setup
if(NOT DEFINED PROJECT_NAME)
    message(FATAL_ERROR "PROJECT_NAME is not defined. Pass it via -DPROJECT_NAME=<name>")
endif()

string(TIMESTAMP PROJECT_VERSION "%Y.%m%d.%H%M")
project(${PROJECT_NAME} VERSION ${PROJECT_VERSION} LANGUAGES CXX)

# Build configuration
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_EXE_LINKER_FLAGS "-static")
set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
set(BUILD_SHARED_LIBS OFF)
set(CMAKE_PREFIX_PATH /usr/local)

# Create executable
file(GLOB_RECURSE SOURCES "src/*.cpp")
add_executable(${PROJECT_NAME} ${SOURCES})

# Configure version.hpp
configure_file("src/version.hpp.in" "src/autogen/version.hpp" @ONLY)

# Pre-build script
add_custom_target(run_prebuild_script ALL 
    COMMAND bash ${CMAKE_CURRENT_SOURCE_DIR}/cmake_prebuild.sh
    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
add_dependencies(${PROJECT_NAME} run_prebuild_script)

# Include directories
target_include_directories(${PROJECT_NAME} PRIVATE 
    ${CMAKE_CURRENT_BINARY_DIR}/src/autogen 
    src
    src/common)

# Find packages
find_package(OpenSSL REQUIRED)
find_package(Drogon CONFIG REQUIRED)
find_package(nlohmann_json REQUIRED)

# Link libraries
target_link_libraries(${PROJECT_NAME} PRIVATE
    nlohmann_json::nlohmann_json Drogon::Drogon
    /usr/local/lib/libpgcommon.a /usr/local/lib/libpgport.a
    lzma dl)
    