#--- mupp for Qt = 6.x --------------------------------------------------------

#--- Find includes in corresponding build directories -------------------------
set(CMAKE_INCLUDE_CURRENT_DIR ON)
#--- instruct CMake to run moc automatically when needed ----------------------
set(CMAKE_AUTOMOC ON)
#--- deal with the resources --------------------------------------------------
set(CMAKE_AUTORCC ON)

#--- define mupp version ------------------------------------------------------
set(mupp_VERSION 1.1.0)

#--- mupp_version.h generation - START ----------------------------------------
configure_file(
  ${CMAKE_CURRENT_SOURCE_DIR}/cmake/configure_mupp_version_file.cmake.in
  ${CMAKE_CURRENT_BINARY_DIR}/configure_mupp_version_file.cmake
  @ONLY
)

add_custom_command(
  OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/mupp_version.h
  COMMAND ${CMAKE_COMMAND} -P
    ${CMAKE_CURRENT_BINARY_DIR}/configure_mupp_version_file.cmake
  DEPENDS
    ${CMAKE_CURRENT_BINARY_DIR}/configure_mupp_version_file.cmake
    ${CMAKE_CURRENT_SOURCE_DIR}/cmake/mupp_version.h.in
  COMMENT "Configuring mupp_version.h"
  VERBATIM
)

add_custom_target(
  configure_mupp_version ALL
  DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/mupp_version.h
)
#--- mupp_version.h generation - END ------------------------------------------

add_subdirectory(plotter)

set(GENERATED_HEADER_FILES
  mupp_version.h
)
set_property(SOURCE mupp_version.h PROPERTY SKIP_AUTOMOC ON) # needed for cmake 3.x

set(MUPP_SOURCE_FILES
  mupp.cpp
  PmuppAdmin.cpp
  Pmupp.cpp
  PmuppScript.cpp
  PmuppGui.cpp
  PVarDialog.cpp
)

if (APPLE)
  set(RESOURCE_FILES icons/mupp.icns)
  add_executable(mupp
     MACOSX_BUNDLE ${GENERATED_HEADER_FILES} ${MUPP_SOURCE_FILES}
     mupp.qrc ${RESOURCE_FILES}
  )
else (APPLE)
  add_executable(mupp ${GENERATED_HEADER_FILES} ${MUPP_SOURCE_FILES} mupp.qrc)
endif (APPLE)

#--- check if project source is repo ------------------------------------------
if (IS_GIT_REPO)
  set(HAVE_GIT_REV_H "-DHAVE_GIT_REV_H")
else (IS_GIT_REPO)
  set(HAVE_GIT_REV_H "")
endif (IS_GIT_REPO)

#--- compiler option to workaround a little cast problem for some
#--- boost/compiler combinations. -fpermissive is a GCC option, clang/AppleClang
#--- silently ignore it, hence only hand it to GCC. ----------------------------
target_compile_options(mupp
  PRIVATE
    "$<$<CXX_COMPILER_ID:GNU>:-fpermissive>"
    "${HAVE_GIT_REV_H}"
)

#--- add the variable related sources -----------------------------------------
add_subdirectory(var)

#--- add the necessary header includes ----------------------------------------
target_include_directories(mupp
  BEFORE PRIVATE
    $<BUILD_INTERFACE:${Boost_INCLUDE_DIRS}>
    $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/../..>
    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/var/include>
)

#--- optional Python support for variable evaluation via ROOT's TPython -------
#--- only available if ROOT was built with -Dtpython=ON -----------------------
if (TARGET ROOT::ROOTTPython)
  target_compile_definitions(mupp PRIVATE MUPP_PYTHON)
  target_link_libraries(mupp PRIVATE ROOT::ROOTTPython)
  target_include_directories(mupp PRIVATE $<BUILD_INTERFACE:${ROOT_INCLUDE_DIRS}>)
  message(STATUS "mupp: optional Python variable support enabled (ROOT TPython found)")
else (TARGET ROOT::ROOTTPython)
  message(STATUS "mupp: optional Python variable support disabled (ROOT built without tpython)")
endif (TARGET ROOT::ROOTTPython)

#--- use the Widgets and XML modules from Qt5 ---------------------------------
target_link_libraries(mupp PRIVATE Qt6::Widgets Qt6::Xml)

if (IS_GIT_REPO)
  add_dependencies(mupp git_revision)
endif (IS_GIT_REPO)

#--- if macOS make an app rather than just a command line executable ----------
if (APPLE)
  #--- do NOT set the VERSION property on a MACOSX_BUNDLE target: cmake would
  #--- name the binary in Contents/MacOS 'mupp-<version>' and only leave a
  #--- symlink 'mupp' next to it, which contradicts CFBundleExecutable and does
  #--- not survive zipping/codesigning. ---------------------------------------
  set_target_properties(mupp PROPERTIES
    MACOSX_BUNDLE TRUE
    MACOSX_BUNDLE_BUNDLE_NAME "mupp"
    MACOSX_BUNDLE_INFO_STRING "mupp is used to plot parameters from musrfit collections."
    MACOSX_BUNDLE_ICON_FILE "mupp.icns"
    MACOSX_BUNDLE_LONG_VERSION_STRING "${mupp_VERSION}"
    MACOSX_BUNDLE_SHORT_VERSION_STRING "${mupp_VERSION}"
    MACOSX_BUNDLE_BUNDLE_VERSION "${mupp_VERSION}"
    MACOSX_BUNDLE_GUI_IDENTIFIER "ch.psi.lmu.mupp"
    MACOSX_FRAMEWORK_IDENTIFIER ch.psi.mupp
    MACOSX_BUNDLE_COPYRIGHT "Andreas Suter"
    RESOURCE "${RESOURCE_FILES}"
    INSTALL_RPATH "${Qt6_DIR}/../.."
  )
else (APPLE)
  set_target_properties(mupp PROPERTIES
    VERSION ${mupp_VERSION}
  )
endif (APPLE)

#--- install ------------------------------------------------------------------
if (APPLE)
  install(TARGETS mupp BUNDLE DESTINATION /Applications)
else (APPLE)
  install(TARGETS mupp DESTINATION ${CMAKE_INSTALL_BINDIR})
endif (APPLE)

#--- Linux: the .desktop file (named after the executable, i.e. the Wayland
#--- app-id) and the icon let the desktop show the musrfit icon for mupp. ---
if (UNIX AND NOT APPLE)
  configure_file(
    ${CMAKE_SOURCE_DIR}/cmake/mupp.desktop.in
    ${CMAKE_CURRENT_BINARY_DIR}/mupp.desktop
    @ONLY
  )
  install(
    FILES ${CMAKE_CURRENT_BINARY_DIR}/mupp.desktop
    DESTINATION ${MUSRFIT_XDG_DATA_DIR}/applications
  )
  install(
    FILES ${CMAKE_CURRENT_SOURCE_DIR}/icons/mupp-plain.svg
    DESTINATION ${MUSRFIT_XDG_DATA_DIR}/icons/hicolor/scalable/apps
    RENAME mupp.svg
  )
endif ()
