# Searches for an installation of the PROTOCOLBUFFERS library. On success, it sets the following variables:
#
#   PROTOCOLBUFFERS_FOUND              Set to true to indicate the library was found
#   PROTOCOLBUFFERS_INCLUDE_DIRS       The directory containing the protocol buffers header files
#   PROTOCOLBUFFERS_LIBRARIES          The libraries needed to use protocol buffers (without the full path)
#   PROTOCOLBUFFERS_COMPILER           The protocol buffers compiler
#
# To specify an additional directory to search, set PROTOCOLBUFFERS_ROOT.
#
# Author: Ewen Cheslack-Postava, 2008
#

# Look for the protocol buffers headers, first in the user-specified location and then in the system locations
SET(PROTOCOLBUFFERS_INCLUDE_DOC "The directory containing the protocol buffers header files")
FIND_PATH(PROTOCOLBUFFERS_INCLUDE_DIRS NAMES google/protobuf/message.h PATHS ${PROTOCOLBUFFERS_ROOT} ${PROTOCOLBUFFERS_ROOT}/include/ DOC ${PROTOCOLBUFFERS_INCLUDE_DOC} NO_DEFAULT_PATH)
IF(NOT PROTOCOLBUFFERS_INCLUDE_DIRS)  # now look in system locations
  FIND_PATH(PROTOCOLBUFFERS_INCLUDE_DIRS NAMES google/protobuf/message.h DOC ${PROTOCOLBUFFERS_INCLUDE_DOC})
ENDIF(NOT PROTOCOLBUFFERS_INCLUDE_DIRS)

SET(PROTOCOLBUFFERS_FOUND FALSE)

IF(PROTOCOLBUFFERS_INCLUDE_DIRS)
  # root directory
  SET(PROTOCOLBUFFERS_ROOT_DIRS ${PROTOCOLBUFFERS_INCLUDE_DIRS})
  IF("${PROTOCOLBUFFERS_ROOT_DIRS}" MATCHES "/include$")
    # Strip off the trailing "/include" in the path.
    GET_FILENAME_COMPONENT(PROTOCOLBUFFERS_ROOT_DIRS ${PROTOCOLBUFFERS_ROOT_DIRS} PATH)
  ENDIF("${PROTOCOLBUFFERS_ROOT_DIRS}" MATCHES "/include$")

  # lib dir
  SET(PROTOCOLBUFFERS_LIBRARY_DIRS ${PROTOCOLBUFFERS_ROOT_DIRS})
  IF(EXISTS "${PROTOCOLBUFFERS_LIBRARY_DIRS}/lib")
    SET(PROTOCOLBUFFERS_LIBRARY_DIRS ${PROTOCOLBUFFERS_LIBRARY_DIRS}/lib)
  ENDIF(EXISTS "${PROTOCOLBUFFERS_LIBRARY_DIRS}/lib")

  # bin dir
  SET(PROTOCOLBUFFERS_BIN_DIRS ${PROTOCOLBUFFERS_ROOT_DIRS})
  IF(EXISTS "${PROTOCOLBUFFERS_BIN_DIRS}/bin")
    SET(PROTOCOLBUFFERS_BIN_DIRS ${PROTOCOLBUFFERS_BIN_DIRS}/bin)
  ENDIF(EXISTS "${PROTOCOLBUFFERS_BIN_DIRS}/bin")

  IF(WIN32)  # locate debug and release builds separately
    FIND_LIBRARY(PROTOCOLBUFFERS_DEBUG_LIBRARY   NAMES protobufd protobuf_d libprotobufd libprotobuf_d
                 PATH_SUFFIXES "" Debug   PATHS ${PROTOCOLBUFFERS_LIBRARY_DIRS} NO_DEFAULT_PATH)
    FIND_LIBRARY(PROTOCOLBUFFERS_RELEASE_LIBRARY NAMES protobuf libprotobuf
                 PATH_SUFFIXES "" Release PATHS ${PROTOCOLBUFFERS_LIBRARY_DIRS} NO_DEFAULT_PATH)

    SET(PROTOCOLBUFFERS_LIBRARIES)
    IF(PROTOCOLBUFFERS_DEBUG_LIBRARY AND PROTOCOLBUFFERS_RELEASE_LIBRARY)
      SET(PROTOCOLBUFFERS_LIBRARIES debug ${PROTOCOLBUFFERS_DEBUG_LIBRARY} optimized ${PROTOCOLBUFFERS_RELEASE_LIBRARY})
    ELSEIF(PROTOCOLBUFFERS_DEBUG_LIBRARY)
      SET(PROTOCOLBUFFERS_LIBRARIES ${PROTOCOLBUFFERS_DEBUG_LIBRARY})
    ELSEIF(PROTOCOLBUFFERS_RELEASE_LIBRARY)
      SET(PROTOCOLBUFFERS_LIBRARIES ${PROTOCOLBUFFERS_RELEASE_LIBRARY})
    ENDIF(PROTOCOLBUFFERS_DEBUG_LIBRARY AND PROTOCOLBUFFERS_RELEASE_LIBRARY)
  ELSE(WIN32)
    FIND_LIBRARY(PROTOCOLBUFFERS_LIBRARIES NAMES protobuf PATHS ${PROTOCOLBUFFERS_LIBRARY_DIRS} NO_DEFAULT_PATH)
  ENDIF(WIN32)

  # protoc compiler
  FIND_FILE(PROTOCOLBUFFERS_COMPILER NAMES protoc protoc.exe PATHS ${PROTOCOLBUFFERS_BIN_DIRS} NO_DEFAULT_PATH)

  IF(PROTOCOLBUFFERS_LIBRARIES AND PROTOCOLBUFFERS_COMPILER)
    SET(PROTOCOLBUFFERS_FOUND TRUE)
  ENDIF(PROTOCOLBUFFERS_LIBRARIES AND PROTOCOLBUFFERS_COMPILER)
ENDIF(PROTOCOLBUFFERS_INCLUDE_DIRS)

IF(PROTOCOLBUFFERS_FOUND)
  IF(NOT PROTOCOLBUFFERS_FIND_QUIETLY)
    MESSAGE(STATUS "Found Protocol Buffers: headers at ${PROTOCOLBUFFERS_INCLUDE_DIRS}, libraries at ${PROTOCOLBUFFERS_LIBRARIES}, compiler at ${PROTOCOLBUFFERS_COMPILER}")
  ENDIF(NOT PROTOCOLBUFFERS_FIND_QUIETLY)
ELSE(PROTOCOLBUFFERS_FOUND)
  IF(PROTOCOLBUFFERS_FIND_REQUIRED)
    MESSAGE(FATAL_ERROR "PROTOCOLBUFFERS not found")
  ENDIF(PROTOCOLBUFFERS_FIND_REQUIRED)
ENDIF(PROTOCOLBUFFERS_FOUND)
