cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

set(FFMPEG_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/ffmpeg")

# C++11
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)
set(CMAKE_CXX_EXTENSIONS False)

PROJECT (mineview)
if (WIN32)
  set(ZeroMQ_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/zeromq/release")
  set(ZeroMQ_LIB "${CMAKE_CURRENT_SOURCE_DIR}/zeromq/release/libzmq-mt-4_3_1.lib")
  install(FILES "${CMAKE_CURRENT_SOURCE_DIR}/zeromq/release/libzmq-mt-4_3_1.dll" DESTINATION bin)

  configure_file(
    "${CMAKE_CURRENT_SOURCE_DIR}/openvr_api.dll"
    "${CMAKE_BINARY_DIR}/openvr_api.dll"
    COPYONLY
    )
  install(FILES "${CMAKE_CURRENT_SOURCE_DIR}/openvr_api.dll" DESTINATION bin)

  find_package(VTK
    COMPONENTS
    CommonCore
    CommonDataModel
    CommonComputationalGeometry
    CommonSystem
    FiltersGeneral
    IOCore
    IOFFMPEG
    IOImage
    IOXML
    InteractionStyle
    RenderingOpenGL2
    RenderingOpenVR
  )

  set(mineview_srcs
    mineview.cxx
    mvActor.cxx
    mvAudioHandler.cxx
    mvCollaborationClient.cxx
    mvDataObject.cxx
    mvFlagpole.cxx
    mvLobby.cxx
    mvLobbyClient.cxx
    mvPhotoSphere.cxx
    mvPoseInterpolator.cxx
    mvTexture.cxx
    mvTourStop.cxx
    mvView.cxx
    vtkOpenGLMovieSphere.cxx
    mineview.rc
  )

  set(ffmpegdlls
    avcodec-58.dll
    avfilter-7.dll
    avformat-58.dll
    avutil-56.dll
    swresample-3.dll
    swscale-5.dll
  )

  add_executable(mineview MACOSX_BUNDLE ${mineview_srcs})
  target_link_libraries(mineview PRIVATE
    VTK::CommonComputationalGeometry
    VTK::CommonSystem
    VTK::IOCore
    VTK::IOFFMPEG
    VTK::IOImage
    VTK::IOXML
    VTK::IOXMLParser
    VTK::InteractionWidgets
    VTK::RenderingOpenVR
    ${ZeroMQ_LIB}
    Xaudio2.lib opengl32 winmm.lib)

  target_include_directories(mineview PUBLIC ${ZeroMQ_INCLUDE_DIR})

  # copy required dlls over that we have in the source tree
  foreach(adll ${ffmpegdlls})
    configure_file(
      "${CMAKE_CURRENT_SOURCE_DIR}/ffmpeg/bin/${adll}"
      "${CMAKE_BINARY_DIR}/${adll}"
      COPYONLY
      )
    install(FILES "${CMAKE_BINARY_DIR}/${adll}" DESTINATION bin)
  endforeach()

  configure_file(
    "${CMAKE_CURRENT_SOURCE_DIR}/zeromq/release/libzmq-mt-4_3_1.dll"
    "${CMAKE_BINARY_DIR}/libzmq-mt-4_3_1.dll"
    COPYONLY
    )
  else()
  find_package(ZeroMQ REQUIRED)
  list(APPEND ZeroMQ_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/zeromq/release")
  # static link to zmq, so we don't have to install it.
  set(ZeroMQ_LIB "libzmq.a" "pthread")
endif()

add_executable(collaboration_server collaborationServer.cxx)
target_include_directories(collaboration_server PUBLIC ${ZeroMQ_INCLUDE_DIR})
target_link_libraries(collaboration_server PUBLIC ${ZeroMQ_LIB})

add_executable(collaboration_client collaborationClient.cxx)
target_include_directories(collaboration_client PUBLIC ${ZeroMQ_INCLUDE_DIR})
target_link_libraries(collaboration_client PUBLIC ${ZeroMQ_LIB})


install(TARGETS mineview RUNTIME DESTINATION bin)
install(FILES Readme.txt DESTINATION .)

SET(CPACK_PACKAGE_DESCRIPTION_SUMMARY "Mineview - Virtual Reality viewer")
SET(CPACK_PACKAGE_VENDOR "Kitware Inc.")
SET(CPACK_PACKAGE_DESCRIPTION_FILE "${CMAKE_CURRENT_SOURCE_DIR}/ReadMe.txt")
SET(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/Copyright.txt")
SET(CPACK_PACKAGE_VERSION_MAJOR "2019")
SET(CPACK_PACKAGE_VERSION_MINOR "12")
SET(CPACK_PACKAGE_VERSION_PATCH "17")
SET(CPACK_PACKAGE_INSTALL_DIRECTORY "MineView")
IF(WIN32 AND NOT UNIX)
  # There is a bug in NSI that does not handle full unix paths properly. Make
  # sure there is at least one set of four (4) backlasshes.
  # SET(CPACK_PACKAGE_ICON "${CMake_SOURCE_DIR}/Utilities/Release\\\\InstallIcon.bmp")
  SET(CPACK_NSIS_INSTALLED_ICON_NAME "bin\\\\mineview.exe")
  SET(CPACK_NSIS_DISPLAY_NAME "MineView")
  SET(CPACK_NSIS_CONTACT "ken.martin@kitware.com")
  SET(CPACK_NSIS_MODIFY_PATH ON)
ENDIF(WIN32 AND NOT UNIX)
SET(CPACK_PACKAGE_EXECUTABLES "MineView" "MineView")
INCLUDE(CPack)


# SET(CPACK_NSIS_EXTRA_INSTALL_COMMANDS "
#   WriteRegStr HKLM 'Software\\\\MyApp' 'MyValue' 'MyData'
#   WriteRegDWORD HKLM 'Software\\\\MyApp' 'OtherValue' '4'
#    ")
# SET(CPACK_NSIS_EXTRA_UNINSTALL_COMMANDS "
#   DeleteRegKey HKLM 'Software\\\\MyApp'
#  ")
