SET( UMFPACK_FOUND "NO" )

if (UMFPACK_INCLUDES AND UMFPACK_LIBRARIES)
  set(UMFPACK_FIND_QUIETLY TRUE)
endif (UMFPACK_INCLUDES AND UMFPACK_LIBRARIES)

find_package(BLAS REQUIRED)

if(BLAS_FOUND)

  find_path(UMFPACK_INCLUDES
    NAMES
    umfpack.h
    PATHS
    $ENV{UMFPACKDIR}
    ${INCLUDE_INSTALL_DIR}
    PATH_SUFFIXES
    suitesparse
  )

  find_library(UMFPACK_LIBRARIES umfpack PATHS $ENV{UMFPACKDIR} ${LIB_INSTALL_DIR})

  if(UMFPACK_LIBRARIES)

    get_filename_component(UMFPACK_LIBDIR ${UMFPACK_LIBRARIES} PATH)

    find_library(AMD_LIBRARY amd PATHS ${UMFPACK_LIBDIR} $ENV{UMFPACKDIR} ${LIB_INSTALL_DIR})
    if (AMD_LIBRARY)
      set(UMFPACK_LIBRARIES ${UMFPACK_LIBRARIES} ${AMD_LIBRARY})
    else (AMD_LIBRARY)
      MESSAGE ( FATAL_ERROR "AMD library not found!" )
      set(UMFPACK_LIBRARIES)
    endif (AMD_LIBRARY)
    
    find_library(CHOLMOD_LIBRARY cholmod PATHS ${UMFPACK_LIBDIR} $ENV{UMFPACKDIR} ${LIB_INSTALL_DIR})
    if (CHOLMOD_LIBRARY)
      set(UMFPACK_LIBRARIES ${UMFPACK_LIBRARIES} ${CHOLMOD_LIBRARY})
    else (CHOLMOD_LIBRARY)
      MESSAGE ( FATAL_ERROR "CHOLAMD library not found!" )
      set(UMFPACK_LIBRARIES)
    endif (CHOLMOD_LIBRARY)
    
    find_library(COLAMD_LIBRARY colamd PATHS ${UMFPACK_LIBDIR} $ENV{UMFPACKDIR} ${LIB_INSTALL_DIR})
    if (COLAMD_LIBRARY)
      set(UMFPACK_LIBRARIES ${UMFPACK_LIBRARIES} ${COLAMD_LIBRARY})
    else (COLAMD_LIBRARY)
      MESSAGE ( FATAL_ERROR "COLAMD library not found!" )
      set(UMFPACK_LIBRARIES)
    endif (COLAMD_LIBRARY)
    
    find_library(CAMD_LIBRARY camd PATHS ${UMFPACK_LIBDIR} $ENV{UMFPACKDIR} ${LIB_INSTALL_DIR})
    if (CAMD_LIBRARY)
      set(UMFPACK_LIBRARIES ${UMFPACK_LIBRARIES} ${CAMD_LIBRARY})
    else (CAMD_LIBRARY)
      MESSAGE ( FATAL_ERROR "CAMD library not found!" )
      set(UMFPACK_LIBRARIES)
    endif (CAMD_LIBRARY)
    
    find_library(CCOLAMD_LIBRARY ccolamd PATHS ${UMFPACK_LIBDIR} $ENV{UMFPACKDIR} ${LIB_INSTALL_DIR})
    if (CCOLAMD_LIBRARY)
      set(UMFPACK_LIBRARIES ${UMFPACK_LIBRARIES} ${CCOLAMD_LIBRARY})
    else (CCOLAMD_LIBRARY)
      MESSAGE ( FATAL_ERROR "CCOLAMD library not found!" )
      set(UMFPACK_LIBRARIES)
    endif (CCOLAMD_LIBRARY)
    
    find_library(METIS_LIBRARY metis PATHS ${UMFPACK_LIBDIR} $ENV{UMFPACKDIR} ${LIB_INSTALL_DIR})
    if (METIS_LIBRARY)
      set(UMFPACK_LIBRARIES ${UMFPACK_LIBRARIES} ${METIS_LIBRARY})
    else (METIS_LIBRARY)
      MESSAGE ( STATUS "METIS library not found!" )
#      set(UMFPACK_LIBRARIES)
    endif (METIS_LIBRARY)

    find_library(SPARSECONFIG_LIBRARY suitesparseconfig PATHS ${UMFPACK_LIBDIR} $ENV{UMFPACKDIR} ${LIB_INSTALL_DIR})
    if (SPARSECONFIG_LIBRARY)
      set(UMFPACK_LIBRARIES ${UMFPACK_LIBRARIES} ${SPARSECONFIG_LIBRARY})
    else (SPARSECONFIG_LIBRARY)
      message ("paths : " ${PATHS})
      MESSAGE ( FATAL_ERROR "SPARSECONFIG library not found!" )
      set(UMFPACK_LIBRARIES)
    endif (SPARSECONFIG_LIBRARY)


  endif(UMFPACK_LIBRARIES)

  if(UMFPACK_LIBRARIES)
    set(UMFPACK_LIBRARIES ${UMFPACK_LIBRARIES} ${BLAS_LIBRARIES} gfortran)
    SET( UMFPACK_FOUND "YES" )
  endif(UMFPACK_LIBRARIES)

endif(BLAS_FOUND)


include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(UMFPACK DEFAULT_MSG
                                  UMFPACK_INCLUDES UMFPACK_LIBRARIES)

mark_as_advanced(UMFPACK_INCLUDES UMFPACK_LIBRARIES AMD_LIBRARY COLAMD_LIBRARY)

