#--- mupp_plotter -------------------------------------------------------------
message("-- Handle the plotter part of mupp")

#--- unlike the rest of musredit_qt6 the plotter *does* use ROOT, hence ROOT's
#--- include dirs and recommended compiler flags are needed again here ---------
include_directories(SYSTEM ${ROOT_INCLUDE_DIRS})
string(APPEND CMAKE_CXX_FLAGS " ${ROOT_CXX_FLAGS}")

#--- instruct cmake NOT to run moc automatically when needed anymore ----------
set(CMAKE_AUTOMOC OFF)

#--- headers ------------------------------------------------------------------
set(HEADER
  mupp_plot.h
  PMuppCanvas.h
  PMuppCanvasLinkDef.h
  PMuppStartupHandler.h
  PMuppStartupHandlerLinkDef.h
)

#--- sources ------------------------------------------------------------------
set(SOURCE
  mupp_plot.cpp
  PMuppCanvas.cpp 
  PMuppStartupHandler.cpp 
)

#--- dictionary generated sources ---------------------------------------------
set ( PLOT_DIR
  ${CMAKE_CURRENT_BINARY_DIR}
)
# ROOT requires that the dictonary header files are found at configuration time.
# Hence, target_include_directories cannot be used here because, targets are 
# setup only afterwards.
include_directories(${PLOT_DIR})

#--- generate necessary dictionaries ------------------------------------------
root_generate_dictionary(
  PMuppCanvasDict 
  PMuppCanvas.h
  OPTIONS 
    -I${CMAKE_CURRENT_SOURCE_DIR}
    -I${CMAKE_CURRENT_SOURCE_DIR}/..
    -inlineInputHeader
  LINKDEF PMuppCanvasLinkDef.h
  MODULE PMuppCanvas
)
root_generate_dictionary(
  PMuppStartupHandlerDict 
  PMuppStartupHandler.h
  OPTIONS 
    -I${CMAKE_CURRENT_SOURCE_DIR}
    -I${CMAKE_CURRENT_SOURCE_DIR}/..
    -inlineInputHeader
  LINKDEF PMuppStartupHandlerLinkDef.h
  MODULE PMuppStartupHandler
)

#--- define all the dictonary cxx ---------------------------------------------
set(DICT
  ${PLOT_DIR}/PMuppCanvasDict.cxx
  ${PLOT_DIR}/PMuppStartupHandlerDict.cxx
)

#--- define executable --------------------------------------------------------
#--- on macOS the Dock icon cannot be set through ROOT, hence the Cocoa helper
#--- shared with musrview/musrFT/musrt0 is compiled in as well ----------------
add_executable(mupp_plot
  ${SOURCE} ${DICT}
  $<$<PLATFORM_ID:Darwin>:${CMAKE_SOURCE_DIR}/src/classes/PMacDockIcon.mm>
)

#--- add all needed include directories ---------------------------------------
target_include_directories(mupp_plot
  PRIVATE
    ${ROOT_INCLUDE_DIRS}
    ${CMAKE_CURRENT_BINARY_DIR}
    ${CMAKE_CURRENT_BINARY_DIR}/..
    ${CMAKE_CURRENT_SOURCE_DIR}
    ${CMAKE_CURRENT_SOURCE_DIR}/..
    $<$<PLATFORM_ID:Darwin>:${CMAKE_SOURCE_DIR}/src/include>
)

#--- define all to be linked libs ---------------------------------------------
target_link_libraries(mupp_plot ${ROOT_LIBRARIES})
if (APPLE)
  target_link_libraries(mupp_plot "-framework Cocoa")
endif (APPLE)

#--- install ------------------------------------------------------------------
if (APPLE)
  install(TARGETS mupp_plot DESTINATION /Applications/mupp.app/Contents/MacOS)
else (APPLE)
  install(TARGETS mupp_plot DESTINATION ${CMAKE_INSTALL_BINDIR})
endif (APPLE)

#--- Linux: mupp_plot is TCanvas based. It sets its own window icon and class
#--- hint (instead of the generic ROOT ones), the .desktop file lets the desktop
#--- match the class hint to the icon. ----------------------------------------
if (UNIX AND NOT APPLE)
  configure_file(
    ${CMAKE_SOURCE_DIR}/cmake/mupp_plot.desktop.in
    ${CMAKE_CURRENT_BINARY_DIR}/mupp_plot.desktop
    @ONLY
  )
  install(
    FILES ${CMAKE_CURRENT_BINARY_DIR}/mupp_plot.desktop
    DESTINATION ${MUSRFIT_XDG_DATA_DIR}/applications
  )
  install(
    FILES ${CMAKE_CURRENT_SOURCE_DIR}/icons/mupp_plot.png
    DESTINATION ${MUSRFIT_XDG_DATA_DIR}/icons/hicolor/48x48/apps
  )
  install(
    FILES ${CMAKE_CURRENT_SOURCE_DIR}/icons/mupp_plot-512.png
    DESTINATION ${MUSRFIT_XDG_DATA_DIR}/icons/hicolor/512x512/apps
    RENAME mupp_plot.png
  )
  #--- a stale icon cache would hide the new icon, hence refresh an existing one
  install(CODE "
    set(icon_dir \"\$ENV{DESTDIR}${MUSRFIT_XDG_DATA_DIR}/icons/hicolor\")
    find_program(GTK_UPDATE_ICON_CACHE gtk-update-icon-cache)
    if (GTK_UPDATE_ICON_CACHE AND EXISTS \"\${icon_dir}/icon-theme.cache\")
      execute_process(COMMAND \${GTK_UPDATE_ICON_CACHE} -q -t -f \"\${icon_dir}\")
    endif ()
  ")
endif ()

#--- install root pcm's and rootmaps ------------------------------------------
install(
  FILES ${CMAKE_CURRENT_BINARY_DIR}/libPMuppCanvas_rdict.pcm
        ${CMAKE_CURRENT_BINARY_DIR}/libPMuppCanvas.rootmap
        ${CMAKE_CURRENT_BINARY_DIR}/libPMuppStartupHandler_rdict.pcm
        ${CMAKE_CURRENT_BINARY_DIR}/libPMuppStartupHandler.rootmap
  DESTINATION ${CMAKE_INSTALL_LIBDIR}
)

#--- install headers ----------------------------------------------------------
install(
  FILES ${CMAKE_CURRENT_SOURCE_DIR}/../mupp.h
        ${CMAKE_CURRENT_SOURCE_DIR}/mupp_plot.h
        ${CMAKE_CURRENT_SOURCE_DIR}/PMuppCanvas.h
        ${CMAKE_CURRENT_SOURCE_DIR}/PMuppStartupHandler.h
  DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
