#
# Locates the libevent library

# BSD won't find the libevent library, even when given the correct path...
IF(${SYSTEM_TYPE} STREQUAL "BSD")
   FIND_PATH(LIBEVENT_INCLUDE_DIRS NAMES event.h PATHS /usr/local/include NO_CMAKE_SYSTEM_PATH )
   FIND_LIBRARY(LIBEVENT_LIBRARIES NAMES event PATHS /usr/local/lib NO_CMAKE_SYSTEM_PATH )
ELSE()
   FIND_PATH(LIBEVENT_INCLUDE_DIRS NAMES event.h PATHS /usr/local/include /usr/include)
   FIND_LIBRARY(LIBEVENT_LIBRARIES NAMES event PATHS /usr/local/lib /usr/lib)
ENDIF()

IF (LIBEVENT_INCLUDE_DIRS AND LIBEVENT_LIBRARIES)
   SET(LIBEVENT_FOUND TRUE)

#
# In case we want to have FindPkgConfig-like behaviour:
# (it's not really needed right now, since LIBEVENT_LIBRARIES
#  includes the whole path to the library)
#
#   FOREACH(LIB_DIR ${LIBEVENT_INCLUDE_DIRS})
#     IF (LIBEVENT_LDFLAGS)
#        SET(LIBEVENT_LDFLAGS "${LIBEVENT_LDFLAGS};")
#     ENDIF (LIBEVENT_LDFLAGS)
#     SET(LIBEVENT_LDFLAGS "${LIBEVENT_LDFLAGS}-L${LIB_DIR}")
#   ENDFOREACH(LIB_DIR)
#
#   FOREACH(LIB_NAME ${LIBEVENT_LIBRARIES})
#     IF (LIBEVENT_LDFLAGS)
#        SET(LIBEVENT_LDFLAGS "${LIBEVENT_LDFLAGS};")
#     ENDIF (LIBEVENT_LDFLAGS)
#     GET_FILENAME_COMPONENT(L_NAME ${LIB_NAME} NAME_WE)
#     string(REGEX REPLACE "^lib" "" L_NAME ${L_NAME})
#     SET(LIBEVENT_LDFLAGS "${LIBEVENT_LDFLAGS}-l${L_NAME}")
#   ENDFOREACH(LIB_NAME)

ENDIF (LIBEVENT_INCLUDE_DIRS AND LIBEVENT_LIBRARIES)

IF (LIBEVENT_FOUND)
   IF (NOT LIBEVENT_FIND_QUIETLY)
#      MESSAGE(STATUS "Found libevent: incdir(${LIBEVENT_INCLUDE_DIRS}) lib(${LIBEVENT_LIBRARIES}) ldflags(${LIBEVENT_LDFLAGS})")
      MESSAGE(STATUS "Found libevent: incdir(${LIBEVENT_INCLUDE_DIRS}) lib(${LIBEVENT_LIBRARIES})")
   ENDIF (NOT LIBEVENT_FIND_QUIETLY)
ELSE (LIBEVENT_FOUND)
   IF (LIBEVENT_FIND_REQUIRED)
      MESSAGE(FATAL_ERROR "Could not find libevent")
   ENDIF (LIBEVENT_FIND_REQUIRED)
ENDIF (LIBEVENT_FOUND)
