INCLUDE_DIRECTORIES(${OCTINCLUDEDIR} "${OCTINCLUDEDIR}/octave" )
LINK_LIBRARIES(octinterp octave cruft vtkCommon)
LINK_DIRECTORIES(${OCTLIBDIR})

ADD_LIBRARY( octaviz SHARED octaviz.cc )

OCTAVE_WRAP_VTK(Common)
# This sets INCLUDE_FLAGS and LINK_FLAGS also for the .cc files below

SET (common_octs)
  # track the .oct files for shorter DEPENDS lines

# How to build the .oct files in this directory
FOREACH(class vtk_init vtkInitializeInteractor vtk_print_ref_table vtk_get_pointer vtkRaiseWindow)

  # FIXME: vtkRaiseWindow needs vtkRendering on Apple?!
  IF(${class} STREQUAL "vtkRaiseWindow")
    SET(APPLE_FLAG "-lvtkRendering")
  ELSE(${class} STREQUAL "vtkRaiseWindow")
    SET(APPLE_FLAG)
  ENDIF(${class} STREQUAL "vtkRaiseWindow")
    
  ADD_CUSTOM_COMMAND(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${class}.oct
    COMMAND ${CMAKE_COMMAND}
    ARGS -E copy_if_different ${CMAKE_CURRENT_SOURCE_DIR}/${class}.cc
      ${CMAKE_CURRENT_BINARY_DIR}/${class}.cc
    # mkoctfile leaves .o files in the directory where it is called, so we
    # must call it in the binary build directory
    COMMAND ${MKOCTFILE}
    ARGS --verbose ${WNODEPRECATED} ${INCLUDE_FLAGS} -I${CMAKE_CURRENT_SOURCE_DIR}
      ${LINK_FLAGS} ${APPLE_FLAG}
      #-Wl,-rpath=${OCTAVIZ_RPATH}
      --output ${CMAKE_CURRENT_BINARY_DIR}/${class}.oct
      ${CMAKE_CURRENT_BINARY_DIR}/${class}.cc
    DEPENDS octaviz
    COMMENT "Building Octaviz' ${class}.oct from ${class}.cc"
    )
  SET (common_octs ${common_octs} ${CMAKE_CURRENT_BINARY_DIR}/${class}.oct)
ENDFOREACH(class)

# Build the few .oct files as part of a "OctavizCommonFiles" target
ADD_CUSTOM_TARGET( OctavizCommonFiles ALL DEPENDS octaviz ${common_octs} )

# Install the .oct files and the shared library
INSTALL(PROGRAMS ${common_octs} DESTINATION ${OCTAVIZ_OCT_DIR})
INSTALL(TARGETS octaviz LIBRARY DESTINATION ${OCTAVIZ_SHARED_DIR})
