# - Find Google C++ Testing Framework
# Google C++ Testing Framework helps you write better C++ tests.
#
#  GTEST_INCLUDE_DIR - where to find gtest.h.
#  GTEST_LIBRARIES   - List of libraries when using gtest.
#  GTEST_FOUND       - True if gtest found.

IF(GTEST_INCLUDE_DIR)

  # Already in cache, be silent
  SET(GTEST_FIND_QUIETLY TRUE)

ELSE(GTEST_INCLUDE_DIR)

  IF(UNIX)
    SET(GTEST_PATH_PREFIXES
      /usr
      /usr/local
      /opt/local
    )
  ENDIF(UNIX)

  IF(WIN32)
    SET(GTEST_PATH_PREFIXES
      $ENV{ProgramFiles}/devtools
      $ENV{SystemDrive}/devtools
    )
  ENDIF(WIN32)

  FIND_PATH(GTEST_INCLUDE_DIR NAMES gtest/gtest.h
    PATHS ${GTEST_PATH_PREFIXES}
    PATH_SUFFIXES include
  )
  FIND_PATH(GMOCK_INCLUDE_DIR NAMES gmock/gmock.h
    PATHS ${GTEST_PATH_PREFIXES}
    PATH_SUFFIXES include
  )
  FIND_PATH(BOOST_INCLUDE_DIR NAMES boost/version.hpp
    PATHS ${GTEST_PATH_PREFIXES}
    PATH_SUFFIXES include include/gmock
  )
  FIND_PATH(BOOST_TR1_INCLUDE_DIR NAMES tuple
    PATHS ${GTEST_PATH_PREFIXES}
    PATH_SUFFIXES include include/tr1 include/gmock/boost/tr1/tr1
  )

  IF(WIN32 AND MSVC)

    FIND_LIBRARY(GTEST_RELEASE NAMES gtest gtestMT gtestMD
      PATHS ${GTEST_PATH_PREFIXES}
      PATH_SUFFIXES lib/VC lib/VC80 lib/VC90
    )
    FIND_LIBRARY(GTEST_DEBUG NAMES gtestd gtestMTd gtestMDd
      PATHS ${GTEST_PATH_PREFIXES}
      PATH_SUFFIXES lib/VC lib/VC80 lib/VC90
    )
    FIND_LIBRARY(GMOCK_RELEASE NAMES gmock gmockMT gmockMD
      PATHS ${GTEST_PATH_PREFIXES}
      PATH_SUFFIXES lib/VC lib/VC80 lib/VC90
    )
    FIND_LIBRARY(GMOCK_DEBUG NAMES gmockd gmockMTd gmockMDd
      PATHS ${GTEST_PATH_PREFIXES}
      PATH_SUFFIXES lib/VC lib/VC80 lib/VC90
    )

    IF(MSVC_IDE)
      IF(GTEST_DEBUG AND GTEST_RELEASE)
        SET(GTEST_LIBRARIES optimized ${GTEST_RELEASE} debug ${GTEST_DEBUG})
      ELSE(GTEST_DEBUG AND GTEST_RELEASE)
        MESSAGE(FATAL_ERROR "Could not find the debug and release version of GTest")
      ENDIF(GTEST_DEBUG AND GTEST_RELEASE)
      IF(GMOCK_DEBUG AND GMOCK_RELEASE)
        SET(GMOCK_LIBRARIES optimized ${GMOCK_RELEASE} debug ${GMOCK_DEBUG})
      ELSE(GMOCK_DEBUG AND GMOCK_RELEASE)
        MESSAGE(FATAL_ERROR "Could not find the debug and release version of GMock")
      ENDIF(GMOCK_DEBUG AND GMOCK_RELEASE)
    ELSE(MSVC_IDE)
      STRING(TOLOWER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE_TOLOWER)
      IF(CMAKE_BUILD_TYPE_TOLOWER MATCHES debug)
        SET(GTEST_LIBRARIES ${GTEST_DEBUG})
        SET(GMOCK_LIBRARIES ${GMOCK_DEBUG})
      ELSE(CMAKE_BUILD_TYPE_TOLOWER MATCHES debug)
        SET(GTEST_LIBRARIES ${GTEST_RELEASE})
        SET(GMOCK_LIBRARIES ${GMOCK_RELEASE})
      ENDIF(CMAKE_BUILD_TYPE_TOLOWER MATCHES debug)
    ENDIF(MSVC_IDE)

    MARK_AS_ADVANCED(FORCE GTEST_RELEASE GTEST_DEBUG GMOCK_RELEASE GMOCK_DEBUG)

  ELSE(WIN32 AND MSVC)

    FIND_LIBRARY(GTEST_LIBRARIES NAMES gtest)
    FIND_LIBRARY(GMOCK_LIBRARIES NAMES gmock)

  ENDIF(WIN32 AND MSVC)

  IF(GTEST_INCLUDE_DIR AND GTEST_LIBRARIES)
    SET(GTEST_FOUND TRUE)
  ENDIF(GTEST_INCLUDE_DIR AND GTEST_LIBRARIES)

  IF(GMOCK_INCLUDE_DIR AND GMOCK_LIBRARIES)
    SET(GMOCK_FOUND TRUE)
  ENDIF(GMOCK_INCLUDE_DIR AND GMOCK_LIBRARIES)

  IF(GTEST_FOUND)
     IF(NOT GTest_FIND_QUIETLY)
        MESSAGE(STATUS "Found GTest: ${GTEST_LIBRARIES}")
     ENDIF(NOT GTest_FIND_QUIETLY)
  ELSE(GTEST_FOUND)
     IF(GTest_FIND_REQUIRED)
        MESSAGE(FATAL_ERROR "Could NOT find GTest")
     ENDIF(GTest_FIND_REQUIRED)
  ENDIF(GTEST_FOUND)

  IF(GMOCK_FOUND)
     IF(NOT GTest_FIND_QUIETLY)
        MESSAGE(STATUS "Found GMock: ${GMOCK_LIBRARIES}")
     ENDIF(NOT GTest_FIND_QUIETLY)
  ELSE(GMOCK_FOUND)
     IF(GTest_FIND_REQUIRED)
        MESSAGE(STATUS "Could NOT find GMock")
     ENDIF(GTest_FIND_REQUIRED)
  ENDIF(GMOCK_FOUND)

  SET(GTEST_LIBRARIES ${GTEST_LIBRARIES} CACHE PATH "Google Test library")
  SET(GMOCK_LIBRARIES ${GMOCK_LIBRARIES} CACHE PATH "Google Mock library")
  MARK_AS_ADVANCED(FORCE GTEST_INCLUDE_DIR GTEST_LIBRARIES)
  MARK_AS_ADVANCED(FORCE GMOCK_INCLUDE_DIR GMOCK_LIBRARIES)
  MARK_AS_ADVANCED(FORCE BOOST_INCLUDE_DIR BOOST_TR1_INCLUDE_DIR)

ENDIF(GTEST_INCLUDE_DIR)

