# - this module looks for Matlab
# Defines:
#  MATLAB_INCLUDE_DIR: include path for mex.h, engine.h
#  MATLAB_LIBRARIES:   required libraries: libmex, etc
#  MATLAB_MEX_LIBRARY: path to libmex.lib
#  MATLAB_MX_LIBRARY:  path to libmx.lib
#  MATLAB_MAT_LIBRARY:  path to libmat.lib # added
#  MATLAB_ENG_LIBRARY: path to libeng.lib
#  MATLAB_ROOT: path to Matlab's root directory
#  MATLAB_MEX_EXTENSION: appropriate extension to use

# This is a derivative work of file FindMatlab.cmake released with
# CMake v2.8

SET(MATLAB_FOUND 0)
IF(WIN32)

  IF((NOT DEFINED MATLAB_ROOT) OR ("${MATLAB_ROOT}" STREQUAL ""))
    # Search default install directory
    FILE(GLOB MATLAB_DIRS RELATIVE C:/Program\ Files/MATLAB/ C:/Program\ Files/MATLAB/*)
    FOREACH(MATLAB_DIR ${MATLAB_DIRS})
      IF((NOT DEFINED MATLAB_ROOT) OR ("${MATLAB_ROOT}" STREQUAL ""))
        IF(EXISTS "C:/Program Files/MATLAB/${MATLAB_DIR}")
          SET(MATLAB_ROOT "C:/Program Files/MATLAB/${MATLAB_DIR}")
        ENDIF(EXISTS "C:/Program Files/MATLAB/${MATLAB_DIR}")
      ENDIF((NOT DEFINED MATLAB_ROOT) OR ("${MATLAB_ROOT}" STREQUAL ""))
    ENDFOREACH(MATLAB_DIR)
  ENDIF((NOT DEFINED MATLAB_ROOT) OR ("${MATLAB_ROOT}" STREQUAL ""))

  IF((NOT DEFINED MATLAB_ROOT) OR ("${MATLAB_ROOT}" STREQUAL ""))
    # Search registry for Matlab
    FOREACH(MATVER "8.14" "8.13" "8.12" "8.11" "8.10" "8.9" "8.8" "8.7" "8.6" "8.5" "8.4" "8.3" "8.2" "8.1" "8")
      IF((NOT DEFINED MATLAB_ROOT)
          OR ("${MATLAB_ROOT}" STREQUAL "")
          OR ("${MATLAB_ROOT}" STREQUAL "/registry"))
        GET_FILENAME_COMPONENT(MATLAB_ROOT
          "[HKEY_LOCAL_MACHINE\\SOFTWARE\\MathWorks\\MATLAB\\${MATVER};MATLABROOT]"
          ABSOLUTE)
        SET(MATLAB_VERSION ${MATVER})
      ENDIF((NOT DEFINED MATLAB_ROOT) 
        OR ("${MATLAB_ROOT}" STREQUAL "") 
        OR ("${MATLAB_ROOT}" STREQUAL "/registry"))
    ENDFOREACH(MATVER)
  ENDIF((NOT DEFINED MATLAB_ROOT) OR ("${MATLAB_ROOT}" STREQUAL ""))  

  # Determine architecture
  IF((DEFINED CMAKE_SIZEOF_VOID_P) AND (CMAKE_SIZEOF_VOID_P MATCHES "4"))
    SET(MATLAB_ARCH "32")
  ELSE((DEFINED CMAKE_SIZEOF_VOID_P) AND (CMAKE_SIZEOF_VOID_P MATCHES "4"))
    SET(MATLAB_ARCH "64")
  ENDIF((DEFINED CMAKE_SIZEOF_VOID_P) AND (CMAKE_SIZEOF_VOID_P MATCHES "4"))

  IF((NOT DEFINED MATLAB_MEX_EXTENSION) OR ("${MATLAB_MEX_EXTENSION}" STREQUAL ""))  
    SET(MATLAB_MEX_EXTENSION "mexw${MATLAB_ARCH}")
  ENDIF((NOT DEFINED MATLAB_MEX_EXTENSION) OR ("${MATLAB_MEX_EXTENSION}" STREQUAL ""))  

  IF((DEFINED MATLAB_ROOT) AND (NOT "${MATLAB_ROOT}" STREQUAL ""))
    # Folder where the MEX libraries are, depending of the Windows compiler
    IF((NOT DEFINED MATLAB_LIBRARIES_DIR) OR ("${MATLAB_LIBRARIES_DIR}" STREQUAL ""))
      #SET(MATLAB_LIBRARIES_DIR "${MATLAB_ROOT}/extern/lib/win${MATLAB_ARCH}/microsoft"
      #                         "${MATLAB_ROOT}/bin/win${MATLAB_ARCH}")
      SET(MATLAB_LIBRARIES_DIR  "${MATLAB_ROOT}/bin/win${MATLAB_ARCH}"
                                "${MATLAB_ROOT}/extern/lib/win${MATLAB_ARCH}/microsoft")

    ENDIF((NOT DEFINED MATLAB_LIBRARIES_DIR) OR ("${MATLAB_LIBRARIES_DIR}" STREQUAL ""))
  ENDIF((DEFINED MATLAB_ROOT) AND (NOT "${MATLAB_ROOT}" STREQUAL ""))

ELSEIF(APPLE)

  IF((NOT DEFINED MATLAB_ROOT) OR ("${MATLAB_ROOT}" STREQUAL ""))
    # Find location using the 'which' command
    EXECUTE_PROCESS(
      COMMAND which matlab
      COMMAND xargs readlink
      COMMAND xargs dirname
      COMMAND xargs dirname
      COMMAND xargs echo -n
      OUTPUT_VARIABLE MATLAB_ROOT
      )
  ENDIF((NOT DEFINED MATLAB_ROOT) OR ("${MATLAB_ROOT}" STREQUAL ""))

  IF((NOT DEFINED MATLAB_ROOT) OR ("${MATLAB_ROOT}" STREQUAL ""))
    # Search default install directory
    FILE(GLOB MATLAB_DIRS RELATIVE /Applications /Applications/MATLAB_*.app)
    FOREACH(MATLAB_DIR ${MATLAB_DIRS})
      IF((NOT DEFINED MATLAB_ROOT) OR ("${MATLAB_ROOT}" STREQUAL ""))
        IF(EXISTS /Applications/{MATLAB_DIR})
          SET(MATLAB_ROOT /Applications/${MATLAB_DIR})
        ENDIF(EXISTS /Applications/${MATLAB_DIR})
      ENDIF((NOT DEFINED MATLAB_ROOT) OR ("${MATLAB_ROOT}" STREQUAL ""))
    ENDFOREACH(MATLAB_DIR)
  ENDIF((NOT DEFINED MATLAB_ROOT) OR ("${MATLAB_ROOT}" STREQUAL ""))

  IF((NOT DEFINED MATLAB_MEX_EXTENSION) OR ("${MATLAB_MEX_EXTENSION}" STREQUAL ""))  
    SET(MATLAB_MEX_EXTENSION "mexmaci64")
  ENDIF((NOT DEFINED MATLAB_MEX_EXTENSION) OR ("${MATLAB_MEX_EXTENSION}" STREQUAL "")) 

  IF((DEFINED MATLAB_ROOT) AND (NOT "${MATLAB_ROOT}" STREQUAL ""))
    # Folder where the MEX libraries are
    IF((NOT DEFINED MATLAB_LIBRARIES_DIR) OR ("${MATLAB_LIBRARIES_DIR}" STREQUAL ""))
      SET(MATLAB_LIBRARIES_DIR "${MATLAB_ROOT}/extern/lib/maci64"
                               "${MATLAB_ROOT}/bin/maci64")
    ENDIF((DEFINED MATLAB_LIBRARIES_DIR) AND (NOT "${MATLAB_LIBRARIES_DIR}" STREQUAL ""))
  ENDIF((NOT DEFINED MATLAB_LIBRARIES_DIR) OR ("${MATLAB_LIBRARIES_DIR}" STREQUAL "")) 

ELSEIF(UNIX)

  IF((NOT DEFINED MATLAB_ROOT) OR ("${MATLAB_ROOT}" STREQUAL ""))
    # Find location using the 'which' command
    EXECUTE_PROCESS(
      COMMAND which matlab
      COMMAND xargs readlink
      COMMAND xargs dirname
      COMMAND xargs dirname
      COMMAND xargs echo -n
      OUTPUT_VARIABLE MATLAB_ROOT
      )
  ENDIF((NOT DEFINED MATLAB_ROOT) OR ("${MATLAB_ROOT}" STREQUAL ""))

  IF((NOT DEFINED MATLAB_ROOT) OR ("${MATLAB_ROOT}" STREQUAL ""))
    # Search default install directory
    FILE(GLOB MATLAB_DIRS RELATIVE /usr/local/MATLAB /usr/local/MATLAB/*)
    FOREACH(MATLAB_DIR ${MATLAB_DIRS})
      IF((NOT DEFINED MATLAB_ROOT) OR ("${MATLAB_ROOT}" STREQUAL ""))
        IF(EXISTS /usr/local/MATLAB/${MATLAB_DIR})
          SET(MATLAB_ROOT /usr/local/MATLAB/${MATLAB_DIR})
        ENDIF(EXISTS /usr/local/MATLAB/${MATLAB_DIR})
      ENDIF((NOT DEFINED MATLAB_ROOT) OR ("${MATLAB_ROOT}" STREQUAL ""))
    ENDFOREACH(MATLAB_DIR)
  ENDIF((NOT DEFINED MATLAB_ROOT) OR ("${MATLAB_ROOT}" STREQUAL ""))

  IF((NOT DEFINED MATLAB_MEX_EXTENSION) OR ("${MATLAB_MEX_EXTENSION}" STREQUAL ""))  
    SET(MATLAB_MEX_EXTENSION "mexa64")
  ENDIF((NOT DEFINED MATLAB_MEX_EXTENSION) OR ("${MATLAB_MEX_EXTENSION}" STREQUAL ""))  

  IF((DEFINED MATLAB_ROOT) AND (NOT "${MATLAB_ROOT}" STREQUAL ""))
    # Folder where the MEX libraries are, depending of the Windows compiler
    IF((NOT DEFINED MATLAB_LIBRARIES_DIR) OR ("${MATLAB_LIBRARIES_DIR}" STREQUAL ""))
      SET(MATLAB_LIBRARIES_DIR "${MATLAB_ROOT}/extern/lib/glnxa64"
                               "${MATLAB_ROOT}/bin/glnxa64")
    ENDIF((DEFINED MATLAB_LIBRARIES_DIR) AND (NOT "${MATLAB_LIBRARIES_DIR}" STREQUAL ""))
  ENDIF((NOT DEFINED MATLAB_LIBRARIES_DIR) OR ("${MATLAB_LIBRARIES_DIR}" STREQUAL ""))

ENDIF(WIN32)

# Get paths to the Matlab MEX libraries
FIND_LIBRARY(MATLAB_MEX_LIBRARY
  libmex
  ${MATLAB_LIBRARIES_DIR}
  )
FIND_LIBRARY(MATLAB_MX_LIBRARY
  libmx
  ${MATLAB_LIBRARIES_DIR}
  )
FIND_LIBRARY(MATLAB_MAT_LIBRARY
  libmat
  ${MATLAB_LIBRARIES_DIR}
  )
FIND_LIBRARY(MATLAB_ENG_LIBRARY
  libeng
  ${MATLAB_LIBRARIES_DIR}
  )

# Get path to the include directory
FIND_PATH(MATLAB_INCLUDE_DIR
  "mex.h"
  "${MATLAB_ROOT}/extern/include"
  )

# This is common to UNIX and Win32:
SET(MATLAB_LIBRARIES
  ${MATLAB_MEX_LIBRARY}
  ${MATLAB_MX_LIBRARY}
  ${MATLAB_ENG_LIBRARY}
)

message("MATLAB root:   ${MATLAB_ROOT}")
message("MATLAB libdir: ${MATLAB_LIBRARIES_DIR}")
message("MATLAB incdir: ${MATLAB_INCLUDE_DIR}")
message("MATLAB libs:   ${MATLAB_LIBRARIES}")

IF(MATLAB_INCLUDE_DIR AND MATLAB_LIBRARIES)
  SET(MATLAB_FOUND 1)
ENDIF(MATLAB_INCLUDE_DIR AND MATLAB_LIBRARIES)

MARK_AS_ADVANCED(
  MATLAB_LIBRARIES
  MATLAB_MEX_LIBRARY
  MATLAB_MX_LIBRARY
  MATLAB_ENG_LIBRARY
  MATLAB_INCLUDE_DIR
  MATLAB_FOUND
  MATLAB_MEX_EXTENSION
)
