dnl CF_GNUTLS version: 22 updated: 2012/11/08 20:57:52
dnl ---------
dnl Check for gnutls library (TLS "is" SSL)
dnl $1 = the [optional] directory in which the library may be found
dnl $2 = the [optional] stub file to provide OpenSSL compatibility
AC_DEFUN([CF_GNUTLS],[
	AC_REQUIRE([CF_PKG_CONFIG])
	cf_cv_have_gnutls=no
	cf_cv_pkg_config_ssl=no

	CF_ADD_OPTIONAL_PATH($1, [ssl library])

	cf_pkg_gnutls=none
	if test "x$PKG_CONFIG" != xnone; then
		cf_pkg_gnutls=ifelse([$2],,gnutls-openssl,gnutls)
		case $1 in #(vi
		no) #(vi
			;;
		yes) # if no explicit directory given, try pkg-config
			CF_VERBOSE(checking pkg-config for $cf_pkg_gnutls)
			if "$PKG_CONFIG" --exists $cf_pkg_gnutls ; then
				CF_VERBOSE(... found $cf_pkg_gnutls in pkg-config)
				cf_cv_have_gnutls=yes
				cf_cv_pkg_config_ssl=yes

				cf_cflags_ssl=`$PKG_CONFIG --cflags $cf_pkg_gnutls`
				cf_libs_ssl=`$PKG_CONFIG --libs $cf_pkg_gnutls`

				if test -n "$cf_cflags_ssl" ; then
					case "$cf_cflags_ssl" in #(vi
					*-I*) #(vi
						cf_cv_header_path_gnutls=`echo "$cf_cflags_ssl" | sed -e 's/^.*-I//' -e 's/ .*//'`
						;;
					*)
						cf_cv_header_path_gnutls=/usr/include
						;;
					esac
					if test -d $cf_cv_header_path_gnutls/gnutls ; then
						cf_cv_header_path_gnutls=$cf_cv_header_path_gnutls/gnutls
					fi
					CF_ADD_CFLAGS($cf_cflags_ssl)
				fi

				if test -n "$cf_libs_ssl" ; then
					case "x$cf_libs_ssl" in #(vi
					*-L*) #(vi
						cf_cv_library_path_gnutls=`echo "$cf_libs_ssl" | sed -e 's/^.*-L//' -e 's/ .*//'`
						;;
					*)
						cf_cv_library_path_gnutls=/usr/lib
						;;
					esac
					CF_VERBOSE(adding $cf_libs_ssl to LIBS)
					CF_ADD_LIBS($cf_libs_ssl)
				fi
			else
				CF_VERBOSE(... did not find $cf_pkg_gnutls in pkg-config)
				cf_pkg_gnutls=none
			fi
			;;
		esac
	fi

	ifelse([$2],,
		[AC_DEFINE(USE_GNUTLS_INCL,1,[Define to 1 if we should include gnutls headers])],
		[AC_DEFINE(USE_GNUTLS_FUNCS,1,[Define to 1 if we should use gnutls functions])])

	if test "$cf_cv_have_gnutls" = no ; then
		cf_gnutls_CPPFLAGS=$CPPFLAGS

		CF_FIND_LINKAGE(CF__SSL_HEAD,
			CF__SSL_BODY,
			gnutls,
			cf_cv_have_gnutls=yes,
			cf_cv_have_gnutls=no,
			,
			ifelse([$2],,[-lgnutls-openssl -lgnutls-extra -lgnutls -lgcrypt],[-lgnutls -lgcrypt]))

		CPPFLAGS=$cf_gnutls_CPPFLAGS
	fi

	if test "$cf_cv_have_gnutls" = yes ; then
		if test -n "$cf_cv_header_path_gnutls" ; then
			AC_DEFINE(USE_SSL)
			case $cf_cv_header_path_gnutls in
				/usr/include/gnutls)
				;;
			*)
				CF_ADD_INCDIR($cf_cv_header_path_gnutls)
				;;
			esac
		fi
		if test -n "$cf_cv_library_path_gnutls" ; then
			CF_ADD_LIBDIR($cf_cv_library_path_gnutls)
		fi
		CF_ADD_LIBS(-lgnutls -lgcrypt)

		ifelse([$2],,
			[if test "$cf_pkg_gnutls" = none ; then
				AC_CHECK_LIB(gnutls-openssl,SSL_connect,
					[CF_ADD_LIBS(-lgnutls-openssl)],
					[AC_CHECK_LIB(gnutls-extra,SSL_connect,
					[CF_ADD_LIBS(-lgnutls-extra)],
					[AC_MSG_ERROR(cannot find gnutls openssl functions)])])
			fi],[EXTRA_OBJS="$EXTRA_OBJS $2"])

		CF_CHECK_SSL_X509
	fi
])dnl
