# Copyright (C) 2020 Tycho Softworks.
#
# This file is free software; as a special exception the author gives
# unlimited permission to copy and/or distribute it, with or without
# modifications, as long as this notice is preserved.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY, to the extent permitted by law; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

file(GLOB server_src *.cpp)
file(GLOB server_hdr *.hpp)
file(GLOB server_man8 *.8)
file(GLOB server_man5 *.5)

if(CMAKE_CXX_STANDARD VERSION_LESS 20)
    list(APPEND server_libs fmt::fmt)
endif()

# setup rpath for exported symbols
if(BUILD_RPATH)
    set(CMAKE_BUILD_WITH_INSTALL_RPATH ON)
    SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_SBINDIR}")
endif()

set(server_name "bordeaux")
if(NOT CMAKE_SYSTEM_NAME MATCHES "OpenBSD" AND NOT CMAKE_SYSTEM_NAME MATCHES "NetBSD")
    list(APPEND server_libs "dl")
endif()

if(MALLOC_FOUND)
    list(APPEND server_libs PkgConfig::MALLOC)
endif()

if(SYSTEMD_FOUND)
    list(APPEND server_libs ${SYSTEMD_LIBRARIES})
endif()

add_executable(server ${server_src} ${server_hdr} bordeaux.conf bordeaux.default ${server_man8} ${server_man5})
target_link_libraries(server ${server_libs} OpenSSL::Crypto Threads::Threads)
set_target_properties(server PROPERTIES
    ENABLE_EXPORTS true OUTPUT_NAME ${server_name})

install(TARGETS server DESTINATION "${CMAKE_INSTALL_SBINDIR}")
install(FILES bordeaux.conf DESTINATION "${CMAKE_INSTALL_SYSCONFDIR}")
install(FILES ${server_man8} DESTINATION "${CMAKE_INSTALL_MANDIR}/man8")
install(FILES ${server_man5} DESTINATION "${CMAKE_INSTALL_MANDIR}/man5")
