# -*- mode: cmake; coding: utf-8; cmake-tab-width: 4; indent-tabs-mode: nil; -*- vim:fenc=utf-8:ft=cmake:et:sw=4:ts=4:sts=4

#========================================================================
# Discover if Octave is available and usable
#========================================================================
set( _OCTAVE_MINIMUM 3.0 )
set( _OCTAVE_MAXIMUM 6.0 )

if (APPLE AND UNIX AND NOT DEFINED ENABLE_SWIG_OCTAVE)
    #--------------------------------------------------------------------
    # Currently there is an issue were SWIG and Octave are incompatable
    #   under MacPorts
    #--------------------------------------------------------------------
    set(ENABLE_SWIG_OCTAVE FALSE)
endif()
if ( NOT DEFINED ENABLE_SWIG_OCTAVE OR ENABLE_SWIG_OCTAVE )
    find_package(HDF5)
    find_package(Octave)
    if ( OCTAVE_FOUND AND
            ( ( OCTAVE_VERSION_STRING VERSION_LESS ${_OCTAVE_MINIMUM} ) OR
              ( OCTAVE_VERSION_STRING VERSION_EQUAL ${_OCTAVE_MAXIMUM} ) OR
              ( OCTAVE_VERSION_STRING VERSION_GREATER ${_OCTAVE_MAXIMUM} ) ) )
        #----------------------------------------------------------------
        # NOT in range of what is supported
        #----------------------------------------------------------------
        get_property( _helpstring CACHE OCTAVE_FOUND PROPERTY HELPSTRING )
        if ( NOT _helpstring )
            set( _helpstring "State of having found Octave" )
        endif( )
        get_property( _type CACHE OCTAVE_FOUND PROPERTY TYPE )
        if ( NOT _type )
            set( _type BOOL )
        endif( )
        message( INFO "_helpstring: ${_helpstring}" )
        message( INFO "_type: ${_type}" )
        set( OCTAVE_FOUND False CACHE ${_type} ${_helpstring} FORCE )
    endif( )
endif( )

if (OCTAVE_FOUND AND SWIG_FOUND)
  set( ENABLE_SWIG_OCTAVE "yes"
      CACHE BOOL "Control the building of the SWIG bindings for Octave" )
  set( OCTAVE_VERSION_STRING ${OCTAVE_VERSION_STRING} PARENT_SCOPE )
  find_program( OCTAVE_CONFIG_PROGRAM octive-config )

  if ( OCTAVE_CONFIG_PROGRAM_FOUND )
      execute_process(
          COMMAND ${OCTAVE_CONFIGURE_PROGRAM} --print OCTLIBDIR
          RESULT_VARIABLE OCTAVE_OCTLIBDIR )
  endif( )
  # ---------------------------------------------------------------------
  # Library: octave
  # ---------------------------------------------------------------------
  find_library( OCTAVE_OCTAVE_LIBRARY
      NAMES octave
		  HINTS ${OCTAVE_LINK_DIRS} )
  find_library( OCTAVE_INTERP_LIBRARY
      NAMES octinterp
		  HINTS ${OCTAVE_LINK_DIRS} )
  set( OCTAVE_LIBRARIES
       ${OCTAVE_INTERP_LIBRARY}
       ${OCTAVE_OCTAVE_LIBRARY} )

else( )
  set( ENABLE_SWIG_OCTAVE "no"
       CACHE BOOL "Control the building of the SWIG bindings for Octave" FORCE )
endif(OCTAVE_FOUND AND SWIG_FOUND)

add_subdirectory(module)
add_subdirectory(test)

#========================================================================
# Provide additional information regarding components
#========================================================================

cpack_add_component(Octave
    DISPLAY_NAME "Octave extensions"
    DESCRIPTION
        "Pieces needed to use the nds2 client from Octave"
    GROUP Runtime)
