dnl CF_SSL version: 24 updated: 2014/01/11 12:06:14
dnl ------
dnl Check for ssl library
dnl $1 = [optional] directory in which the library may be found, set by AC_ARG_WITH
AC_DEFUN([CF_SSL],[
	AC_REQUIRE([CF_PKG_CONFIG])
	cf_cv_have_ssl=no
	cf_cv_pkg_config_ssl=no

	# command-line option, etc., override default behavior
	CF_ADD_OPTIONAL_PATH($1, [ssl library])

	if test "x$PKG_CONFIG" != xnone; then
		case $1 in #(vi
		no) #(vi
			;;
		yes) # if no explicit directory given, try pkg-config
			cf_cv_pkg_ssl=
			for cf_try_package in openssl libssl
			do
				AC_MSG_CHECKING(pkg-config for $cf_try_package)
				if "$PKG_CONFIG" --exists $cf_try_package ; then
					cf_cv_pkg_ssl=$cf_try_package
					AC_MSG_RESULT(yes)
					break
				else
					AC_MSG_RESULT(no)
				fi
			done
			if test -n "$cf_cv_pkg_ssl" ; then
				cf_cv_have_ssl=yes
				cf_cv_pkg_config_ssl=yes

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

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

					# workaround for broken openssl package using kerberos
					case "$cf_cflags_ssl" in #(vi
					*kerberos*)
						cf_cv_have_ssl=maybe
						;;
					esac
				else
					cf_cv_header_path_ssl=/usr/include
				fi

				if test -d $cf_cv_header_path_ssl/openssl ; then
					cf_cv_header_path_ssl=$cf_cv_header_path_ssl/openssl
				fi

				if test -n "$cf_libs_ssl" ; then
					case "x$cf_libs_ssl" in #(vi
					*-L*) #(vi
						cf_cv_library_path_ssl=`echo "$cf_libs_ssl" | sed -e 's/^.*-L//' -e 's/ .*//'`
						;;
					*)
						cf_cv_library_path_ssl=/usr/lib
						;;
					esac
					CF_VERBOSE(adding $cf_libs_ssl to LIBS)
					CF_ADD_LIBS($cf_libs_ssl)
				fi
			fi
			;;
		esac
	fi

	if test "$cf_cv_have_ssl" != yes; then
		case $host_os in #(vi
		mingw*) #(vi
			cf_extra_ssl_libs="-lcrypto -lgdi32"
			;;
		*)
			# openssl 0.9.6 and up use dynamic loading for engines.
			cf_extra_ssl_libs="-lcrypto"
			case "x$LIBS" in #(vi
			*-ldl) #(vi
				;;
			*)
				AC_CHECK_LIB(dl,dlsym,
					[cf_extra_ssl_libs="$cf_extra_ssl_libs -ldl"])
				;;
			esac
			;;
		esac

		CF_FIND_LINKAGE(CF__SSL_HEAD,
			CF__SSL_BODY,
			ssl,
			cf_cv_have_ssl=yes,
			cf_cv_have_ssl=no,
			openssl,
			$cf_extra_ssl_libs)

		if test "$cf_cv_pkg_config_ssl" != yes ; then
			if test "$cf_cv_have_ssl" = yes ; then
				if test -n "$cf_cv_library_path_ssl" ; then
					CF_ADD_LIBDIR($cf_cv_library_path_ssl)
				fi
				CF_ADD_LIBS(-lssl $cf_extra_ssl_libs)
				if test -n "$cf_cv_header_path_ssl" ; then
					case $cf_cv_header_path_ssl in #(vi
					/usr/include/openssl) #(vi
						;;
					*)
						CF_ADD_INCDIR($cf_cv_header_path_ssl)
						;;
					esac
				fi
			fi
		fi
	fi

	if test "$cf_cv_have_ssl" = yes ; then
		AC_DEFINE(USE_SSL,1,[Define to 1 if we are using SSL])
		if test -n "$cf_cv_header_path_ssl" ; then
			CF_VERBOSE(checking ssl header-path $cf_cv_header_path_ssl)
			case $cf_cv_header_path_ssl in #(vi
			*/openssl)
				AC_DEFINE(USE_OPENSSL_INCL,1,[Define to 1 if we are using OpenSSL headers])
				;;
			esac
		fi
		CF_CHECK_SSL_X509
	fi
])dnl
