cmake_minimum_required(VERSION 3.5)
project(web)

include(${CMAKE_SOURCE_DIR}/tools/safe_dep_glob.cmake)
include(${CMAKE_SOURCE_DIR}/tools/Protobuf.cmake)

# DEPENDENCIES
set(PROTO_GLOBS
  ../lib/api/*.proto
  ../lib/api/services/*.proto
  ../lib/domain_model/*.proto
  ../lib/sparkproto/*.proto
  ../lib/md/protos/*.proto
  ../../externals/linux/include/google/protobuf/descriptor.proto
  ../../externals/linux/include/google/protobuf/timestamp.proto
  )
file (GLOB_RECURSE Proto_Deps RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ${PROTO_GLOBS})
file (GLOB_RECURSE Proto_Deps_For_Tool RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}/api ${PROTO_GLOBS})
file (GLOB_RECURSE Api_Src RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ui/src/*.fs)
file (GLOB_RECURSE Ui_Src RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} api/src/*.ts)
file (GLOB_RECURSE build_scripts RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} api/*.sh)
set (All_Deps ${Proto_Deps} ${Api_Src} ${Ui_Src} ${build_scripts})
update_deps_file("${All_Deps}")
# END DEPENDENCIES

add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/web/ui/bundle.js
  COMMAND /bin/bash ${CMAKE_CURRENT_SOURCE_DIR}/api/quiet_build.sh
  ARGS ${CMAKE_BINARY_DIR} ${Proto_Deps_For_Tool}
  COMMENT "Building Web UI"
  WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/api
  DEPENDS ${All_Deps})

add_custom_target(web_ui_bundle ALL DEPENDS ${CMAKE_BINARY_DIR}/web/ui/bundle.js)

install(DIRECTORY "${CMAKE_BINARY_DIR}/web" DESTINATION bin)
