# Find the native bullet headers
#
#  BULLET_INCLUDE_DIR  - where to find bulletenc.h, etc.
#  BULLET_LIBRARIES    - bullet library
#  BULLET_FOUND        - True if bullet found.

IF(BULLET_FOUND)
    # Already in cache, be silent
    SET(BULLET_FIND_QUIETLY TRUE)
ENDIF()

IF(BUILD_BULLET OR ACGL_GLOBAL_EXTERN_DIR_FOUND_FIRST_TIME OR BUILD_TYPE_CHANGED)
    UNSET(BULLET_INCLUDE_DIR CACHE)
    UNSET(BULLET_LIBRARIES CACHE)
ENDIF()

IF(BUILD_BULLET)
    SET(BULLET_INCLUDE_DIR "${ACGL_LOCAL_EXTERN_DIR}/bullet/src")
    SET(BULLET_LIBRARIES BulletCollision${COMPILE_POSTFIX} BulletDynamics${COMPILE_POSTFIX} BulletMultiThreaded${COMPILE_POSTFIX} BulletSoftBody${COMPILE_POSTFIX} BulletSoftBodySolvers_OpenCL_Mini${COMPILE_POSTFIX} LinearMath${COMPILE_POSTFIX} MiniCL${COMPILE_POSTFIX})
ELSE()
    # Look for the header file.
    IF(ACGL_GLOBAL_EXTERN_DIR)
        FIND_PATH(BULLET_INCLUDE_DIR NAMES btBulletCollisionCommon.h PATHS "${ACGL_GLOBAL_EXTERN_DIR}/bullet/src" NO_DEFAULT_PATH)
    ENDIF()
    IF(NOT BULLET_INCLUDE_DIR)
        FIND_PATH(BULLET_INCLUDE_DIR NAMES btBulletCollisionCommon.h)
    ENDIF()

    # Look for the library file.
    IF(ACGL_GLOBAL_EXTERN_DIR)
        FIND_LIBRARY(BULLET_LIBRARIES NAMES BulletCollision${COMPILE_POSTFIX} BulletDynamics${COMPILE_POSTFIX} BulletMultiThreaded${COMPILE_POSTFIX} BulletSoftBody${COMPILE_POSTFIX} BulletSoftBodySolvers_OpenCL_Mini${COMPILE_POSTFIX} LinearMath${COMPILE_POSTFIX} MiniCL${COMPILE_POSTFIX} PATHS "${ACGL_GLOBAL_EXTERN_DIR}/bullet/lib" NO_DEFAULT_PATH)
    ENDIF()
    IF(NOT BULLET_LIBRARIES)
        FIND_LIBRARY(BULLET_LIBRARIES NAMES BulletCollision${COMPILE_POSTFIX} BulletDynamics${COMPILE_POSTFIX} BulletMultiThreaded${COMPILE_POSTFIX} BulletSoftBody${COMPILE_POSTFIX} BulletSoftBodySolvers_OpenCL_Mini${COMPILE_POSTFIX} LinearMath${COMPILE_POSTFIX} MiniCL${COMPILE_POSTFIX})
    ENDIF()
ENDIF()

# Copy the results to the output variables.
IF(BULLET_INCLUDE_DIR AND BULLET_LIBRARIES)
    SET(BULLET_FOUND TRUE CACHE INTERNAL "")
ELSE()
    SET(BULLET_FOUND FALSE CACHE INTERNAL "")
ENDIF()

# Report the results.
IF(NOT BULLET_FOUND)
    SET(BULLET_MESSAGE "bullet was not found. Make sure BULLET_INCLUDE_DIR AND BULLET_LIBRARIES are set correctly.")
    IF(BULLET_FIND_REQUIRED)
        MESSAGE(FATAL_ERROR "${BULLET_MESSAGE}")
    ELSEIF(NOT BULLET_FIND_QUIETLY)
        MESSAGE(STATUS "${BULLET_MESSAGE}")
    ENDIF()
ELSEIF(NOT BULLET_FIND_QUIETLY)
    MESSAGE(STATUS "Looking for bullet - found")
ENDIF()

MESSAGE(STATUS "BULLET_INCLUDE_DIR:${BULLET_INCLUDE_DIR}")
MESSAGE(STATUS "BULLET_LIBRARIES:${BULLET_LIBRARIES}")

