# -------------------------------------------------------------------
#       item            : dplotter/CMakeLists.txt
#       made by         : Rene' van Paassen
#       date            : 190105
#       category        : cmake definition
#       copyright       : (c) 2019 TUDelft-AE-C&S

#https://itnext.io/vuejs-and-webpack-4-from-scratch-part-1-94c9c28a534a

# NOTE: npm does not seem to support out-of-tree builds, don't try!

# gnu install dirs
enable_language(C)
include(GNUInstallDirs)

cmake_minimum_required(VERSION 3.0)
set(dplotter_VERSION 1.0)
project(dplotter VERSION ${dplotter_VERSION} LANGUAGES)

# only required programs
find_program(NPM npm)
find_package(ImageMagick COMPONENTS convert)

# configure all build material
configure_file(obs/dplotter.spec.in obs/dplotter.spec @ONLY)
configure_file(obs/dplotter.dsc.in obs/dplotter.dsc @ONLY)
configure_file(obs/Portfile.in obs/Portfile @ONLY)
configure_file(obs/debian/changelog.in obs/debian/changelog @ONLY)

# node modules required for compiling all
add_custom_command(OUTPUT node_modules
  COMMAND
  ${NPM} install)

# create the dist version
add_custom_command(OUTPUT dist
  COMMAND ${NPM} run build &&
  sed -i -e 's/Vue App/DUECA data \\&amp\\; plots/' dist/index.html
  DEPENDS node_modules)

# icon generation
set(RESOLUTIONS 16 24 32 48 64)
foreach (RES ${RESOLUTIONS})
  add_custom_command(OUTPUT favicon${RES}.png
    COMMAND ${ImageMagick_convert_EXECUTABLE}
    ${CMAKE_CURRENT_SOURCE_DIR}/pixmaps/favicon.svg
    -resize ${RES}x${RES} favicon${RES}.png
    DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/pixmaps/favicon.svg)
  list(APPEND ALLPNG favicon${RES}.png)
endforeach()

add_custom_command(OUTPUT favicon.ico
  COMMAND ${ImageMagick_convert_EXECUTABLE} ${ALLPNG} favicon.ico
  DEPENDS ${ALLPNG})

# need targets to turn this into buildable stuff
add_custom_target(icons DEPENDS favicon.ico)
add_custom_target(webcode DEPENDS dist)

# install work
install(DIRECTORY
  ${CMAKE_CURRENT_BINARY_DIR}/dist
  DESTINATION ${CMAKE_INSTALL_DATADIR}/dplotter)
install(FILES
  ${CMAKE_CURRENT_BINARY_DIR}/favicon.ico
  DESTINATION ${CMAKE_INSTALL_DATADIR}/dplotter/dist)
