# - Try to find libev
# Once done this will define
#  LIBEV_FOUND        - System has libev
#  LIBEV_INCLUDE_DIRS - The libev include directories
#  LIBEV_LIBRARIES    - The libraries needed to use libev

IF(LIBEV_USE_STATIC)
  MESSAGE(STATUS "LIBEV_USE_STATIC: ON")
ELSE()
  MESSAGE(STATUS "LIBEV_USE_STATIC: OFF")
ENDIF(LIBEV_USE_STATIC)

FIND_PATH(LIBEV_INCLUDE_DIR NAMES ev.h)

IF(LIBEV_USE_STATIC)
  FIND_LIBRARY(LIBEV_LIBRARY NAMES libev.a)
ELSE()
  FIND_LIBRARY(LIBEV_LIBRARY NAMES ev)
ENDIF(LIBEV_USE_STATIC)

IF(LIBEV_INCLUDE_DIR)
  FILE(STRINGS "${LIBEV_INCLUDE_DIR}/ev.h"
    LIBEV_VERSION_MAJOR REGEX "^#define[ \t]+EV_VERSION_MAJOR[ \t]+[0-9]+")
  FILE(STRINGS "${LIBEV_INCLUDE_DIR}/ev.h"
    LIBEV_VERSION_MINOR REGEX "^#define[ \t]+EV_VERSION_MINOR[ \t]+[0-9]+")
  STRING(REGEX REPLACE "[^0-9]+" "" LIBEV_VERSION_MAJOR "${LIBEV_VERSION_MAJOR}")
  STRING(REGEX REPLACE "[^0-9]+" "" LIBEV_VERSION_MINOR "${LIBEV_VERSION_MINOR}")
  SET(LIBEV_VERSION "${LIBEV_VERSION_MAJOR}.${LIBEV_VERSION_MINOR}")
  UNSET(LIBEV_VERSION_MINOR)
  UNSET(LIBEV_VERSION_MAJOR)
ENDIF()

INCLUDE(FindPackageHandleStandardArgs)
# handle the QUIETLY and REQUIRED arguments and set LIBEV_FOUND to TRUE
# if all listed variables are TRUE and the requested version matches.
FIND_PACKAGE_HANDLE_STANDARD_ARGS(LibEv REQUIRED_VARS
                                  LIBEV_LIBRARY LIBEV_INCLUDE_DIR
                                  VERSION_VAR LIBEV_VERSION)

IF(LIBEV_FOUND)
  SET(LIBEV_LIBRARIES     ${LIBEV_LIBRARY})
  SET(LIBEV_INCLUDE_DIRS  ${LIBEV_INCLUDE_DIR})
ENDIF(LIBEV_FOUND)

MARK_AS_ADVANCED(LIBEV_INCLUDE_DIR LIBEV_LIBRARY)
