##*************************************************************************##
##  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.                                                 ##
##*************************************************************************##


cmake_minimum_required(VERSION 3.16)

# get version information from git, e.g. ${CUBEGUI_FULL_NAME}, ${CUBEGUI_REVISION}
include(./cmake-modules/CubeGuiVersion.cmake)

project(cubegui VERSION ${CUBEGUI_VERSION_MAJOR}.${CUBEGUI_VERSION_MINOR}.${CUBEGUI_VERSION_PATCH} LANGUAGES C CXX)
message(STATUS "CubeGUI version ${CUBEGUI_VERSION} ${CUBEGUI_REVISION} ${CUBEGUI_FULL_NAME}" )

#####################################################################################
# set release build type as default for every cmake run
#####################################################################################
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo")
set(CMAKE_CONFIGURATION_TYPES "Debug;Release;RelWithDebInfo;MinSizeRel" CACHE STRING "" FORCE)

if(NOT CMAKE_BUILD_TYPE)
     set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build." FORCE)
endif()
set(CMAKE_CXX_FLAGS_RELEASE "-O2")

#####################################################################################
# build cubegui
#####################################################################################
add_subdirectory(src)

#####################################################################################
# optionally build doxygen user guides
#####################################################################################
option(CUBEGUI_BUILD_DOC "Build documentation" OFF)
set( DOC_ROOT ${CMAKE_SOURCE_DIR}/doc ) # pdf and html documentation will be placed here
if (CUBEGUI_BUILD_DOC)
   add_subdirectory(doc)
   set( BUILD_DOCUMENTATION documentation )
endif()

#####################################################################################
# install
#####################################################################################

# configure and install desktop file and application metainfo file
set( prefix ${CMAKE_INSTALL_PREFIX} )
set( VERSION ${CUBEGUI_VERSION} )
configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/share/cubegui.desktop.in ${CMAKE_BINARY_DIR}/share/cubegui.desktop @ONLY )
install( FILES ${CMAKE_BINARY_DIR}/share/cubegui.desktop ${CMAKE_SOURCE_DIR}/share/cubegui.metainfo.xml DESTINATION ${CMAKE_INSTALL_PREFIX}/share/ )

# install README, OPEN_ISSUES, ...
install( FILES ${CMAKE_SOURCE_DIR}/README ${CMAKE_SOURCE_DIR}/OPEN_ISSUES DESTINATION ${CMAKE_INSTALL_PREFIX}/share/doc/cubegui )
install( FILES ${CMAKE_SOURCE_DIR}/COPYING DESTINATION ${CMAKE_INSTALL_PREFIX} RENAME cubelicense.txt )

#--- check if doxygen documentation will be generated or is already included
if( CUBEGUI_BUILD_DOC OR EXISTS ${DOC_ROOT}/guide-html.tar.gz )
     #--- extract html documentation from tarball
     add_custom_command(OUTPUT ${DOC_ROOT}/guide
          WORKING_DIRECTORY ${DOC_ROOT}
          COMMAND ${CMAKE_COMMAND} -E tar xzf guide-html.tar.gz )
     add_custom_command(OUTPUT ${DOC_ROOT}/plugin-guide
          WORKING_DIRECTORY ${DOC_ROOT}
          COMMAND ${CMAKE_COMMAND} -E tar xzf plugins-guide-html.tar.gz )
     add_custom_target(extract-guide ALL DEPENDS ${BUILD_DOCUMENTATION} ${DOC_ROOT}/guide ${DOC_ROOT}/plugin-guide)

     #--- install documentation
     set( DOC_INSTALL "${CMAKE_INSTALL_PREFIX}/share/doc/cubegui" )
     install( FILES ${DOC_ROOT}/CubeUserGuide.pdf        DESTINATION ${DOC_INSTALL} )
     install( FILES ${DOC_ROOT}/CubePluginsUserGuide.pdf DESTINATION ${DOC_INSTALL} )
     install( DIRECTORY ${DOC_ROOT}/guide                DESTINATION ${DOC_INSTALL} )
     install( DIRECTORY ${DOC_ROOT}/plugins-guide        DESTINATION ${DOC_INSTALL} )
else()
     message(WARNING "Prebuild documentation is not available. Enable CUBEGUI_BUILD_DOC to build documentation with doxygen")
endif()

#####################################################################################
# build tarball
#####################################################################################

if (NOT EMSCRIPTEN)
# tarball contains the git controlled source tree plus the generated documentation
add_custom_target( dist
            COMMAND ${GIT_EXECUTABLE} archive --format=tar.gz -o ${CUBEGUI_FULL_NAME}.tar.gz
            --prefix=${CUBEGUI_FULL_NAME}/src/ --add-file=${CMAKE_BINARY_DIR}/src/cubegui-config.h
            --prefix=${CUBEGUI_FULL_NAME}/ HEAD
            COMMAND rm -rf ${CUBEGUI_FULL_NAME} && tar -xzf ${CUBEGUI_FULL_NAME}.tar.gz && # add generated documentation to tarball
                    cp -r ${DOC_ROOT} ${CUBEGUI_FULL_NAME} &&
                    tar -czf ${CUBEGUI_FULL_NAME}.tar.gz ${CUBEGUI_FULL_NAME}
            WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
            DEPENDS documentation
)
endif()
