# adapted from
# http://code.google.com/p/opencollada/source/browse/trunk/Externals/cmake-modules/FindPCRE.cmake
#
# - Try to find the PCRE regular expression library
# Once done this will define
#
#  PCRE_FOUND - system has the PCRE library
#  PCRE_INCLUDE_DIR - the PCRE include directory
#  PCRE_LIBRARIES - The libraries needed to use PCRE

# Copyright (c) 2006, Alexander Neundorf, <neundorf@kde.org>
#
# Redistribution and use is allowed according to the terms of the BSD license.
# For details see the accompanying COPYING-CMAKE-SCRIPTS file.

if (PCRE_INCLUDE_DIR AND PCRE_PCREPOSIX_LIBRARY AND PCRE_PCRE_LIBRARY)
  # Already in cache, be silent
  set(PCRE_FIND_QUIETLY TRUE)
endif (PCRE_INCLUDE_DIR AND PCRE_PCREPOSIX_LIBRARY AND PCRE_PCRE_LIBRARY)

# do not use pkgconfig	
# if (NOT WIN32)
#   # use pkg-config to get the directories and then use these values
#   # in the FIND_PATH() and FIND_LIBRARY() calls
#   find_package(PkgConfig)
#   pkg_check_modules(PC_PCRE QUIET libpcre)
#   set(PCRE_DEFINITIONS ${PC_PCRE_CFLAGS_OTHER})
# endif (NOT WIN32)

find_path(PCRE_INCLUDE_DIR pcre.h
          HINTS ${PC_PCRE_INCLUDEDIR} ${PC_PCRE_INCLUDE_DIRS}
          PATH_SUFFIXES pcre)

find_library(PCRE_PCRE_LIBRARY_RELEASE NAMES pcre HINTS ${PC_PCRE_LIBDIR} ${PC_PCRE_LIBRARY_DIRS})
if (PCRE_PCRE_LIBRARY_RELEASE)
	list(APPEND PCRE_PCRE_LIBRARY optimized ${PCRE_PCRE_LIBRARY_RELEASE})
endif()

find_library(PCRE_PCRE_LIBRARY_DEBUG NAMES pcred HINTS ${PC_PCRE_LIBDIR} ${PC_PCRE_LIBRARY_DIRS})
if (PCRE_PCRE_LIBRARY_DEBUG)
	list(APPEND PCRE_PCRE_LIBRARY debug ${PCRE_PCRE_LIBRARY_DEBUG})
elseif(PCRE_PCRE_LIBRARY_RELEASE)
	# just fall back to the release library to avoid unresolved symbols
	list(APPEND PCRE_PCRE_LIBRARY debug ${PCRE_PCRE_LIBRARY_RELEASE})
endif()

IF(NOT WIN32)
	find_library(PCRE_PCREPOSIX_LIBRARY_RELEASE NAMES pcreposix HINTS ${PC_PCRE_LIBDIR} ${PC_PCRE_LIBRARY_DIRS})
	if (PCRE_PCREPOSIX_LIBRARY_RELEASE)
		list(APPEND PCRE_PCREPOSIX_LIBRARY optimized ${PCRE_PCRE_LIBRARY_RELEASE})
	endif()

	find_library(PCRE_PCREPOSIX_LIBRARY_DEBUG NAMES pcreposixd HINTS ${PC_PCRE_LIBDIR} ${PC_PCRE_LIBRARY_DIRS})
	if (PCRE_PCREPOSIX_LIBRARY_DEBUG)
		list(APPEND PCRE_PCREPOSIX_LIBRARY debug ${PCRE_PCREPOSIX_LIBRARY_DEBUG})
	elseif(PCRE_PCREPOSIX_LIBRARY_RELEASE)
		list(APPEND PCRE_PCREPOSIX_LIBRARY debug ${PCRE_PCREPOSIX_LIBRARY_RELEASE})
	endif()
endif()

include(FindPackageHandleStandardArgs)

mark_as_advanced(PCRE_PCRE_LIBRARY_RELEASE PCRE_PCRE_LIBRARY_DEBUG PCRE_PCREPOSIX_LIBRARY_RELEASE PCRE_PCREPOSIX_LIBRARY_DEBUG)
IF(NOT WIN32)
        find_package_handle_standard_args(PCRE DEFAULT_MSG PCRE_INCLUDE_DIR PCRE_PCRE_LIBRARY PCRE_PCREPOSIX_LIBRARY )
        mark_as_advanced(PCRE_INCLUDE_DIR PCRE_LIBRARIES PCRE_PCREPOSIX_LIBRARY PCRE_PCRE_LIBRARY)
        set(PCRE_LIBRARIES ${PCRE_PCRE_LIBRARY} ${PCRE_PCREPOSIX_LIBRARY})
ELSE()
        find_package_handle_standard_args(PCRE DEFAULT_MSG PCRE_INCLUDE_DIR PCRE_PCRE_LIBRARY  )
        set(PCRE_LIBRARIES ${PCRE_PCRE_LIBRARY} )
        mark_as_advanced(PCRE_INCLUDE_DIR PCRE_LIBRARIES PCRE_PCRE_LIBRARY)
ENDIF()