# Searches for an GCRYPT installation
#
# Defines:
#
#   GCRYPT_FOUND         True if GCRYPT was found, else false
#   GCRYPT_LIBRARIES     Libraries to link
#   GCRYPT_INCLUDE_DIRS  The directories containing the header files
#
# To specify an additional directory to search, set GCRYPT_ROOT.
#
# Author: Siddhartha Chaudhuri, 2008
#

SET(GCRYPT_FOUND FALSE)

IF(${CMAKE_SYSTEM_NAME} STREQUAL "Darwin")  # OS X

  # I don't currently know where this lives on mac, so just disable it.

ELSE(${CMAKE_SYSTEM_NAME} STREQUAL "Darwin")  # Windows, Linux etc

  SET(GCRYPT_INCLUDE_DOC "The directory containing gcrypt.h")

  SET(USER_GCRYPT_SEARCH_DIRS)
  IF(GCRYPT_ROOT)
    SET(USER_GCRYPT_SEARCH_DIRS ${GCRYPT_ROOT} ${GCRYPT_ROOT}/include)
  ENDIF()

  FIND_PATH(GCRYPT_INCLUDE_DIRS NAMES gcrypt.h PATHS ${USER_GCRYPT_SEARCH_DIRS} DOC ${GCRYPT_INCLUDE_DOC} NO_DEFAULT_PATH)
  IF(NOT GCRYPT_INCLUDE_DIRS)  # now look in system locations
    FIND_PATH(GCRYPT_INCLUDE_DIRS NAMES gcrypt.h DOC ${GCRYPT_INCLUDE_DOC})
  ENDIF(NOT GCRYPT_INCLUDE_DIRS)

  IF(GCRYPT_INCLUDE_DIRS)
    SET(GCRYPT_LIBRARY_DIRS ${GCRYPT_INCLUDE_DIRS})
    IF("${GCRYPT_LIBRARY_DIRS}" MATCHES "/include$")
      # Strip off the trailing "/include" in the path.
      GET_FILENAME_COMPONENT(GCRYPT_LIBRARY_DIRS ${GCRYPT_LIBRARY_DIRS} PATH)
    ENDIF("${GCRYPT_LIBRARY_DIRS}" MATCHES "/include$")

    IF(EXISTS "${GCRYPT_LIBRARY_DIRS}/lib")
      SET(GCRYPT_LIBRARY_DIRS ${GCRYPT_LIBRARY_DIRS}/lib)
    ENDIF(EXISTS "${GCRYPT_LIBRARY_DIRS}/lib")

    IF(WIN32)  # Windows
      FIND_LIBRARY(GCRYPT_DEBUG_LIBRARY   NAMES gcryptd gcrypt_d libgcryptd libgcrypt_d
                   PATH_SUFFIXES "" Debug   PATHS ${GCRYPT_LIBRARY_DIRS} NO_DEFAULT_PATH)
      FIND_LIBRARY(GCRYPT_RELEASE_LIBRARY NAMES gcrypt libcrypt
                   PATH_SUFFIXES "" Release PATHS ${GCRYPT_LIBRARY_DIRS} NO_DEFAULT_PATH)

      SET(GCRYPT_LIBRARIES)
      IF(GCRYPT_DEBUG_LIBRARY AND GCRYPT_RELEASE_LIBRARY)
        SET(GCRYPT_LIBRARIES debug ${GCRYPT_DEBUG_LIBRARY} optimized ${GCRYPT_RELEASE_LIBRARY})
      ELSEIF(GCRYPT_DEBUG_LIBRARY)
        SET(GCRYPT_LIBRARIES ${GCRYPT_DEBUG_LIBRARY})
      ELSEIF(GCRYPT_RELEASE_LIBRARY)
        SET(GCRYPT_LIBRARIES ${GCRYPT_RELEASE_LIBRARY})
      ENDIF(GCRYPT_DEBUG_LIBRARY AND GCRYPT_RELEASE_LIBRARY)
    ELSE(WIN32)  # Linux etc
      # Use NO_DEFAULT_PATH to prefer user search dirs
      FIND_LIBRARY(GCRYPT_LIBRARIES NAMES gcrypt PATHS ${GCRYPT_LIBRARY_DIRS} NO_DEFAULT_PATH)
      FIND_LIBRARY(GCRYPT_LIBRARIES NAMES gcrypt PATHS ${GCRYPT_LIBRARY_DIRS})
    ENDIF(WIN32)

    IF(GCRYPT_LIBRARIES)
      SET(GCRYPT_FOUND TRUE)
    ENDIF(GCRYPT_LIBRARIES)
  ENDIF(GCRYPT_INCLUDE_DIRS)

ENDIF(${CMAKE_SYSTEM_NAME} STREQUAL "Darwin")

IF(GCRYPT_FOUND)
  IF(NOT GCRYPT_FIND_QUIETLY)
    IF(${CMAKE_SYSTEM_NAME} STREQUAL "Darwin")
      MESSAGE(STATUS "Found gcrypt: ${GCRYPT_LDFLAGS}")
    ELSE(${CMAKE_SYSTEM_NAME} STREQUAL "Darwin")
      MESSAGE(STATUS "Found gcrypt: headers at ${GCRYPT_INCLUDE_DIRS}, libraries at ${GCRYPT_LIBRARIES}")
    ENDIF(${CMAKE_SYSTEM_NAME} STREQUAL "Darwin")
  ENDIF(NOT GCRYPT_FIND_QUIETLY)
ELSE(GCRYPT_FOUND)
  IF(GCRYPT_FIND_REQUIRED)
    MESSAGE(FATAL_ERROR "gcrypt not found")
  ENDIF(GCRYPT_FIND_REQUIRED)
ENDIF(GCRYPT_FOUND)
