# Searches for an installation of the CLAPACK library. On success, it sets the following variables:
#
#   CLAPACK_FOUND         Set to true to indicate the library was found
#   CLAPACK_LIBRARIES     All libraries needed to use CLAPACK (with full path)
#   CLAPACK_INCLUDE_DIRS  Directories containing CLAPACK header files
#
# To specify an additional directory to search, set CLAPACK_ROOT.
#
# Author: Siddhartha Chaudhuri, 2012
#

# Look for the header, first in the user-specified location and then in the system locations
SET(CLAPACK_INCLUDE_DOC "The directory containing the header file clapack.h")
FIND_PATH(CLAPACK_INCLUDE_DIRS NAMES clapack.h PATHS ${CLAPACK_ROOT} ${CLAPACK_ROOT}/include DOC ${CLAPACK_INCLUDE_DOC}
          NO_DEFAULT_PATH)
IF(NOT CLAPACK_INCLUDE_DIRS)  # now look in system locations
  FIND_PATH(CLAPACK_INCLUDE_DIRS NAMES clapack.h DOC ${CLAPACK_INCLUDE_DOC})
ENDIF(NOT CLAPACK_INCLUDE_DIRS)

SET(CLAPACK_FOUND FALSE)

IF(CLAPACK_INCLUDE_DIRS)
  SET(CLAPACK_LIBRARY_DIRS ${CLAPACK_INCLUDE_DIRS})

  IF("${CLAPACK_LIBRARY_DIRS}" MATCHES "/include$")
    # Strip off the trailing "/include" in the path.
    GET_FILENAME_COMPONENT(CLAPACK_LIBRARY_DIRS ${CLAPACK_LIBRARY_DIRS} PATH)
  ENDIF("${CLAPACK_LIBRARY_DIRS}" MATCHES "/include$")

  IF(EXISTS "${CLAPACK_LIBRARY_DIRS}/lib")
    SET(CLAPACK_LIBRARY_DIRS ${CLAPACK_LIBRARY_DIRS}/lib)
  ENDIF(EXISTS "${CLAPACK_LIBRARY_DIRS}/lib")

  # Find LAPACK library
  FIND_LIBRARY(CLAPACK_DEBUG_LIBRARY
               NAMES clapackd lapackd liblapackd LAPACKd libLAPACKd
               PATHS  ${CLAPACK_LIBRARY_DIRS} NO_DEFAULT_PATH)

  FIND_LIBRARY(CLAPACK_RELEASE_LIBRARY
               NAMES clapack lapack liblapack LAPACK libLAPACK
               PATHS  ${CLAPACK_LIBRARY_DIRS} NO_DEFAULT_PATH)

  # If we didn't find the libs in the location adjacent to the header, so just look in the system paths
  IF(NOT CLAPACK_DEBUG_LIBRARY)
    IF(NOT CLAPACK_RELEASE_LIBRARY)
      FIND_LIBRARY(CLAPACK_DEBUG_LIBRARY NAMES clapackd lapackd liblapackd LAPACKd libLAPACKd)
      FIND_LIBRARY(CLAPACK_RELEASE_LIBRARY NAMES clapack lapack liblapack LAPACK libLAPACK)
    ENDIF(NOT CLAPACK_RELEASE_LIBRARY)
  ENDIF(NOT CLAPACK_DEBUG_LIBRARY)

  SET(CLAPACK_LIBRARIES)
  IF(CLAPACK_DEBUG_LIBRARY AND CLAPACK_RELEASE_LIBRARY)
    SET(CLAPACK_LIBRARIES debug ${CLAPACK_DEBUG_LIBRARY} optimized ${CLAPACK_RELEASE_LIBRARY})
  ELSEIF(CLAPACK_DEBUG_LIBRARY)
    SET(CLAPACK_LIBRARIES ${CLAPACK_DEBUG_LIBRARY})
  ELSEIF(CLAPACK_RELEASE_LIBRARY)
    SET(CLAPACK_LIBRARIES ${CLAPACK_RELEASE_LIBRARY})
  ENDIF(CLAPACK_DEBUG_LIBRARY AND CLAPACK_RELEASE_LIBRARY)

  IF(CLAPACK_LIBRARIES)
    SET(CLAPACK_FOUND TRUE)
  ENDIF(CLAPACK_LIBRARIES)

  # Find BLAS library, if it exists
  FIND_LIBRARY(CLAPACK_BLAS_DEBUG_LIBRARY
               NAMES blasd libblasd BLASd libBLASd
               PATHS  ${CLAPACK_LIBRARY_DIRS} NO_DEFAULT_PATH)

  FIND_LIBRARY(CLAPACK_BLAS_RELEASE_LIBRARY
               NAMES blas libblas BLAS libBLAS
               PATHS  ${CLAPACK_LIBRARY_DIRS} NO_DEFAULT_PATH)

  # If we didn't find the BLAS libs in the location adjacent to the header, so just look in the system paths
  IF(NOT CLAPACK_BLAS_DEBUG_LIBRARY)
    IF(NOT CLAPACK_BLAS_RELEASE_LIBRARY)
      FIND_LIBRARY(CLAPACK_BLAS_DEBUG_LIBRARY NAMES blasd libblasd BLASd libBLASd)
      FIND_LIBRARY(CLAPACK_BLAS_RELEASE_LIBRARY NAMES blas libblas BLAS libBLAS)
    ENDIF(NOT CLAPACK_BLAS_RELEASE_LIBRARY)
  ENDIF(NOT CLAPACK_BLAS_DEBUG_LIBRARY)

  SET(CLAPACK_BLAS_LIBRARIES)
  IF(CLAPACK_BLAS_DEBUG_LIBRARY AND CLAPACK_BLAS_RELEASE_LIBRARY)
    SET(CLAPACK_BLAS_LIBRARIES debug ${CLAPACK_BLAS_DEBUG_LIBRARY} optimized ${CLAPACK_BLAS_RELEASE_LIBRARY})
  ELSEIF(CLAPACK_BLAS_DEBUG_LIBRARY)
    SET(CLAPACK_BLAS_LIBRARIES ${CLAPACK_BLAS_DEBUG_LIBRARY})
  ELSEIF(CLAPACK_BLAS_RELEASE_LIBRARY)
    SET(CLAPACK_BLAS_LIBRARIES ${CLAPACK_BLAS_RELEASE_LIBRARY})
  ENDIF(CLAPACK_BLAS_DEBUG_LIBRARY AND CLAPACK_BLAS_RELEASE_LIBRARY)

  # Find f2c library, if it exists
  FIND_LIBRARY(CLAPACK_F2C_DEBUG_LIBRARY
               NAMES f2cd libf2cd F2Cd libF2Cd
               PATHS  ${CLAPACK_LIBRARY_DIRS} NO_DEFAULT_PATH)

  FIND_LIBRARY(CLAPACK_F2C_RELEASE_LIBRARY
               NAMES f2c libf2c F2C libF2C
               PATHS  ${CLAPACK_LIBRARY_DIRS} NO_DEFAULT_PATH)

  # If we didn't find the f2c libs in the location adjacent to the header, so just look in the system paths
  IF(NOT CLAPACK_F2C_DEBUG_LIBRARY)
    IF(NOT CLAPACK_F2C_RELEASE_LIBRARY)
      FIND_LIBRARY(CLAPACK_F2C_DEBUG_LIBRARY NAMES f2cd libf2cd F2Cd libF2Cd)
      FIND_LIBRARY(CLAPACK_F2C_RELEASE_LIBRARY NAMES f2c libf2c F2C libF2C)
    ENDIF(NOT CLAPACK_F2C_RELEASE_LIBRARY)
  ENDIF(NOT CLAPACK_F2C_DEBUG_LIBRARY)

  SET(CLAPACK_F2C_LIBRARIES)
  IF(CLAPACK_F2C_DEBUG_LIBRARY AND CLAPACK_F2C_RELEASE_LIBRARY)
    SET(CLAPACK_F2C_LIBRARIES debug ${CLAPACK_F2C_DEBUG_LIBRARY} optimized ${CLAPACK_F2C_RELEASE_LIBRARY})
  ELSEIF(CLAPACK_F2C_DEBUG_LIBRARY)
    SET(CLAPACK_F2C_LIBRARIES ${CLAPACK_F2C_DEBUG_LIBRARY})
  ELSEIF(CLAPACK_F2C_RELEASE_LIBRARY)
    SET(CLAPACK_F2C_LIBRARIES ${CLAPACK_F2C_RELEASE_LIBRARY})
  ENDIF(CLAPACK_F2C_DEBUG_LIBRARY AND CLAPACK_F2C_RELEASE_LIBRARY)

  SET(CLAPACK_LIBRARIES ${CLAPACK_LIBRARIES} ${CLAPACK_BLAS_LIBRARIES} ${CLAPACK_F2C_LIBRARIES})

ENDIF(CLAPACK_INCLUDE_DIRS)

IF(CLAPACK_FOUND)
  IF(NOT CLAPACK_FIND_QUIETLY)
    MESSAGE(STATUS "Found CLAPACK: headers at ${CLAPACK_INCLUDE_DIRS}, libraries at ${CLAPACK_LIBRARY_DIRS}")
  ENDIF(NOT CLAPACK_FIND_QUIETLY)
ELSE(CLAPACK_FOUND)
  IF(CLAPACK_FIND_REQUIRED)
    MESSAGE(FATAL_ERROR "CLAPACK not found")
  ENDIF(CLAPACK_FIND_REQUIRED)
ENDIF(CLAPACK_FOUND)
