cmake_minimum_required(VERSION 3.5)

project(singlechat.wt LANGUAGES CXX)
add_compile_options(-Wno-deprecated-declarations)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_COMPILER "/usr/bin/g++-12")
set(CMAKE_PREFIX_PATH "/usr")

# Set default build type if not specified
if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE Release)
endif()

# Set compiler flags based on build type
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
    add_compile_options(-g)
else()
    add_compile_options(-O2)
endif()

add_executable(${PROJECT_NAME}
    src/main.cpp
    src/broadcast.h src/broadcast.cpp
    src/app.h src/app.cpp
    docroot/text.xml
    docroot/ads.txt
    docroot/links.csv
)
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_20)

find_package(Threads REQUIRED)

target_link_libraries(${PROJECT_NAME}
    wt
    wthttp
    curl
    xml2
    ${CMAKE_THREAD_LIBS_INIT}
)

find_package(Boost COMPONENTS system filesystem REQUIRED)
if(UNIX)
    # Ubuntu-spezifische Include-Verzeichnisse
    find_package(PkgConfig)
    pkg_check_modules(XML2 libxml-2.0)
    if(XML2_FOUND)
        target_include_directories(${PROJECT_NAME} PRIVATE /usr/include/GraphicsMagick ${XML2_INCLUDE_DIRS})
    endif()
elseif(EXISTS "/etc/os-release" AND ("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux"))
    # Tumbleweed-spezifische Include-Verzeichnisse
    target_include_directories(${PROJECT_NAME} PRIVATE /usr/include/GraphicsMagick)
endif()

if(Boost_FOUND)
    include_directories(${Boost_INCLUDE_DIRS})
    target_link_libraries(${PROJECT_NAME} ${Boost_LIBRARIES} GraphicsMagick++ GraphicsMagick)
endif()

include(GNUInstallDirs)

install(TARGETS ${PROJECT_NAME}
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    RUNTIME DESTINATION /opt/ypchat/bin
)
install(DIRECTORY docroot/
    DESTINATION /opt/ypchat/docroot
)
install(CODE "file(MAKE_DIRECTORY /opt/ypchat/logs)")
install(CODE "execute_process(COMMAND chmod 777 /opt/ypchat/logs)")
