#This configuration file is shipped with the great FEM-Toolbox AMDiS.
#It simplifies the configure of different amdis-version and sets some variables.
#We set the following variables:
# AMDIS_INCLUDE_DIR 	the amdis-directory only
# AMDIS_INCLUDE_DIRS	all include directories (mtl, umfpack, parmetis, metis, ...)
# AMDIS_LIBRARIES	the needed libraries
# AMDIS_LIBRARY_DIRS	the link directories
# AMDiS_COMPILEFLAGS	some compile flags for amdis
#
#
# For a fast start, you use this package in the following way
# simply look for it, possibly set the AMDiS directory
# find_package(AMDiS )
# include(${AMDIS_USE_FILE})
# add_executable(yourTarget <yourSources>)
# target_link_libraries(yourTarget ${AMDIS_LIBRARIES})
#
#
#The AMDIS_USE_FILE adds the include and link-directories for amdis to cmake.
# 
#If you use the parallel amdis-version, the AMDIS_USE_FILE also trys to detect
#the mpi and petsc configuration. If those versions don't fit your needs,
#you can change the corresponding directories, or simply do this work at your own
#and omit the AMDIS_USE_FILE. Then you also have to set the flags, include 
#and library directories.


#try to detect the AMDiS include directory
find_file(_AMDIS_H AMDiS.h PATHS ${AMDIS_INCLUDE_DIR} ${AMDIS_DIR}/../../include/amdis/ /usr/include/amdis/)
if(_AMDIS_H)
	get_filename_component(AMDIS_INCLUDE_DIR ${_AMDIS_H} PATH CACHE)
	set(AMDIS_INCLUDE_DIRS ${AMDIS_INCLUDE_DIR})
else()
	message(ERROR "could not detect the AMDiS include directory. Please set the variable AMDIS_INCLUDE_DIR to the directory containing the AMDiS headers.")
endif()
unset(_AMDIS_H CACHE)
set(MTL_DIR ${AMDIS_INCLUDE_DIR}/mtl4 CACHE PATH "the mtl directory")
list(APPEND AMDIS_INCLUDE_DIRS ${MTL_DIR})

find_library(_AMDIS_LIB amdis PATHS ${AMDIS_LIBRARY_DIR} ${AMDIS_DIR}/../../lib/amdis/ /usr/include/amdis)
if(_AMDIS_LIB)
	get_filename_component(AMDIS_LIBRARY_DIR ${_AMDIS_LIB} PATH CACHE)
	set(AMDIS_LIBRARY_DIRS ${AMDIS_LIBRARY_DIR})
	set(AMDIS_LIBRARIES "${_AMDIS_LIB};${AMDIS_LIBRARY_DIR}/libcompositeFEM.so" CACHE STRING "amdis libraries")
else()
	message(ERROR "could not detect the AMDiS library directory. Please set the variable AMDIS_LIBRARY_DIR to the directory containg the AMDiS library")
endif()
unset(_AMDIS_LIB CACHE)

find_package(Boost 1.42 REQUIRED system iostreams filesystem)
if(Boost_FOUND)
	list(APPEND AMDIS_LIBRARIES ${Boost_LIBRARIES})
	list(APPEND AMDIS_LIBRARY_DIRS ${Boost_LIBRARY_DIRS})
	list(APPEND AMDIS_INCLUDE_DIRS ${Boost_INCLUDE_DIR})
endif(Boost_FOUND)

set(AMDiS_NEED_PARMETIS @ENABLE_PARMETIS@)
set(AMDiS_NEED_ZOLTAN @ENABLE_ZOLTAN@)
set(AMDiS_HAS_PARALLEL_DOMAIN @ENABLE_PARALLEL_DOMAIN@)
set(AMDiS_NEED_UMFPACK @ENABLE_UMFPACK@)
set(AMDiS_NEED_MKL @ENABLE_MKL@)
set(AMDIS_USE_FILE ${AMDIS_DIR}/AMDiSUse.cmake)
set(AMDiS_COMPILEFLAGS "@COMPILEFLAGS@")
set(AMDIS_VERSION @CurrentRevision@)
set(AMDIS_MAJOR_VERSION @AMDIS_MAJOR@)
set(AMDIS_MINOR_VERSION @AMDIS_MINOR@)

if(AMDiS_NEED_UMFPACK)
	#look for umfpack, we need the headers only
	find_file(UMFPACK_H umfpack.h
			HINTS /usr/include /usr/include/suitesparse 
			DOC "headerfile umfpack.h for UMFPACK")
	if( UMFPACK_H )
		#construct the include path
		get_filename_component(UMFPACK_PATH ${UMFPACK_H} PATH)
		#look for ufconfig
		find_file(UFCONFIG_H UFconfig.h
				HINTS /usr/include ${UMFPACK_PATH}
				DOC "headerfile UFConfig.h")
		if( UFCONFIG_H )
			get_filename_component(UFCONFIG_PATH ${UFCONFIG_H} PATH)
		else()
			message(FATAL_ERROR "could not find UFConfig.h, needed for umfpack")
		endif()
		#look for amd
		find_file(AMD_H amd.h
				HINTS /usr/include/ ${UMFPACK_PATH}
				DOC "headerfile amd.h")
		if( AMD_H )
			get_filename_component(AMD_PATH ${AMD_H} PATH)
		else()
			message( FATAL_ERROR "could not find amd.h, needed for umfpack")
		endif()
	
		#add the compileflags and directories
		list(APPEND AMDIS_INCLUDE_DIRS ${UMFPACK_PATH} ${UFCONFIG_PATH} ${AMD_PATH})
	else()
		message(FATAL_ERROR "Could not find the umfpack-headers. Please install umfpack")
	endif()
	find_library(BLAS_LIBRARY NAMES "blas" "goto"
			DOC "the blas library")
#	message("blaslibrary: ${BLAS_LIBRARY}")
	if( BLAS_LIBRARY )
		list(APPEND AMDIS_LIBRARIES ${BLAS_LIBRARY})
	else()
		message(ERROR " could not find the blas library. please set the variable BLAS_LIBRARY to the blas library with full path")
	endif()

	find_library(UMFPACK_LIB umfpack
			DOC "the UMFPACK library")
	if( UMFPACK_LIB )
		list(APPEND AMDIS_LIBRARIES ${UMFPACK_LIB})
	else()
		message(FATAL_ERROR "Did not find the umfpack library")
	endif()
	
	find_library(AMD_LIB amd
			DOC "the AMD library")
	if( AMD_LIB )
		list(APPEND AMDIS_LIBRARIES ${AMD_LIB})
	else()
		message(FATAL_ERROR "Did not find the amd library")
	endif()
	#message("amdis-libs: ${AMDIS_LIBRARIES}")
	list(APPEND AMDIS_LIBRARY_DIRS ${BLAS_LIBRARY_DIR})
endif(AMDiS_NEED_UMFPACK)

if(${AMDIS_FIND_COMPONENTS} MATCHES umfpack )
	if( NOT AMDiS_NEED_UMFPACK )
	  set( AMDiS_umfpack_FOUND FALSE)
	  message(SEND_ERROR "the selected amdis was not compiled with umfpack")
	endif()
endif()

if( AMDiS_NEED_PARMETIS )
  list(APPEND AMDIS_LIBRARY_DIRS ${AMDIS_LIBRARY_DIR}/parmetis)
  list(APPEND AMDIS_LIBRARIES ${AMDIS_LIBRARY_DIR}/parmetis/libparmetis.a ${AMDIS_LIBRARY_DIR}/parmetis/libmetis.a)
endif()

if( AMDiS_NEED_ZOLTAN )
  find_library(ZOLTAN_LIB zoltan DOC "full path to the zoltan library")
  list(APPEND AMDIS_LIBRARIES ${ZOLTAN_LIB})
endif()
if(${AMDIS_FIND_COMPONENTS} MATCHES parmetis AND NOT AMDiS_NEED_PARMETIS)
	message(SEND_ERROR "amdis was not compiled with parmetis")
endif()