include(FindPackageHandleStandardArgs)
include(GNUInstallDirs)
include(SelectLibraryConfigurations)

foreach(PATH ${CMAKE_PREFIX_PATH})
	file(
		GLOB
		HINTS
		${PATH}/${CMAKE_INSTALL_INCLUDEDIR}
		${PATH}/ATIDAQ*/${CMAKE_INSTALL_INCLUDEDIR}
	)
	list(APPEND ATIDAQ_INCLUDE_HINTS ${HINTS})
endforeach()

list(
	APPEND
	ATIDAQ_INCLUDE_HINTS
	$ENV{ATIDAQ_DIR}/${CMAKE_INSTALL_INCLUDEDIR}
)

foreach(PATH $ENV{CMAKE_PREFIX_PATH})
	file(
		GLOB
		HINTS
		${PATH}/${CMAKE_INSTALL_INCLUDEDIR}
		${PATH}/ATIDAQ*/${CMAKE_INSTALL_INCLUDEDIR}
	)
	list(APPEND ATIDAQ_INCLUDE_HINTS ${HINTS})
endforeach()

foreach(PATH $ENV{PATH})
	file(GLOB HINTS ${PATH}/../${CMAKE_INSTALL_INCLUDEDIR})
	list(APPEND ATIDAQ_INCLUDE_HINTS ${HINTS})
endforeach()

file(
	GLOB
	ATIDAQ_INCLUDE_PATHS
	$ENV{HOME}/include
	/usr/local/include
	/opt/local/include
	/usr/include
)

find_path(
	ATIDAQ_INCLUDE_DIRS
	NAMES
	atidaq/ftconfig.h
	HINTS
	${ATIDAQ_INCLUDE_HINTS}
	HINTS
	${ATIDAQ_INCLUDE_PATHS}
)

mark_as_advanced(ATIDAQ_INCLUDE_DIRS)

foreach(PATH ${CMAKE_PREFIX_PATH})
	file(
		GLOB
		HINTS
		${PATH}/${CMAKE_INSTALL_LIBDIR}
		${PATH}/ATIDAQ*/${CMAKE_INSTALL_LIBDIR}
	)
	list(APPEND ATIDAQ_LIBRARY_HINTS ${HINTS})
endforeach()

list(
	APPEND
	ATIDAQ_LIBRARY_HINTS
	$ENV{ATIDAQ_DIR}/${CMAKE_INSTALL_LIBDIR}
)

foreach(PATH $ENV{CMAKE_PREFIX_PATH})
	file(
		GLOB
		HINTS
		${PATH}/${CMAKE_INSTALL_LIBDIR}
		${PATH}/ATIDAQ*/${CMAKE_INSTALL_LIBDIR}
	)
	list(APPEND ATIDAQ_LIBRARY_HINTS ${HINTS})
endforeach()

foreach(PATH $ENV{PATH})
	file(
		GLOB
		HINTS
		${PATH}/../${CMAKE_INSTALL_LIBDIR}
	)
	list(APPEND ATIDAQ_LIBRARY_HINTS ${HINTS})
endforeach()

file(
	GLOB
	ATIDAQ_LIBRARY_PATHS
	$ENV{HOME}/lib
	/usr/local/lib
	/opt/local/lib
	/usr/lib
)

find_library(
	ATIDAQ_LIBRARY_DEBUG
	NAMES
	atidaqd
	HINTS
	${ATIDAQ_LIBRARY_HINTS}
	PATHS
	${ATIDAQ_LIBRARY_PATHS}
)

find_library(
	ATIDAQ_LIBRARY_RELEASE
	NAMES
	atidaq
	HINTS
	${ATIDAQ_LIBRARY_HINTS}
	PATHS
	${ATIDAQ_LIBRARY_PATHS}
)

select_library_configurations(ATIDAQ)

find_package_handle_standard_args(
	ATIDAQ
	FOUND_VAR ATIDAQ_FOUND
	REQUIRED_VARS ATIDAQ_INCLUDE_DIRS ATIDAQ_LIBRARIES
)

if(ATIDAQ_FOUND AND NOT TARGET ATIDAQ::ATIDAQ)
	add_library(ATIDAQ::ATIDAQ UNKNOWN IMPORTED)
	
	if(ATIDAQ_LIBRARY_RELEASE)
		set_property(TARGET ATIDAQ::ATIDAQ APPEND PROPERTY IMPORTED_CONFIGURATIONS RELEASE)
		set_target_properties(ATIDAQ::ATIDAQ PROPERTIES IMPORTED_LOCATION_RELEASE "${ATIDAQ_LIBRARY_RELEASE}")
	endif()
	
	if(ATIDAQ_LIBRARY_DEBUG)
		set_property(TARGET ATIDAQ::ATIDAQ APPEND PROPERTY IMPORTED_CONFIGURATIONS DEBUG)
		set_target_properties(ATIDAQ::ATIDAQ PROPERTIES IMPORTED_LOCATION_DEBUG "${ATIDAQ_LIBRARY_DEBUG}")
	endif()
	
	set_target_properties(
		ATIDAQ::ATIDAQ PROPERTIES
		INTERFACE_INCLUDE_DIRECTORIES "${ATIDAQ_INCLUDE_DIRS}"
	)
endif()
