# - Find GLEW
# Find the native GLEW headers
#
#  GLEW_INCLUDE_DIR  - where to find glew.h, etc.
#  GLEW_LIBRARIES    - glew library
#  GLEW_DEFINES      - glew defines
#  GLEW_FOUND        - True if GLEW found.

IF(GLEW_FOUND)
    # Already in cache, be silent
    SET(GLEW_FIND_QUIETLY TRUE)
ENDIF()

INCLUDE(${CMAKE_CURRENT_LIST_DIR}/DefinesGLEW.txt)

IF(BUILD_GLEW OR ACGL_GLOBAL_EXTERN_DIR_FOUND_FIRST_TIME OR BUILD_TYPE_CHANGED)
    UNSET(GLEW_INCLUDE_DIR CACHE)
    UNSET(GLEW_LIBRARIES CACHE)
ENDIF()

IF(BUILD_GLEW)
    SET(GLEW_INCLUDE_DIR "${ACGL_LOCAL_EXTERN_DIR}/glew/include")
    SET(GLEW_LIBRARIES GLEW${COMPILE_POSTFIX})
ELSE()
    # Look for the header file.
    IF(ACGL_GLOBAL_EXTERN_DIR)
        FIND_PATH(GLEW_INCLUDE_DIR NAMES GL/glew.h PATHS "${ACGL_GLOBAL_EXTERN_DIR}/glew/include" NO_DEFAULT_PATH)
    ENDIF()
    IF(NOT GLEW_INCLUDE_DIR)
        FIND_PATH(GLEW_INCLUDE_DIR NAMES GL/glew.h)
    ENDIF()

    # Look for the library file.
    IF(WIN32)
        IF(ACGL_GLOBAL_EXTERN_DIR)
            FIND_LIBRARY(GLEW_LIBRARIES NAMES glew32${COMPILE_POSTFIX} PATHS "${ACGL_GLOBAL_EXTERN_DIR}/glew/lib" NO_DEFAULT_PATH)
        ENDIF()
        IF(NOT GLEW_LIBRARIES)
            FIND_LIBRARY(GLEW_LIBRARIES NAMES glew32)
        ENDIF()
    ELSE()
        IF(ACGL_GLOBAL_EXTERN_DIR)
            FIND_LIBRARY(GLEW_LIBRARIES NAMES glew${COMPILE_POSTFIX} GLEW${COMPILE_POSTFIX} PATHS "${ACGL_GLOBAL_EXTERN_DIR}/glew/lib" NO_DEFAULT_PATH)
        ENDIF()
        IF(NOT GLEW_LIBRARIES)
            FIND_LIBRARY(GLEW_LIBRARIES NAMES glew GLEW)
        ENDIF()
    ENDIF()
ENDIF()

# Copy the results to the output variables.
IF(GLEW_INCLUDE_DIR AND GLEW_LIBRARIES)
    SET(GLEW_FOUND TRUE CACHE INTERNAL "")
ELSE()
    SET(GLEW_FOUND FALSE CACHE INTERNAL "")
ENDIF()

# Report the results.
IF(NOT GLEW_FOUND)
    SET(GLEW_MESSAGE "GLEW source was not found. Make sure GLEW_INCLUDE_DIR AND GLEW_LIBRARIES are set correctly.")
    IF(GLEW_FIND_REQUIRED)
        MESSAGE(FATAL_ERROR "${GLEW_MESSAGE}")
    ELSEIF(NOT GLEW_FIND_QUIETLY)
        MESSAGE(STATUS "${GLEW_MESSAGE}")
    ENDIF()
ELSEIF(NOT GLEW_FIND_QUIETLY)
    MESSAGE(STATUS "Looking for GLEW - found")
ENDIF()

MESSAGE(STATUS "GLEW_INCLUDE_DIR:${GLEW_INCLUDE_DIR}")
MESSAGE(STATUS "GLEW_LIBRARIES:${GLEW_LIBRARIES}")

