dnl CF_NSS_COMPAT version: 5 updated: 2015/04/15 19:08:48
dnl -------------
dnl Check for NSS compatible SSL libraries
dnl $1 = the [optional] directory in which the library may be found
AC_DEFUN([CF_NSS_COMPAT],[
check=`pkg-config --version 2>/dev/null`
if test -n "$check" ; then
	cf_ssl_library=`pkg-config --libs nss`
	cf_ssl_cflags=`pkg-config --cflags nss`
else
	# Without pkg-config, we'll kludge in some defaults
	cf_ssl_library="-lssl3 -lsmime3 -lnss3 -lplds4 -lplc4 -lnspr4 -lpthread -ldl"
	cf_ssl_cflags="-I/usr/include/nss3 -I/usr/include/nspr4"
fi
cf_ssl_library="-lnss_compat_ossl $cf_ssl_library"

case "$1" in
(no)
	cf_ssl_root=
	;;
(yes)
	AC_CHECK_LIB(nss_compat_ossl, SSL_get_version,[],[
		cf_ssl_root=/usr/local/nss_compat_ossl
		if test -d $cf_ssl_root ; then
			CF_VERBOSE(assume it is in $cf_ssl_root)
			cf_ssl_library="-L$cf_ssl_root/lib $cf_ssl_library"
		else
			AC_MSG_ERROR(cannot find NSS compilant libraries)
		fi
	],
	[-lnss_compat_ossl])
	;;
(*)
	if test -d $1 ; then
		if test -d $1/include ; then
			cf_ssl_root=$1
		elif test -d $1/../include ; then
			cf_ssl_root=$1/..
		else
			AC_MSG_ERROR(cannot find NSS compilant library under $1)
		fi
		cf_ssl_library="-L$cf_ssl_root/lib $cf_ssl_library"
	else
		AC_MSG_WARN(expected a directory: $1)
	fi
	;;
esac
CF_ADD_LIBS($cf_ssl_library)

cf_ssl_subincs=yes
if test -n "$cf_ssl_root" ; then
	if test -d $cf_ssl_root/include ; then
		cf_ssl_cflags="-I$cf_ssl_root/include  $cf_ssl_cflags"
		test -d $cf_ssl_root/include/nss_compat_ossl || cf_ssl_subincs=no
	fi
fi
CF_ADD_CFLAGS($cf_ssl_cflags)

if test "$cf_ssl_subincs" = yes ; then
AC_MSG_CHECKING(for NSS compilant include directory)
AC_TRY_COMPILE([
#include <stdio.h>
#include <nss_compat_ossl/nss_compat_ossl.h>],
	[SSL_shutdown((SSL *)0)],
	[cf_ssl_incl=yes],
	[cf_ssl_incl=no])
AC_MSG_RESULT($cf_ssl_incl)
test "$cf_ssl_incl" = yes && AC_DEFINE(USE_NSS_COMPAT_INCL,1,[Define to 1 if we should use nss compatibility header])
fi

AC_MSG_CHECKING(if we can link to NSS compilant library)
AC_TRY_LINK([
#include <stdio.h>
#ifdef USE_NSS_COMPAT_INCL
#include <nss_compat_ossl/nss_compat_ossl.h>
#else
#include <ssl.h>
#endif
],
	[SSL_shutdown((SSL *)0)],
	[cf_ssl_library=yes],
	[cf_ssl_library=no])
AC_MSG_RESULT($cf_ssl_library)
if test "$cf_ssl_library" = yes ; then
	AC_DEFINE(USE_SSL,1,[Define to 1 if we should use SSL])
	AC_DEFINE(USE_X509_SUPPORT,1,[Define to 1 if the SSL library provides X509 support])
else
	AC_MSG_ERROR(Cannot link with NSS compilant libraries)
fi
])dnl
