##*************************************************************************##
##  CUBE        http://www.scalasca.org/                                   ##
##*************************************************************************##
##  Copyright (c) 2024-2025                                                ##
##  Forschungszentrum Juelich GmbH, Juelich Supercomputing Centre          ##
##                                                                         ##
##  This software may be modified and distributed under the terms of       ##
##  a BSD-style license.  See the COPYING file in the package base         ##
##  directory for details.                                                 ##
##*************************************************************************##


include(CheckIncludeFileCXX)
include(GNUInstallDirs) # for CMAKE_INSTALL_INCLUDEDIR

set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_VERBOSE_MAKEFILE ON)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

#####################################################################################
# find CubeLib
#####################################################################################

find_package(CubeLib REQUIRED)

#####################################################################################
# Qt
#####################################################################################

find_package(QT NAMES Qt5 Qt6 REQUIRED COMPONENTS Core)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets Gui Network)
find_package(Qt${QT_VERSION_MAJOR} OPTIONAL_COMPONENTS Concurrent QUIET)

message(STATUS "building with WebSockets???")
if(EMSCRIPTEN OR WEB_SOCKETS)
    message(STATUS "building with WebSockets")
    find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS WebSockets)  # use WebSockets instead of posix sockets
    if(Qt6WebSockets_FOUND)
        set(WEB_SOCKETS ON CACHE BOOL "Enable WebSockets support")
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DWEB_SOCKETS")
        if(EMSCRIPTEN)
            # todo: check faster alternative -fwasm-exceptions
            add_compile_options(-fexceptions -sDISABLE_EXCEPTION_CATCHING=0)  # allow exceptions
            # set(CMAKE_EXE_LINKER_FLAGS "-sASSERTIONS=0 -sSAFE_HEAP=1 -gsource-map")  # debugging
        endif()
    else()
       message(FATAL_ERROR "WebSockets are required for emscripten build, but not found")
    endif()
else()
    set(WEB_SOCKETS OFF CACHE BOOL "Enable WebSockets support")
endif()

option(CUBEGUI_DISABLE_QWEBENGINE "Disable QWebEngine " OFF )
if (NOT CUBEGUI_DISABLE_QWEBENGINE)
    find_package(Qt${QT_VERSION_MAJOR} OPTIONAL_COMPONENTS WebEngineWidgets QUIET)
endif()

# qt_add_plugin was introduced with qt6 and seems to be buggy in version 6.4.2
if(Qt5_FOUND OR (Qt6_FOUND AND Qt6_VERSION VERSION_EQUAL 6.4.2))
    include(../cmake-modules/qt5-compability.cmake) # replacement for qt_add_plugin...
endif()
if(Qt5_FOUND)
     set(QT_VERSION ${Qt5_VERSION})
elseif(Qt6_FOUND)
    set(QT_VERSION ${Qt6_VERSION})
endif()

message(STATUS "Qt version ${QT_VERSION} found")

if (Qt${QT_VERSION_MAJOR}Concurrent_FOUND)
    set(DEFINE_CUBE_CONCURRENT_LIB "#define CUBE_CONCURRENT_LIB")
else()
    message(WARNING "Qt Concurrent not found. Building single-threaded version.")
endif()

if (Qt${QT_VERSION_MAJOR}WebEngineWidgets_FOUND)
    set(DEFINE_WITH_WEB_ENGINE "#define WITH_WEB_ENGINE")
    message(STATUS "Qt WebEngineWidgets found.")
else()
    message(STATUS "Qt WebEngineWidgets not found. Using simplified documentation browser.")
endif()

# generate moc files automatically and use qrc resources
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON) # build .qrc resource files

#####################################################################################
# generate header files
#####################################################################################

configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake-include/cubegui-version.h.in ${CMAKE_BINARY_DIR}/src/cubegui-version.h @ONLY)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake-include/cubegui-config.h.in ${CMAKE_BINARY_DIR}/src/cubegui-config.h @ONLY)
# the following header files are only used for the build process
file(CREATE_LINK ${CMAKE_BINARY_DIR}/src/cubegui-config.h ${CMAKE_BINARY_DIR}/src/cubegui-concurrent.h COPY_ON_ERROR SYMBOLIC)
file(CREATE_LINK ${CMAKE_BINARY_DIR}/src/cubegui-config.h ${CMAKE_BINARY_DIR}/src/config.h COPY_ON_ERROR SYMBOLIC)
file(CREATE_LINK ${CMAKE_BINARY_DIR}/src/cubegui-config.h ${CMAKE_BINARY_DIR}/src/config-frontend.h COPY_ON_ERROR SYMBOLIC)

#####################################################################################
# build cubegui library and executable
##########################j###########################################################

add_subdirectory(GUI-qt/display)

#####################################################################################
# build cubegui plugins
#####################################################################################

# define install path for cube gui plugins
if (NOT WIN32)
    set(PLUGIN_DIR ${CMAKE_INSTALL_LIBDIR}/cube-plugins) # Linux&Co
else ()
    set(PLUGIN_DIR ${CMAKE_INSTALL_PREFIX}/lib/cube-plugins) # Windows
endif ()

add_subdirectory(GUI-qt/plugins)

#####################################################################################
# deploy cubegui on Windows with all dependent libraries
#####################################################################################
if (WIN32)
    qt_generate_deploy_app_script(
    TARGET CubeGui # use library target instead of binary target to deploy all dependant libraries
        OUTPUT_SCRIPT deploy_script
        NO_TRANSLATIONS # translations don't exist yet
    )
    install(SCRIPT ${deploy_script})

    # qt deploy only installs Qt and compiler runtime libraries
    # install further libraries which CubeGui depends on (cubelib and dependencies of cubelib)
    find_package(dlfcn-win32)
    if (dlfcn-win32_FOUND)
        get_target_property(LIBDL_LOCATION dlfcn-win32::dl LOCATION) # get path to dl.dll
        message(STATUS "dlfcn-win32 found -> deploy ${LIBDL_LOCATION}")
    endif()
    install(FILES
        $<TARGET_FILE:CubeLib>
        ${LIBDL_LOCATION}
        DESTINATION bin
    )
    # set pathes in nsi windows installer configuration file
    set(CUBEGUI_INSTALL ${CMAKE_INSTALL_PREFIX})
    set(CUBELIB_INSTALL ${CubeLib_DIR}/../../.. ) # CubeLib_DIR=${CUBELIB_INSTALL_PREFIX}/lib/cmake/CubeLib
    configure_file(${CMAKE_SOURCE_DIR}/vendor/windows/cubeinstaller.nsi.in ${CMAKE_BINARY_DIR}/cubeinstaller.nsi @ONLY)
endif()
