
#-----------------------------------------------------------------------------
set(MODULE_NAME NetGenMesher)

#-----------------------------------------------------------------------------

#
# SlicerExecutionModel
#
find_package(SlicerExecutionModel REQUIRED)
include(${SlicerExecutionModel_USE_FILE})

find_package(Netgen QUIET REQUIRED)


#-----------------------------------------------------------------------------
set(MODULE_INCLUDE_DIRECTORIES
  ${MRMLCore_INCLUDE_DIRS}
  )

set(MODULE_SRCS
  vtkNetGenVolume.cxx
  vtkNetGenVolume.h
)

set(MODULE_TARGET_LIBRARIES
  ${VTK_LIBRARIES}
  nglib
  MRMLCore
  )



#-----------------------------------------------------------------------------
SEMMacroBuildCLI(
  NAME ${MODULE_NAME}
  TARGET_LIBRARIES ${MODULE_TARGET_LIBRARIES}
  INCLUDE_DIRECTORIES ${MODULE_INCLUDE_DIRECTORIES}
  ADDITIONAL_SRCS ${MODULE_SRCS}
  )

#-----------------------------------------------------------------------------
if(BUILD_TESTING)
  add_subdirectory(Testing)
endif()



if(WIN32)

  get_target_property(nglib_path nglib LOCATION)
  get_target_property(ngcore_path ngcore LOCATION)


  #Copy nglib/ngcore dll to build tree
  set(ng_dest "${CMAKE_BINARY_DIR}/${Slicer_CLIMODULES_BIN_DIR}/${CMAKE_CFG_INTDIR}")
  get_filename_component(nglib_name ${nglib_path} NAME)
  get_filename_component(ngcore_name ${ngcore_path} NAME)

  add_custom_command(
    OUTPUT "${ng_dest}/${nglib_name}" "${ng_dest}/${ngcore_name}"
    COMMAND "${CMAKE_COMMAND}" -E make_directory "${ng_dest}"
    COMMAND "${CMAKE_COMMAND}" -E copy "${nglib_path}" "${ng_dest}"
    COMMAND "${CMAKE_COMMAND}" -E copy "${ngcore_path}" "${ng_dest}"
    DEPENDS "${nglib_path}" "${ngcore_path}"
    )

  add_custom_target(NGLIB_COPY ALL
    SOURCES "${nglib_path}" "${ngcore_path}"
    DEPENDS "${ng_dest}/${nglib_name}" "${ng_dest}/${ngcore_name}"
    COMMENT "Copying ng libraries to  ${ng_dest}"
    )

  install(
    FILES ${nglib_path} ${ngcore_path}
    DESTINATION ${Slicer_INSTALL_CLIMODULES_BIN_DIR} 
    COMPONENT RuntimeLibraries
    )
else()
  get_target_property(nglib_path nglib LOCATION)
  get_target_property(ngcore_path ngcore LOCATION)
  get_target_property(csg_path csg LOCATION)
  get_target_property(geom2d_path geom2d LOCATION)
  get_target_property(interface_path interface LOCATION)
  get_target_property(mesh_path mesh LOCATION)
  get_target_property(occ_path occ LOCATION)
  get_target_property(stl_path stl LOCATION)
  get_target_property(visual_path visual LOCATION)


  #Copy nglib/ngcore dll to build tree
  set(ng_dest "${CMAKE_BINARY_DIR}/${Slicer_CLIMODULES_BIN_DIR}/${CMAKE_CFG_INTDIR}")
  get_filename_component(nglib_name ${nglib_path} NAME)
  get_filename_component(ngcore_name ${ngcore_path} NAME)
  get_filename_component(csg_name ${csg_path} NAME)
  get_filename_component(geom2d_name ${geom2d_path} NAME)
  get_filename_component(interface_name ${interface_path} NAME)
  get_filename_component(mesh_name ${mesh_path} NAME)
  get_filename_component(occ_name ${occ_path} NAME)
  get_filename_component(stl_name ${stl_path} NAME)
  get_filename_component(visual_name ${visual_path} NAME)

  add_custom_command(
    OUTPUT "${ng_dest}/${nglib_name}" "${ng_dest}/${ngcore_name}" "${ng_dest}/${csg_name}" "${ng_dest}/${geom2d_name}" "${ng_dest}/${interface_name}" "${ng_dest}/${mesh_name}" "${ng_dest}/${occ_name}" "${ng_dest}/${stl_name}" "${ng_dest}/${visual_name}"
    COMMAND "${CMAKE_COMMAND}" -E make_directory "${ng_dest}"
    COMMAND "${CMAKE_COMMAND}" -E copy "${nglib_path}" "${ng_dest}"
    COMMAND "${CMAKE_COMMAND}" -E copy "${ngcore_path}" "${ng_dest}"
    COMMAND "${CMAKE_COMMAND}" -E copy "${csg_path}" "${ng_dest}"
    COMMAND "${CMAKE_COMMAND}" -E copy "${geom2d_path}" "${ng_dest}"
    COMMAND "${CMAKE_COMMAND}" -E copy "${interface_path}" "${ng_dest}"
    COMMAND "${CMAKE_COMMAND}" -E copy "${mesh_path}" "${ng_dest}"
    COMMAND "${CMAKE_COMMAND}" -E copy "${occ_path}" "${ng_dest}"
    COMMAND "${CMAKE_COMMAND}" -E copy "${stl_path}" "${ng_dest}"
    COMMAND "${CMAKE_COMMAND}" -E copy "${visual_path}" "${ng_dest}"
    DEPENDS "${nglib_path}" "${ngcore_path}" "${csg_path}" "${geom2d_path}" "${interface_path}" "${mesh_path}" "${occ_path}" "${stl_path}" "${visual_path}"
    )

  add_custom_target(NGLIB_COPY ALL
    SOURCES "${nglib_path}" "${ngcore_path}" "${csg_path}" "${geom2d_path}" "${interface_path}" "${mesh_path}" "${occ_path}" "${stl_path}" "${visual_path}"
    DEPENDS "${ng_dest}/${nglib_name}" "${ng_dest}/${ngcore_name}" "${ng_dest}/${csg_name}" "${ng_dest}/${geom2d_name}" "${ng_dest}/${interface_name}" "${ng_dest}/${mesh_name}" "${ng_dest}/${occ_name}" "${ng_dest}/${stl_name}" "${ng_dest}/${visual_name}"
    COMMENT "Copying ng libraries to  ${ng_dest}"
    )

  install(
    FILES ${nglib_path} ${ngcore_path} ${csg_path} ${geom2d_path} ${interface_path} ${mesh_path} ${occ_path} ${stl_path} ${visual_path}
    DESTINATION ${Slicer_INSTALL_CLIMODULES_BIN_DIR} 
    COMPONENT RuntimeLibraries
    )

endif()

