IF (NOT MAGMA_FOUND)

# Setting default paths
IF (NOT MAGMA_INCLUDE_DIR)
  SET (MAGMA_INCLUDE_DIR /usr/local/magma/include)
ENDIF (NOT MAGMA_INCLUDE_DIR)
MESSAGE (STATUS "MAGMA_INCLUDE_DIR : ${MAGMA_INCLUDE_DIR}")
IF (NOT MAGMA_LIBRARY_DIR)
  SET (MAGMA_LIBRARY_DIR /usr/local/magma/lib)
ENDIF (NOT MAGMA_LIBRARY_DIR)
MESSAGE (STATUS "MAGMA_LIBRARY_DIR : ${MAGMA_LIBRARY_DIR}")

# Check whether header files are in MAGMA_INCLUDE_DIR or not.
MESSAGE (STATUS "Checking for MAGMA header files")
SET (HEADER_FILES "magma.h;magma_lapack.h")
FOREACH (HEADER_FILE ${HEADER_FILES})
  MESSAGE (STATUS "  Looking for ${HEADER_FILE}")
  FIND_PATH (HEADER_FILE_PATH NAMES ${HEADER_FILE} HINTS "${MAGMA_INCLUDE_DIR}/magma")
  IF (HEADER_FILE_PATH)
    MESSAGE (STATUS "  Looking for ${HEADER_FILE} - found")
  ELSE ()
    MESSAGE (FATAL_ERROR "  ${HEADER_FILE} is not detected.")
  ENDIF (HEADER_FILE_PATH)
ENDFOREACH ()

# Check library
FIND_LIBRARY (MAGMA_LIBRARY_DIR NAMES magma libmagma HINTS ${MAGMA_LIBRARY_DIR})
IF (MAGMA_LIBRARY_DIR)
  MESSAGE (STATUS "MAGMA library found")
ELSE()
  MESSAGE (FATAL_ERROR "  Library MAGMA is not detected.")
ENDIF (MAGMA_LIBRARY_DIR)

# final
SET (MAGMA_FOUND TRUE)
INCLUDE_DIRECTORIES (SYSTEM ${MAGMA_INCLUDE_DIR})
LINK_DIRECTORIES(${MAGMA_LIBRARY_DIR})
LINK_DIRECTORIES(${CUBLAS_LIBRARY_DIR})
SET (MAGMA_LIBRARIES "magma")
ENDIF (NOT MAGMA_FOUND)
