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

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

list(
	APPEND
	BULLET_INCLUDE_HINTS
	$ENV{Bullet_DIR}/${CMAKE_INSTALL_INCLUDEDIR}
)

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

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

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

find_path(
	BULLET_INCLUDE_DIRS
	NAMES
	btBulletCollisionCommon.h
	HINTS
	${BULLET_INCLUDE_HINTS}
	PATHS
	${BULLET_INCLUDE_PATHS}
	PATH_SUFFIXES
	bullet
)

mark_as_advanced(BULLET_INCLUDE_DIRS)

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

list(
	APPEND
	BULLET_LIBRARY_HINTS
	$ENV{Bullet_DIR}/${CMAKE_INSTALL_LIBDIR}
)

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

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

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

find_library(
	BULLET_BULLETCOLLISION_LIBRARY_DEBUG
	NAMES
	bulletcollisiond BulletCollisiond BulletCollision_debug BulletCollision_Debug
	HINTS
	${BULLET_LIBRARY_HINTS}
	PATHS
	${BULLET_LIBRARY_PATHS}
)

find_library(
	BULLET_BULLETCOLLISION_LIBRARY_RELEASE
	NAMES
	bulletcollision BulletCollision BulletCollision_RelWithDebugInfo BulletCollision_MinsizeRel
	HINTS
	${BULLET_LIBRARY_HINTS}
	PATHS
	${BULLET_LIBRARY_PATHS}
)

select_library_configurations(BULLET_BULLETCOLLISION)

find_library(
	BULLET_BULLETDYNAMICS_LIBRARY_DEBUG
	NAMES
	bulletdynamicsd BulletDynamicsd BulletDynamics_debug BulletDynamics_Debug
	HINTS
	${BULLET_LIBRARY_HINTS}
	PATHS
	${BULLET_LIBRARY_PATHS}
)

find_library(
	BULLET_BULLETDYNAMICS_LIBRARY_RELEASE
	NAMES
	bulletdynamics BulletDynamics BulletDynamics_RelWithDebugInfo BulletDynamics_MinsizeRel
	HINTS
	${BULLET_LIBRARY_HINTS}
	PATHS
	${BULLET_LIBRARY_PATHS}
)

select_library_configurations(BULLET_BULLETDYNAMICS)

find_library(
	BULLET_BULLETSOFTBODY_LIBRARY_DEBUG
	NAMES
	bulletsoftbodyd BulletSoftBodyd BulletSoftBody_debug BulletSoftBody_Debug
	HINTS
	${BULLET_LIBRARY_HINTS}
	PATHS
	${BULLET_LIBRARY_PATHS}
)

find_library(
	BULLET_BULLETSOFTBODY_LIBRARY_RELEASE
	NAMES
	bulletsoftbody BulletSoftBody BulletSoftBody_RelWithDebugInfo BulletSoftBody_MinsizeRel
	HINTS
	${BULLET_LIBRARY_HINTS}
	PATHS
	${BULLET_LIBRARY_PATHS}
)

select_library_configurations(BULLET_BULLETSOFTBODY)

find_library(
	BULLET_CONVEXDECOMPOSITION_LIBRARY_DEBUG
	NAMES
	convexdecompositiond ConvexDecompositiond ConvexDecomposition_debug ConvexDecomposition_Debug
	HINTS
	${BULLET_LIBRARY_HINTS}
	PATHS
	${BULLET_LIBRARY_PATHS}
)

find_library(
	BULLET_CONVEXDECOMPOSITION_LIBRARY_RELEASE
	NAMES
	convexdecomposition ConvexDecomposition ConvexDecomposition_RelWithDebugInfo ConvexDecomposition_MinsizeRel
	HINTS
	${BULLET_LIBRARY_HINTS}
	PATHS
	${BULLET_LIBRARY_PATHS}
)

select_library_configurations(BULLET_CONVEXDECOMPOSITION)

find_library(
	BULLET_LINEARMATH_LIBRARY_DEBUG
	NAMES
	bulletmathd LinearMathd LinearMath_debug LinearMath_Debug
	HINTS
	${BULLET_LIBRARY_HINTS}
	PATHS
	${BULLET_LIBRARY_PATHS}
)

find_library(
	BULLET_LINEARMATH_LIBRARY_RELEASE
	NAMES
	bulletmath LinearMath LinearMath_RelWithDebugInfo LinearMath_MinsizeRel
	HINTS
	${BULLET_LIBRARY_HINTS}
	PATHS
	${BULLET_LIBRARY_PATHS}
)

select_library_configurations(BULLET_LINEARMATH)

set(
	BULLET_LIBRARIES
	${BULLET_BULLETCOLLISION_LIBRARIES}
	${BULLET_BULLETDYNAMICS_LIBRARIES}
	${BULLET_BULLETSOFTBODY_LIBRARIES}
	${BULLET_LINEARMATH_LIBRARIES}
)

find_package_handle_standard_args(
	Bullet
	FOUND_VAR BULLET_FOUND
	REQUIRED_VARS BULLET_INCLUDE_DIRS BULLET_LIBRARIES
)

if((BULLET_BULLETCOLLISION_LIBRARY_RELEASE OR BULLET_BULLETCOLLISION_LIBRARY_DEBUG) AND BULLET_INCLUDE_DIRS)
	set(BULLET_BULLETCOLLISION_LIBRARY_FOUND ON)
endif()

if((BULLET_BULLETDYNAMICS_LIBRARY_RELEASE OR BULLET_BULLETDYNAMICS_LIBRARY_DEBUG) AND BULLET_INCLUDE_DIRS)
	set(BULLET_BULLETDYNAMICS_LIBRARY_FOUND ON)
endif()

if((BULLET_BULLETSOFTBODY_LIBRARY_RELEASE OR BULLET_BULLETSOFTBODY_LIBRARY_DEBUG) AND BULLET_INCLUDE_DIRS)
	set(BULLET_BULLETSOFTBODY_LIBRARY_FOUND ON)
endif()

if((BULLET_CONVEXDECOMPOSITION_LIBRARY_RELEASE OR BULLET_CONVEXDECOMPOSITION_LIBRARY_DEBUG) AND BULLET_INCLUDE_DIRS)
	set(BULLET_CONVEXDECOMPOSITION_LIBRARY_FOUND ON)
endif()

if((BULLET_LINEARMATH_LIBRARY_RELEASE OR BULLET_LINEARMATH_LIBRARY_DEBUG) AND BULLET_INCLUDE_DIRS)
	set(BULLET_LINEARMATH_LIBRARY_FOUND ON)
endif()

if(BULLET_BULLETCOLLISION_LIBRARY_FOUND AND NOT TARGET Bullet::BulletCollision)
	add_library(Bullet::BulletCollision UNKNOWN IMPORTED)
	
	if(BULLET_BULLETCOLLISION_LIBRARY_RELEASE)
		set_property(TARGET Bullet::BulletCollision APPEND PROPERTY IMPORTED_CONFIGURATIONS RELEASE)
		set_target_properties(Bullet::BulletCollision PROPERTIES IMPORTED_LOCATION_RELEASE "${BULLET_BULLETCOLLISION_LIBRARY_RELEASE}")
	endif()
	
	if(BULLET_BULLETCOLLISION_LIBRARY_DEBUG)
		set_property(TARGET Bullet::BulletCollision APPEND PROPERTY IMPORTED_CONFIGURATIONS DEBUG)
		set_target_properties(Bullet::BulletCollision PROPERTIES IMPORTED_LOCATION_DEBUG "${BULLET_BULLETCOLLISION_LIBRARY_DEBUG}")
	endif()
	
	set_target_properties(
		Bullet::BulletCollision PROPERTIES
		INTERFACE_INCLUDE_DIRECTORIES "${BULLET_INCLUDE_DIRS}"
	)
endif()

if(BULLET_BULLETDYNAMICS_LIBRARY_FOUND AND NOT TARGET Bullet::BulletDynamics)
	add_library(Bullet::BulletDynamics UNKNOWN IMPORTED)
	
	if(BULLET_BULLETDYNAMICS_LIBRARY_RELEASE)
		set_property(TARGET Bullet::BulletDynamics APPEND PROPERTY IMPORTED_CONFIGURATIONS RELEASE)
		set_target_properties(Bullet::BulletDynamics PROPERTIES IMPORTED_LOCATION_RELEASE "${BULLET_BULLETDYNAMICS_LIBRARY_RELEASE}")
	endif()
	
	if(BULLET_BULLETDYNAMICS_LIBRARY_DEBUG)
		set_property(TARGET Bullet::BulletDynamics APPEND PROPERTY IMPORTED_CONFIGURATIONS DEBUG)
		set_target_properties(Bullet::BulletDynamics PROPERTIES IMPORTED_LOCATION_DEBUG "${BULLET_BULLETDYNAMICS_LIBRARY_DEBUG}")
	endif()
	
	set_target_properties(
		Bullet::BulletDynamics PROPERTIES
		INTERFACE_INCLUDE_DIRECTORIES "${BULLET_INCLUDE_DIRS}"
	)
endif()

if(BULLET_BULLETSOFTBODY_LIBRARY_FOUND AND NOT TARGET Bullet::BulletSoftBody)
	add_library(Bullet::BulletSoftBody UNKNOWN IMPORTED)
	
	if(BULLET_BULLETSOFTBODY_LIBRARY_RELEASE)
		set_property(TARGET Bullet::BulletSoftBody APPEND PROPERTY IMPORTED_CONFIGURATIONS RELEASE)
		set_target_properties(Bullet::BulletSoftBody PROPERTIES IMPORTED_LOCATION_RELEASE "${BULLET_BULLETSOFTBODY_LIBRARY_RELEASE}")
	endif()
	
	if(BULLET_BULLETSOFTBODY_LIBRARY_DEBUG)
		set_property(TARGET Bullet::BulletSoftBody APPEND PROPERTY IMPORTED_CONFIGURATIONS DEBUG)
		set_target_properties(Bullet::BulletSoftBody PROPERTIES IMPORTED_LOCATION_DEBUG "${BULLET_BULLETSOFTBODY_LIBRARY_DEBUG}")
	endif()
	
	set_target_properties(
		Bullet::BulletSoftBody PROPERTIES
		INTERFACE_INCLUDE_DIRECTORIES "${BULLET_INCLUDE_DIRS}"
	)
endif()

if(BULLET_CONVEXDECOMPOSITION_LIBRARY_FOUND AND NOT TARGET Bullet::ConvexDecomposition)
	add_library(Bullet::ConvexDecomposition UNKNOWN IMPORTED)
	
	if(BULLET_CONVEXDECOMPOSITION_LIBRARY_RELEASE)
		set_property(TARGET Bullet::ConvexDecomposition APPEND PROPERTY IMPORTED_CONFIGURATIONS RELEASE)
		set_target_properties(Bullet::ConvexDecomposition PROPERTIES IMPORTED_LOCATION_RELEASE "${BULLET_CONVEXDECOMPOSITION_LIBRARY_RELEASE}")
	endif()
	
	if(BULLET_CONVEXDECOMPOSITION_LIBRARY_DEBUG)
		set_property(TARGET Bullet::ConvexDecomposition APPEND PROPERTY IMPORTED_CONFIGURATIONS DEBUG)
		set_target_properties(Bullet::ConvexDecomposition PROPERTIES IMPORTED_LOCATION_DEBUG "${BULLET_CONVEXDECOMPOSITION_LIBRARY_DEBUG}")
	endif()
	
	set_target_properties(
		Bullet::ConvexDecomposition PROPERTIES
		INTERFACE_INCLUDE_DIRECTORIES "${BULLET_INCLUDE_DIRS}"
	)
endif()

if(BULLET_LINEARMATH_LIBRARY_FOUND AND NOT TARGET Bullet::LinearMath)
	add_library(Bullet::LinearMath UNKNOWN IMPORTED)
	
	if(BULLET_LINEARMATH_LIBRARY_RELEASE)
		set_property(TARGET Bullet::LinearMath APPEND PROPERTY IMPORTED_CONFIGURATIONS RELEASE)
		set_target_properties(Bullet::LinearMath PROPERTIES IMPORTED_LOCATION_RELEASE "${BULLET_LINEARMATH_LIBRARY_RELEASE}")
	endif()
	
	if(BULLET_LINEARMATH_LIBRARY_DEBUG)
		set_property(TARGET Bullet::LinearMath APPEND PROPERTY IMPORTED_CONFIGURATIONS DEBUG)
		set_target_properties(Bullet::LinearMath PROPERTIES IMPORTED_LOCATION_DEBUG "${BULLET_LINEARMATH_LIBRARY_DEBUG}")
	endif()
	
	set_target_properties(
		Bullet::LinearMath PROPERTIES
		INTERFACE_INCLUDE_DIRECTORIES "${BULLET_INCLUDE_DIRS}"
	)
endif()
