#-----------------------------------------------------------------------------
#
# Copyright (C) 2012-2024  Florian Pose <fp@igh.de>
#               2022       Bjarne von Horn <vh at igh dot de>
#
# This file is part of the data logging service (DLS).
#
# DLS is free software: you can redistribute it and/or modify it under the
# terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# DLS is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
# more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with the DLS. If not, see <http://www.gnu.org/licenses/>.
#
#-----------------------------------------------------------------------------

cmake_minimum_required(VERSION 3.6)

project(dls VERSION 1.6.8) # remember to edit NEWS file

include(CMakeDependentOption)
include(CMakePackageConfigHelpers)
include(GNUInstallDirs)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

option(BUILD_SHARED_LIBS "Build using shared libraries" ON)
option(BUILD_DAEMON "Build DLS Daemon" ON)
option(DLS_SERVER "Build DLS protocol server" ON)
option(DLS_PROTO_DEBUG "Debug protocol" OFF)
option(BUILD_FLTK_GUIS "Build FLTK GUIs" ON)
option(BUILD_DLS_TOOL "Build commandline tool" ON)
option(BUILD_WIDGETS "Build DLS Widgets" ON)
option(DLS_HDF5_EXPORT "Enable HDF5 Export" ON)
cmake_dependent_option(DLS_DESIGNER "Build DLS Widgets as Qt Designer Plugin" ON "BUILD_WIDGETS" OFF)
cmake_dependent_option(DLS_GUI "Build DLS GUI" ON "BUILD_WIDGETS" OFF)

# Place for dls config file, depends on linux distribution
if (NOT DLS_ENVIRONMENT_FILE_LOCATION)
    set(DLS_ENVIRONMENT_FILE_LOCATION "${CMAKE_INSTALL_FULL_SYSCONFDIR}/sysconfig")
endif()

find_package(LibXml2 REQUIRED)
find_package(PkgConfig)
pkg_check_modules(FFTW3 REQUIRED fftw3)
pkg_check_modules(PCRE REQUIRED libpcre2-8)

set(protobuf_MODULE_COMPATIBLE ON)
find_package(Protobuf CONFIG)
if (NOT protobuf_FOUND)
    find_package(Protobuf REQUIRED MODULE)
    if (NOT TARGET protobuf::libprotobuf)
        add_library(protobuf::libprotobuf SHARED IMPORTED)
        set_target_properties(protobuf::libprotobuf PROPERTIES
            INTERFACE_INCLUDE_DIRECTORIES "${PROTOBUF_INCLUDE_DIRS}"
            IMPORTED_LOCATION "${PROTOBUF_LIBS}"
        )
    endif()
endif()

find_package(uriparser REQUIRED)
find_package(Threads REQUIRED)
find_package(ZLIB REQUIRED)
if (DLS_HDF5_EXPORT)
    find_package(HDF5 REQUIRED COMPONENTS C CXX)
endif ()

if (BUILD_WIDGETS)
    set(REQUIRED_QT_COMPONENTS Core Gui LinguistTools PrintSupport Svg Widgets Xml)
    if (DLS_DESIGNER)
        list(APPEND REQUIRED_QT_COMPONENTS Designer)
    endif()

    find_package(Qt5 REQUIRED COMPONENTS ${REQUIRED_QT_COMPONENTS})
endif()

if (NOT REVISION)
    execute_process(COMMAND sh "${CMAKE_CURRENT_SOURCE_DIR}/revision.sh" "${CMAKE_CURRENT_SOURCE_DIR}"
        OUTPUT_VARIABLE REVISION
        ERROR_VARIABLE REVISION_RESULT
        OUTPUT_STRIP_TRAILING_WHITESPACE
    )
    if (NOT ${REVISION_RESULT} EQUAL 0)
        message(FATAL_ERROR "Could not get revsision, is git installed?")
    endif()
endif()

configure_file(config_cmake.h.in "${CMAKE_CURRENT_BINARY_DIR}/config.h" @ONLY)

add_subdirectory(lib)
add_subdirectory(script)

if (BUILD_DAEMON)
    find_package(pdcom5 REQUIRED)
    add_subdirectory(daemon)
endif()
if (BUILD_FLTK_GUIS)
    find_package(FLTK REQUIRED)
    add_subdirectory(fltkguis)
endif()
if (BUILD_DLS_TOOL)
    add_subdirectory(tool)
endif()

if (BUILD_WIDGETS)
    add_subdirectory(widgets)
    if (DLS_GUI)
        add_subdirectory(gui)
    endif()
endif()

## build documentation by default, if doxygen is found

CONFIGURE_FILE(Doxyfile.in Doxyfile)

FIND_PACKAGE(Doxygen)
OPTION(BUILD_DOCS "Build documentation" ${DOXYGEN_FOUND})
IF(BUILD_DOCS)
    INSTALL(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/doxygen-output/html
        DESTINATION ${CMAKE_INSTALL_DATADIR}/doc/${LIBNAME} OPTIONAL)

    ADD_CUSTOM_TARGET(doc ALL
        ${DOXYGEN_EXECUTABLE} Doxyfile
        WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
        COMMENT "Generating API documentation with Doxygen" VERBATIM
    )
ENDIF()
