dnl
dnl Copyright 1998,1999 The OpenLDAP Foundation.  All Rights Reserved.
dnl 
dnl Redistribution and use in source and binary forms are permitted only
dnl as authorized by the OpenLDAP Public License.  A copy of this
dnl license is available at http://www.OpenLDAP.org/license.html or
dnl in file LICENSE in the top-level directory of the distribution.
dnl

dnl Configure.in for OpenLDAP
AC_INIT(include/ldap.h)dnl
AC_CONFIG_AUX_DIR(build)dnl
AM_INIT_AUTOMAKE(OpenLDAP,[2.0-devel], [no ac_define])dnl

dnl We use autoconf features new to 2.13.
dnl 	aclocal.m4 should be built using aclocal from automake 1.4
AC_PREREQ(2.13)dnl Required Autoconf version

AC_CONFIG_HEADER(include/portable.h include/ldap_features.h)dnl

dnl
dnl Start Args
AC_MSG_CHECKING(configure arguments)
AC_PREFIX_DEFAULT(/usr/local)

top_builddir=`pwd`
AC_SUBST(top_builddir)dnl

ldap_subdir="openldap"

AC_ARG_WITH(subdir, [  --with-subdir=DIR change default subdirectory used for installs], [
	if test "$withval" = "no"; then
		ldap_subdir=""
	elif test "$withval" != "yes"; then
		ldap_subdir="$withval"
	fi
])dnl

AC_SUBST(ldap_subdir)dnl

OL_ARG_ENABLE(debug,[  --enable-debug 	enable debugging], yes)dnl
ol_enable_syslog=no
dnl OL_ARG_ENABLE(syslog,[  --enable-syslog	enable syslog support], auto)dnl
OL_ARG_ENABLE(proctitle,[  --enable-proctitle	enable proctitle support], yes)dnl
OL_ARG_ENABLE(cache,[  --enable-cache	enable caching], yes)dnl
OL_ARG_ENABLE(dns,[  --enable-dns		enable V2 DNS  extension], no)dnl
OL_ARG_ENABLE(referrals,[  --enable-referrals	enable V2 Referrals extension], yes)dnl
OL_ARG_ENABLE(cldap,[  --enable-cldap	enable connectionless ldap], no)dnl
OL_ARG_ENABLE(x_compile,[  --enable-x-compile	enable cross compiling],
	no, [yes no])dnl

dnl General "with" options
OL_ARG_ENABLE(dmalloc,[  --enable-dmalloc	enable debug malloc support], no)dnl

OL_ARG_WITH(kerberos,[  --with-kerberos	with Kerberos support],
	auto, [auto k5 k4 afs yes no])
OL_ARG_WITH(readline,[  --with-readline  with readline support],
	auto, [auto yes no] )
OL_ARG_WITH(threads,[  --with-threads	use threads],
	auto, [auto posix mach lwp yes no manual] )
OL_ARG_WITH(tls,[  --with-tls  with TLS/SSL support],
	auto, [auto ssleay openssl yes no] )
OL_ARG_WITH(yielding_select,[  --with-yielding-select	with implicitly yielding select],
	auto, [auto yes no manual] )

dnl Server options

dnl LDAPD OPTIONS
AC_ARG_WITH(xxldapdoptions,[LDAPD Options:])
OL_ARG_ENABLE(ldapd,[  --enable-ldapd	enable building ldapd], no)dnl

dnl SLAPD OPTIONS
AC_ARG_WITH(xxslapdoptions,[SLAPD Options:])
OL_ARG_ENABLE(slapd,[  --enable-slapd	enable building slapd], yes)dnl
OL_ARG_ENABLE(aclgroups,[    --enable-aclgroups	enable ACL group support], auto)dnl
OL_ARG_ENABLE(cleartext,[    --enable-cleartext	enable cleartext passwords], yes)dnl
OL_ARG_ENABLE(crypt,[    --enable-crypt	enable crypt(3) passwords], auto)dnl
OL_ARG_ENABLE(wrappers,[    --enable-wrappers	enable tcp wrapper support], no)dnl
OL_ARG_ENABLE(phonetic,[    --enable-phonetic	enable phonetic/soundex], no)dnl
OL_ARG_ENABLE(rlookups,[    --enable-rlookups	enable reverse lookups], auto)dnl

dnl SLAPD Backend options
OL_ARG_ENABLE(bdb2,[    --enable-bdb2	enable bdb2 backend], no)dnl
OL_ARG_ENABLE(ldbm,[    --enable-ldbm	enable ldbm backend], yes)dnl
OL_ARG_WITH(ldbm_api,[      --with-ldbm-api	use LDBM API], auto,
	[auto db2 db gdbm ndbm manual])
OL_ARG_WITH(ldbm_type,[      --with-ldbm-type	use LDBM type], auto,
	[auto btree hash])

OL_ARG_ENABLE(passwd,[    --enable-passwd	enable passwd backend], no)dnl
OL_ARG_ENABLE(perl,[    --enable-perl	enable perl backend], no)dnl
OL_ARG_ENABLE(shell,[    --enable-shell	enable shell backend], no)dnl
OL_ARG_ENABLE(tcl,[    --enable-tcl	enable tcl backend], no)dnl

dnl SLURPD OPTIONS
AC_ARG_WITH(xxslurpdoptions,[SLURPD Options:])
OL_ARG_ENABLE(slurpd,[  --enable-slurpd	enable building slurpd], auto)dnl

AC_ARG_WITH(xxliboptions,[Library Generation & Linking Options])
AM_ENABLE_STATIC
dnl AM_DISABLE_SHARED
AM_ENABLE_SHARED

dnl General "enable" options
# validate options
if test $ol_enable_dns = yes ; then
	if test $ol_enable_referrals = no ; then
		AC_MSG_ERROR([DNS requires --enable-referrals])
	fi
	if test $ol_enable_referrals = auto ; then
		AC_MSG_WARN([DNS requires referrals, adding --enable-referrals])
		ol_enable_referrals=yes
	fi
fi

if test $ol_enable_slapd = no ; then
	dnl SLAPD was specificallly disabled
	if test $ol_enable_bdb2 = yes ; then
		AC_MSG_WARN([slapd disabled, ignoring --enable_bdb2 argument])
	fi
	if test $ol_enable_ldbm = yes ; then
		AC_MSG_WARN([slapd disabled, ignoring --enable_ldbm argument])
	fi
	if test $ol_enable_passwd = yes ; then
		AC_MSG_WARN([slapd disabled, ignoring --enable_passwd argument])
	fi
	if test $ol_enable_perl = yes ; then
		AC_MSG_WARN([slapd disabled, ignoring --enable_perl argument])
	fi
	if test $ol_enable_shell = yes ; then
		AC_MSG_WARN([slapd disabled, ignoring --enable_shell argument])
	fi
	if test $ol_enable_tcl = yes ; then
		AC_MSG_WARN([slapd disabled, ignoring --enable_tcl argument])
	fi
	if test $ol_enable_aclgroups = yes ; then
		AC_MSG_WARN([slapd disabled, ignoring --enable_aclgroups argument])
	fi
	if test $ol_enable_wrappers = yes ; then
		AC_MSG_WARN([slapd disabled, ignoring --enable_wrappers argument])
	fi
	if test $ol_enable_phonetic = yes ; then
		AC_MSG_WARN([slapd disabled, ignoring --enable_phonetic argument])
	fi
	if test $ol_enable_rlookups = yes ; then
		AC_MSG_WARN([slapd disabled, ignoring --enable_rlookups argument])
	fi
	if test $ol_with_ldbm_api != auto ; then
		AC_MSG_WARN([slapd disabled, ignoring --with_ldbm_api argument])
	fi
	if test $ol_with_ldbm_type != auto ; then
		AC_MSG_WARN([slapd disabled, ignoring --with_ldbm_type argument])
	fi
	if test $ol_enable_slurpd = yes ; then
		AC_MSG_ERROR([slurpd requires slapd])
	fi

	# force settings to no
	ol_enable_bdb2=no
	ol_enable_ldbm=no
	ol_enable_passwd=no
	ol_enable_perl=no
	ol_enable_shell=no
	ol_enable_tcl=no
	ol_enable_aclgroups=no
	ol_enable_wrappers=no
	ol_enable_phonetic=no
	ol_enable_rlookups=no
	ol_with_ldbm_api=no
	ol_with_ldbm_type=no
	ol_enable_slurpd=no

elif test $ol_enable_ldbm = no ; then
	dnl SLAPD without LDBM

	if test $ol_with_ldbm_api != auto ; then
		AC_MSG_WARN([LDBM disabled, ignoring --with_ldbm_api argument])
	fi

	if test $ol_with_ldbm_type != auto ; then
		AC_MSG_WARN([LDBM disabled, ignoring --with_ldbm_type argument])
	fi

	if test $ol_enable_bdb2 = yes ; then
		AC_MSG_ERROR([BDB2 requires --enable-ldbm])
	fi

	if test $ol_enable_passwd = no -a \
		$ol_enable_perl = no -a \
		$ol_enable_shell = no -a \
		$ol_enable_tcl = no ; then
		AC_MSG_ERROR([slapd requires a backend])
	fi

	ol_with_ldbm_api=no
	ol_with_ldbm_type=no

elif test $ol_enable_bdb2 = yes ; then
	dnl SLAPD with BDB2

	if test $ol_with_ldbm_api != auto -a \
		$ol_with_ldbm_api != db2 ; then
		AC_MSG_ERROR([BDB2 requires LDBM API DB2])
	fi

	ol_with_ldbm_api=db2

else
	dnl SLAPD with LDBM

	if test $ol_enable_bdb2 != no ; then
		if test $ol_with_ldbm_api != auto -a \
			$ol_with_ldbm_api != db2 ; then
			AC_MSG_WARN([BDB2 requires LDBM api db2 or auto])
			ol_enable_bdb2=no
		fi
	fi

	if test $ol_with_ldbm_api = gdbm -a \
		$ol_with_ldbm_type = btree ; then
		AC_MSG_ERROR([GDBM only supports LDBM type hash])
	fi
	if test $ol_with_ldbm_api = ndbm -a \
		$ol_with_ldbm_type = btree ; then
		AC_MSG_ERROR([NDBM only supports LDBM type hash])
	fi
fi

if test $ol_enable_slurpd = yes ; then
	dnl SLURPD was specifically enabled
	if test $ol_with_threads = no ; then
		AC_MSG_ERROR([slurpd requires threads])
	fi
fi

AC_MSG_RESULT(done)

AC_CANONICAL_HOST

## Initialize vars
LDAP_LIBS=
LDBM_LIBS=
LTHREAD_LIBS=
LUTIL_LIBS=

LDAPD_LIBS=
SLAPD_LIBS=
SLURPD_LIBS=

BUILD_LDAPD=no
BUILD_SLAPD=no
BUILD_SLURPD=no

BUILD_BDB2=no
BUILD_LDBM=no
BUILD_PASSWD=no
BUILD_PERL=no
BUILD_SHELL=no
BUILD_TCL=no
BUILD_THREAD=no

SLAPD_PERL_LDFLAGS=
SLAPD_PERL_CPPFLAGS=

KRB_LIBS=
READLINE_LIBS=
TERMCAP_LIBS=
TLS_LIBS=

dnl ----------------------------------------------------------------
dnl Checks for programs

AC_PROG_INSTALL

dnl The default compiler is cc (NOT gcc)
CC=${CC-"cc"}
dnl The default CFLAGS is empty NOT whatever AC_PROG_CC sets.
dnl CFLAGS=${CFLAGS-""}

AM_PROG_LIBTOOL
dnl AC_PROG_MAKE_SET

AC_PROG_AWK
OL_PROG_LN_H
AC_PROG_LN_S

if test "$LN_H" = "cp" -a "$LN_S" = "ln"; then
	LN_S="$LN_H"
fi

AC_PATH_PROG(SENDMAIL, sendmail, /usr/lib/sendmail,
	$PATH:/usr/libexec:/usr/lib:/usr/sbin:/usr/etc:/etc)
AC_PATH_PROG(EDITOR, vi, /usr/ucb/vi, $PATH:/usr/ucb)
AC_PATH_PROG(FINGER, finger, /usr/ucb/finger, $PATH:/usr/ucb)

if test $ol_enable_perl = yes ; then
	AC_PATH_PROG(PERLBIN, perl, /usr/bin/perl)

	if test "no$PERLBIN" = "no" ; then
		ol_enable_perl=no
	fi

	SLAPD_PERL_LDFLAGS="`$PERLBIN -MExtUtils::Embed -e ldopts|sed -e s/-lc//`"
	SLAPD_PERL_CPPFLAGS="`$PERLBIN -MExtUtils::Embed -e perl_inc`"
fi

AC_PROG_CPP

if test $cross_compiling = yes -a $ol_enable_x_compile = yes; then
	AC_MSG_WARN([cross compiling....  some functionality will be removed.])

elif test $cross_compiling = no -a $ol_enable_x_compile = yes; then
	AC_MSG_WARN([programs compiled here do run here...])
	AC_MSG_ERROR([  if not cross compiling, use --disable-x-compile.])

elif test $cross_compiling = yes -a $ol_enable_x_compile = no; then
	AC_MSG_WARN([programs compiled here do not run here...])
	AC_MSG_ERROR([  if cross compiling,  add --enable-x-compile.])
fi

dnl Checks for UNIX Variants
AC_AIX
AC_ISC_POSIX
AC_MINIX

dnl BeOS requires -lbe -lroot
AC_CHECK_LIB(be, be_app,[LIBS="$LIBS -lbe -lroot"])

dnl Checks for system services
AC_CYGWIN
AC_MINGW32
AC_EXEEXT
AC_OBJEXT

dnl OpenLDAP requires STDC features
AM_PROG_CC_STDC
if test "X${am_cv_prog_cc_stdc}" = "Xno" ; then
	AC_MSG_ERROR([OpenLDAP requires compiler to support STDC prototypes.])
fi

dnl AM_C_PROTOTYPES

dnl Checks for libraries

dnl Find socket()
dnl Likely combinations:
dnl		-lsocket [ -lnsl_s | -lnsl ]
dnl		-linet

AC_CHECK_FUNC(socket, [have_socket=yes], [	
dnl
dnl hopefully we won't include too many libraries
dnl
	AC_CHECK_LIB(socket, main)
	AC_CHECK_LIB(net, main)
	AC_CHECK_LIB(nsl_s, main)
	AC_CHECK_LIB(nsl, main)
	AC_CHECK_LIB(inet, socket)
	AC_CHECK_LIB(gen, main)
])

dnl Check for resolver routines
AC_CHECK_FUNCS(res_search)
if test $ac_cv_func_res_search = "no" ; then 
	AC_CHECK_LIB(bind, res_search)
	if test "$ac_cv_lib_bind_res_search" = "yes" ; then
		AC_DEFINE(HAVE_RES_SEARCH,1)
	else
		AC_CHECK_LIB(resolv, res_search)
		if test "$ac_cv_lib_resolv_res_search" = "yes" ; then
			AC_DEFINE(HAVE_RES_SEARCH,1)
		fi
	fi
fi

dnl HP-UX requires -lV3
AC_CHECK_LIB(V3, sigset)

# ISODE tests
ol_link_isode=no
if test $ol_enable_ldapd != no ; then
	dnl look for ISODE libraries
	AC_CHECK_LIB(xtpp, main, [
		ol_link_isode=yes
		AC_DEFINE(HAVE_XTPP,1, [define if you have -lxttp])
		LDAPD_LIBS="$LDAPD_LIBS -lxtpp -lxtdsap -lxtisode -losi"
		],:,[-lxtdsap -lxtisode -losi])
	AC_CHECK_LIB(dsap, main, [
		ol_link_isode=yes
		AC_DEFINE(HAVE_DSAP,1, [define if you have -ldsap])
		LDAPD_LIBS="$LDAPD_LIBS -ldsap"
		],:,[-lisode])
	AC_CHECK_LIB(isode, main, [
		ol_link_isode=yes
		AC_DEFINE(HAVE_ISODE,1, [define if you have -lisode])
		LDAPD_LIBS="$LDAPD_LIBS -lisode"
		],:)
fi

if test $ol_link_isode != no; then
	AC_CHECK_LIB(pp, main, [
		AC_DEFINE(HAVE_PP,1, [define if you have -lpp])
		LDAPD_LIBS="-lpp $LDAPD_LIBS"
		],:)

	AC_PATH_PROG(PEPSY, pepsy)
fi

dnl
dnl check for kerberos
dnl
ol_link_kerberos=no

if test $ol_with_kerberos = auto -o $ol_with_kerberos = k5 ; then
	AC_CHECK_HEADERS(kerberosIV/krb.h kerberosIV/des.h)

	if test $ac_cv_header_kerberosIV_krb_h = yes ; then
		AC_CHECK_LIB(krb4, main, [have_k5=yes], [have_k5=no],
			[-lkrb5 -ldes425])

		if test $have_k5 = yes ; then
			ol_with_kerberos=found
			ol_link_kerberos=yes

			KRB_LIBS="-lkrb4 -lkrb5 -ldes425"

			LIBS="$KRB_LIBS $LIBS"

			AC_CACHE_CHECK([for des_debug in Kerberos libraries],
				[ol_cv_var_des_debug], [
				dnl save the flags
				save_LIBS="$LIBS"
				LIBS="$KRB_LIBS $LIBS"
				AC_TRY_LINK([
#include <kerberosIV/krb.h>
#include <kerberosIV/des.h>
extern int des_debug;
],[
des_debug = 1;
],				ol_cv_var_des_debug=yes, ol_cv_var_des_debug=no)
				dnl restore the LIBS
				LIBS="$save_LIBS"
			])

			if test $ol_cv_var_des_debug = yes ; then
				AC_DEFINE(HAVE_DES_DEBUG,1,
					[define if you have Kerberos des_debug])
			fi

			LIBS="$save_LIBS"
		fi
	fi
fi

if test $ol_with_kerberos = auto -o $ol_with_kerberos = k4 ; then
	AC_CHECK_HEADERS(krb.h des.h)

	if test $ac_cv_header_krb_h = yes ; then
		AC_CHECK_LIB(krb, main, [have_k4=yes], [have_k4=no], [-ldes])

		if test $have_k4 = yes ; then
			ol_with_kerberos=found
			ol_link_kerberos=yes

			KRB_LIBS="-lkrb -ldes"
		fi
	fi
fi

if test $ol_link_kerberos = yes ; then
	AC_DEFINE(HAVE_KERBEROS, 1, [define if you have Kerberos])
fi

dnl
dnl Check for SSL/TLS
dnl
ol_link_tls=no
if test $ol_with_tls != no ; then
	
	AC_CHECK_HEADERS(ssl.h)
	
	if test $ac_cv_header_ssl_h = yes ; then
		AC_CHECK_LIB(ssl, SSLeay_add_ssl_algorithms, 
			[have_ssleay=yes
			need_rsaref=no],
			[have_ssleay=no],
			[-lcrypto])
			
		if test $have_ssleay = no ; then
			AC_CHECK_LIB(ssl, ssl3_accept, 
				[have_ssleay=yes
				need_rsaref=yes], [have_ssleay=no],
				[-lcrypto -lRSAglue -lrsaref])
		fi

		if test $have_ssleay = yes ; then
			ol_with_tls=found
			ol_link_tls=yes

			AC_DEFINE(HAVE_SSLEAY, 1, 
				[define if you have SSLeay or OpenSSL])

			if test $need_rsaref = yes; then
				AC_DEFINE(HAVE_RSAREF, 1, 
					[define if you have RSAref])

				TLS_LIBS="-lssl -lcrypto -lRSAglue -lrsaref"
			else
				TLS_LIBS="-lssl -lcrypto"
			fi
		fi
	fi
fi

if test $ol_link_tls = yes ; then
	AC_DEFINE(HAVE_TLS, 1, [define if you have TLS])
fi	

dnl Tests for reentrant functions necessary to build a
dnl thread_safe -lldap.
AC_CHECK_FUNCS(		\
	ctime_r			\
	gethostbyname_r	gethostbyaddr_r \
)

if test "$ac_cv_func_ctime_r" = no ; then
	ol_cv_func_ctime_r_nargs=0
else
	OL_FUNC_CTIME_R_NARGS
dnl	OL_FUNC_CTIME_R_TYPE
fi

if test "$ac_cv_func_gethostbyname_r" = yes ; then
 	OL_FUNC_GETHOSTBYNAME_R_NARGS
else
 	ol_cv_func_gethostbyname_r_nargs=0
fi
 
if test "$ac_cv_func_gethostbyaddr_r" = yes ; then
 	OL_FUNC_GETHOSTBYADDR_R_NARGS
else
 	ol_cv_func_gethostbyaddr_r_nargs=0
fi

if test "$ac_cv_func_ctime_r" = yes \
	-a "$ol_cv_func_ctime_r_nargs" -ge 2 \
	-a "$ol_cv_func_ctime_r_nargs" -le 3 \
	-a "$ac_cv_func_gethostbyname_r" = yes \
	-a "$ol_cv_func_gethostbyname_r_nargs" -ge 5 \
	-a "$ol_cv_func_gethostbyname_r_nargs" -le 6 \
 	-a "$ac_cv_func_gethostbyaddr_r" = yes \
 	-a "$ol_cv_func_gethostbyaddr_r_nargs" -ge 5 \
	-a "$ol_cv_func_gethostbyaddr_r_nargs" -le 6 \
 	; then
 
 	AC_DEFINE(LDAP_API_FEATURE_X_OPENLDAP_REENTRANT,1)
fi

ol_link_threads=no
if test $ol_with_threads = auto -o $ol_with_threads = yes \
	-o $ol_with_threads = posix ; then

	AC_CHECK_HEADERS(pthread.h sched.h)

	if test $ac_cv_header_pthread_h = yes ; then
		OL_POSIX_THREAD_VERSION

		if test $ol_cv_pthread_version = final ; then
			AC_DEFINE(HAVE_PTHREADS_FINAL,1,
				[define if pthreads API compatible with final spec])
		elif test $ol_cv_pthread_version = draft4 ; then
			AC_DEFINE(HAVE_PTHREADS_D4,1,
				[define if pthreads API compatible with draft4 spec])
		else
			AC_MSG_ERROR([unknown pthread version])
		fi

		# consider threads found
		ol_with_threads=found

		OL_LINUX_THREADS

		if test $ol_cv_linux_threads = yes ; then
			AC_DEFINE(HAVE_LINUX_THREADS,1,
				[define if you have LinuxThreads])
		fi

		dnl Now the hard part, how to link?
		dnl
		dnl currently supported checks:
		dnl
		dnl Check for no flags 
		dnl 	pthread_create() in $LIBS
		dnl
		dnl Check special pthread (final) flags
		dnl 	pthread_create() with -mt (Solaris)
		dnl 	pthread_create() with -kthread (FreeBSD)
		dnl 	pthread_create() with -pthread (FreeBSD/Digital Unix)
		dnl 	pthread_create() with -pthreads (?)
		dnl 	pthread_create() with -thread (?)
		dnl
		dnl Check pthread (final) libraries
		dnl 	pthread_mutex_unlock() in -lpthread -lmach -lexc -lc_r (OSF/1)
		dnl 	pthread_mutex_lock() in -lpthread -lmach -lexc (OSF/1)
		dnl 	pthread_mutex_trylock() in -lpthread -lexc (OSF/1)
		dnl 	pthread_create() in -lpthread (many)
		dnl 	pthread_create() in -lc_r (FreeBSD)
		dnl 	pthread_create() in -lpthreads (many)
		dnl 	pthread_join() -Wl,-woff,85 -lpthreads (IRIX)
		dnl
		dnl Check pthread (draft4) flags (depreciated)
		dnl 	pthread_create() with -threads (OSF/1)
		dnl
		dnl Check pthread (final) libraries (depreciated)
		dnl 	pthread_mutex_unlock() in -lpthreads -lmach -lexc -lc_r (OSF/1)
		dnl 	pthread_mutex_lock() in -lpthreads -lmach -lexc (OSF/1)
		dnl 	pthread_mutex_trylock() in -lpthreads -lexc (OSF/1)
		dnl

		dnl pthread_create in $LIBS
		AC_CHECK_FUNC(pthread_create,[ol_link_threads=yes])

		if test $ol_link_threads = no ; then
			dnl try -mt
			AC_CACHE_CHECK([for pthread_create with -mt],
				[ol_cv_thread_flag], [
				dnl save the flags
				save_LIBS="$LIBS"
				LIBS="-mt $LIBS"
				AC_TRY_LINK([char pthread_create();],
					[pthread_create();],
					[ol_cv_thread_flag=yes], [ol_cv_thread_flag=no])
				dnl restore the LIBS
				LIBS="$save_LIBS"
			])

			if test $ol_cv_thread_flag = yes ; then
				LTHREAD_LIBS="$LTHREAD_LIBS -mt"
				ol_link_threads=posix
			fi
		fi

		if test $ol_link_threads = no ; then
			dnl try -kthread
			AC_CACHE_CHECK([for pthread_create with -kthread],
				[ol_cv_kthread_flag], [
				dnl save the flags
				save_LIBS="$LIBS"
				LIBS="-kthread $LIBS"
				AC_TRY_LINK([#include <pthread.h>],[
#if HAVE_PTHREADS_D4
	pthread_create(NULL,pthread_attr_default,NULL,NULL);
#else
	pthread_create(NULL,NULL,NULL,NULL);
#endif
],
					[ol_cv_kthread_flag=yes], [ol_cv_kthread_flag=no])
				dnl restore the LIBS
				LIBS="$save_LIBS"
			])

			if test $ol_cv_kthread_flag = yes ; then
				LTHREAD_LIBS="$LTHREAD_LIBS -kthread"
				ol_link_threads=posix
			fi
		fi

		if test $ol_link_threads = no ; then
			dnl try -pthread
			AC_CACHE_CHECK([for pthread_create with -pthread],
				[ol_cv_pthread_flag], [
				dnl save the flags
				save_LIBS="$LIBS"
				LIBS="-pthread $LIBS"
				AC_TRY_LINK([#include <pthread.h>],[
#if HAVE_PTHREADS_D4
	pthread_create(NULL,pthread_attr_default,NULL,NULL);
#else
	pthread_create(NULL,NULL,NULL,NULL);
#endif
],
					[ol_cv_pthread_flag=yes], [ol_cv_pthread_flag=no])
				dnl restore the LIBS
				LIBS="$save_LIBS"
			])

			if test $ol_cv_pthread_flag = yes ; then
				LTHREAD_LIBS="$LTHREAD_LIBS -pthread"
				ol_link_threads=posix
			fi
		fi

		if test $ol_link_threads = no ; then
			dnl try -pthreads
			AC_CACHE_CHECK([for pthread_create with -pthreads],
				[ol_cv_pthreads_flag], [
				dnl save the flags
				save_LIBS="$LIBS"
				LIBS="-pthreads $LIBS"
				AC_TRY_LINK([#include <pthread.h>],[
#if HAVE_PTHREADS_D4
	pthread_create(NULL,pthread_attr_default,NULL,NULL);
#else
	pthread_create(NULL,NULL,NULL,NULL);
#endif
],
					[ol_cv_pthreads_flag=yes], [ol_cv_pthreads_flag=no])
				dnl restore the LIBS
				LIBS="$save_LIBS"
			])

			if test $ol_cv_pthreads_flag = yes ; then
				LTHREAD_LIBS="$LTHREAD_LIBS -pthreads"
				ol_link_threads=posix
			fi
		fi

		if test $ol_link_threads = no ; then
			dnl try -thread
			AC_CACHE_CHECK([for pthread_create with -thread],
				[ol_cv_thread_flag], [
				dnl save the flags
				save_LIBS="$LIBS"
				LIBS="-thread $LIBS"
				AC_TRY_LINK([char pthread_create();],
					[pthread_create();],
					[ol_cv_thread_flag=yes], [ol_cv_thread_flag=no])
				dnl restore the LIBS
				LIBS="$save_LIBS"
			])

			if test $ol_cv_thread_flag = yes ; then
				LTHREAD_LIBS="$LTHREAD_LIBS -thread"
				ol_link_threads=posix
			fi
		fi

		if test $ol_link_threads = no ; then
			dnl try DEC Threads -lpthread -lmach -lexc -lc_r
			save_LIBS="$LIBS"
			AC_CHECK_LIB(pthread, pthread_mutex_unlock, [
				ol_link_threads=posix
				LTHREAD_LIBS="$LTHREAD_LIBS -lpthread -lmach -lexc -lc_r"
				if test $ol_with_yielding_select = auto ; then
					ol_with_yielding_select=yes
				fi
				],:,[-lmach -lexc -lc_r])
			LIBS="$save_LIBS"
		fi

		if test $ol_link_threads = no ; then
			dnl try DEC Threads -lpthread -lmach -lexc
			save_LIBS="$LIBS"
			AC_CHECK_LIB(pthread, pthread_mutex_lock, [
				ol_link_threads=posix
				LTHREAD_LIBS="$LTHREAD_LIBS -lpthread -lmach -lexc"
				if test $ol_with_yielding_select = auto ; then
					ol_with_yielding_select=yes
				fi
				],:,[-lmach -lexc])
			LIBS="$save_LIBS"
		fi

		if test $ol_link_threads = no ; then
			dnl try DEC Threads -lpthread -lexc
			save_LIBS="$LIBS"
			AC_CHECK_LIB(pthread, pthread_mutex_trylock, [
				ol_link_threads=posix
				LTHREAD_LIBS="$LTHREAD_LIBS -lpthread -lexc"
				if test $ol_with_yielding_select = auto ; then
					ol_with_yielding_select=yes
				fi
				],:,[-lexc])
			LIBS="$save_LIBS"
		fi

		if test $ol_link_threads = no ; then
			dnl try -lpthread
			save_LIBS="$LIBS"
			AC_CHECK_LIB(pthread, pthread_create, [
				ol_link_threads=posix
				LTHREAD_LIBS="$LTHREAD_LIBS -lpthread"],:)
			LIBS="$save_LIBS"
		fi

		if test $ol_link_threads = no ; then
			dnl try -lc_r
			save_LIBS="$LIBS"
			AC_CHECK_LIB(c_r, pthread_create, [
				ol_link_threads=posix
				LTHREAD_LIBS="$LTHREAD_LIBS -lc_r"],:)
			LIBS="$save_LIBS"
		fi

		if test $ol_link_threads = no ; then
			dnl try -lpthreads
			save_LIBS="$LIBS"
			AC_CHECK_LIB(pthreads, pthread_create, [
				ol_link_threads=posix
				LTHREAD_LIBS="$LTHREAD_LIBS -lpthreads"],:)
			LIBS="$save_LIBS"
		fi

		dnl IRIX Pthread check
		if test $ol_link_threads = no ; then
			dnl try IRIX Pthreads -Wl,-woff,85 -lpthreads
			save_LIBS="$LIBS"
			AC_CHECK_LIB(pthreads, pthread_join, [
				ol_link_threads=posix
				LTHREAD_LIBS="$LTHREAD_LIBS -lpthreads"
				],:,[-Wl,-woff,85])
			LIBS="$save_LIBS"
		fi

		if test $ol_link_threads = no ; then
			dnl try -threads
			AC_CACHE_CHECK([for pthread_create with -threads],
				[ol_cv_thread_flag], [
				dnl save the flags
				save_LIBS="$LIBS"
				LIBS="-threads $LIBS"
				AC_TRY_LINK([char pthread_create();],[
					pthread_create();
					], ol_cv_thread_flag=yes, ol_cv_thread_flag=no)
				dnl restore the LIBS
				LIBS="$save_LIBS"
			])

			if test $ol_cv_thread_flag = yes ; then
				LTHREAD_LIBS="$LTHREAD_LIBS -threads"
				ol_link_threads=posix
			fi
		fi

		if test $ol_link_threads = no ; then
			dnl try DEC Threads -lpthreads -lmach -lexc -lc_r
			save_LIBS="$LIBS"
			AC_CHECK_LIB(pthreads, pthread_mutex_unlock, [
				ol_link_threads=posix
				LTHREAD_LIBS="$LTHREAD_LIBS -lpthreads -lmach -lexc -lc_r"
				if test $ol_with_yielding_select = auto ; then
					ol_with_yielding_select=yes
				fi
				],:,[-lmach -lexc -lc_r])
			LIBS="$save_LIBS"
		fi

		if test $ol_link_threads = no ; then
			dnl try DEC Threads -lpthreads -lmach -lexc
			save_LIBS="$LIBS"
			AC_CHECK_LIB(pthreads, pthread_mutex_lock, [
				ol_link_threads=posix
				LTHREAD_LIBS="$LTHREAD_LIBS -lpthreads -lmach -lexc"
				if test $ol_with_yielding_select = auto ; then
					ol_with_yielding_select=yes
				fi
				],:,[-lmach -lexc])
			LIBS="$save_LIBS"
		fi

		if test $ol_link_threads = no ; then
			dnl try DEC Threads -lpthreads -lexc
			save_LIBS="$LIBS"
			AC_CHECK_LIB(pthreads, pthread_mutex_trylock, [
				ol_link_threads=posix
				LTHREAD_LIBS="$LTHREAD_LIBS -lpthreads -lexc"
				if test $ol_with_yielding_select = auto ; then
					ol_with_yielding_select=yes
				fi
				],:,[-lexc])
			LIBS="$save_LIBS"
		fi

		if test $ol_link_threads != no ; then
			AC_DEFINE(HAVE_PTHREADS,1,
				[define if you have POSIX Threads])

			dnl save flags
			save_CPPFLAGS="$CPPFLAGS"
			save_LIBS="$LIBS"
			LIBS="$LTHREAD_LIBS $LIBS"

			dnl All POSIX Thread (final) implementations should have
			dnl sched_yield instead of pthread yield.
			dnl check for both
			AC_CHECK_FUNCS(sched_yield pthread_yield)

			if test $ac_cv_func_sched_yield = no -a \
				$ac_cv_func_pthread_yield = no ; then
				dnl Digital UNIX has sched_yield() in -lrt
				AC_CHECK_LIB(rt, sched_yield,
					[LTHREAD_LIBS="$LTHREAD_LIBS -lrt"
					AC_DEFINE(HAVE_SCHED_YIELD,1)
					ac_cv_func_sched_yield=yes],
					[ac_cv_func_sched_yield=no])
			fi
			if test $ac_cv_func_sched_yield = no -a \
				$ac_cv_func_pthread_yield = no ; then
				dnl Solaris has sched_yield() in -lposix4
				dnl but we'll use thr_yield instead.
				AC_CHECK_FUNCS(thr_yield)
			fi
			if test $ac_cv_func_sched_yield = no -a \
				$ac_cv_func_pthread_yield = no -a \
				"$ac_cv_func_thr_yield" = no ; then
				AC_MSG_WARN([could not locate sched_yield() or pthread_yield()])
			fi

			dnl Check functions for compatibility
			AC_CHECK_FUNCS(pthread_kill)

			dnl Check for pthread_detach with <pthread.h> inclusion
			dnl as it's symbol may have been mangled.
			AC_CACHE_CHECK([for pthread_detach with <pthread.h>],
				[ol_cv_func_pthread_detach], [
				dnl save the flags
				AC_TRY_LINK([#include <pthread.h>],
					[pthread_detach(NULL);],
					[ol_cv_func_pthread_detach=yes],
					[ol_cv_func_pthread_detach=no])
			])

			if test $ol_cv_func_pthread_detach = no ; then
				AC_MSG_ERROR([could not locate pthread_detach()])
			fi

			AC_DEFINE(HAVE_PTHREAD_DETACH,1,
				[define if you have pthread_detach function])

			dnl Check for setconcurreny functions
			AC_CHECK_FUNCS(	\
				pthread_setconcurrency \
				pthread_getconcurrency \
				thr_setconcurrency \
				thr_getconcurrency \
			)

			AC_MSG_CHECKING([if pthread_create() works])
			AC_CACHE_VAL(ol_cv_pthread_create_works,[
			AC_TRY_RUN([
#include <pthread.h>
#ifndef NULL
#define NULL (void*)0
#endif

static void *task(p)
	void *p;
{
	return (void *) (p == NULL);
}

int main(argc, argv)
	int argc;
	char **argv;
{
	pthread_t t;
#if HAVE_PTHREADS_D4
	exit(pthread_create(&t, pthread_attr_default, task, NULL));
#else
	exit(pthread_create(&t, NULL, task, NULL));
#endif
}
],
				[ol_cv_pthread_create_works=yes],
				[ol_cv_pthread_create_works=no],
				[dnl assume yes
				ol_cv_pthread_create_works=yes])])
			AC_MSG_RESULT($ol_cv_pthread_create_works)

			if test $ol_cv_pthread_create_works = no ; then
				AC_MSG_ERROR([pthread_create is not usable, check environment settings])
			fi

			dnl Check if select causes an yield
			if test $ol_with_yielding_select = auto ; then
				AC_MSG_CHECKING([if select yields when using pthreads])
				AC_CACHE_VAL(ol_cv_pthread_select_yields,[
				AC_TRY_RUN([
#include <sys/types.h>
#include <sys/time.h>
#include <unistd.h>
#include <pthread.h>
#ifndef NULL
#define NULL (void*) 0
#endif

static int fildes[2];

static void *task(p)
	void *p;
{
	int i;
	struct timeval tv;

	fd_set rfds;

	tv.tv_sec=10;
	tv.tv_usec=0;

	FD_ZERO(&rfds);
	FD_SET(fildes[0], &rfds);

	/* we're not interested in any fds */
	i = select(FD_SETSIZE, &rfds, NULL, NULL, &tv);

	if(i < 0) {
		perror("select");
		exit(10);
	}

	exit(0); /* if we exit here, the select blocked the whole process */
}

int main(argc, argv)
	int argc;
	char **argv;
{
	pthread_t t;

	/* create a pipe to select */
	if(pipe(&fildes[0])) {
		perror("select");
		exit(1);
	}

#ifdef HAVE_PTHREAD_SETCONCURRENCY
	(void) pthread_setconcurrency(2);
#else
#ifdef HAVE_THR_SETCONCURRENCY
	/* Set Solaris LWP concurrency to 2 */
	thr_setconcurrency(2);
#endif
#endif

#if HAVE_PTHREADS_D4
	pthread_create(&t, pthread_attr_default, task, NULL);
#else
	pthread_create(&t, NULL, task, NULL);
#endif

#if HAVE_SCHED_YIELD
	sched_yield();	/* make sure task runs first */
#else
#ifdef HAVE_PTHREAD_YIELD
	pthread_yield();	/* make sure task runs first */
#endif
#endif

	exit(2);
}],
				[ol_cv_pthread_select_yields=no],
				[ol_cv_pthread_select_yields=yes], [
				AC_MSG_ERROR([crossing compiling: use --with-yielding_select=yes|no|manual])])])
				AC_MSG_RESULT($ol_cv_pthread_select_yields)

				if test $ol_cv_pthread_select_yields = yes ; then
					ol_with_yielding_select=yes
				fi
			fi

			dnl restore flags
			CPPFLAGS="$save_CPPFLAGS"
			LIBS="$save_LIBS"
		else
			AC_MSG_ERROR([could not link with POSIX Threads])
		fi
	fi

	if test $ol_with_threads = posix ; then
		AC_MSG_ERROR([could not locate POSIX Threads])
	fi
fi

if test $ol_with_threads = auto -o $ol_with_threads = yes \
	-o $ol_with_threads = mach ; then

	dnl check for Mach CThreads
	AC_CHECK_HEADERS(mach/cthreads.h)
	if test $ac_cv_header_mach_cthreads_h = yes ; then
		ol_with_threads=found

		dnl check for cthread support in current $LIBS
		AC_CHECK_FUNC(cthread_fork,[ol_link_threads=yes])

		if test $ol_link_threads = no ; then
			dnl try -all_load
			dnl this test needs work
			AC_CACHE_CHECK([for cthread_fork with -all_load],
				[ol_cv_cthread_all_load], [
				dnl save the flags
				save_LIBS="$LIBS"
				LIBS="-all_load $LIBS"
				AC_TRY_LINK([#include <mach/cthreads.h>],[
					cthread_fork((void *)0, (void *)0);
					], ol_cv_cthread_all_load=yes, ol_cv_cthread_all_load=no)
				dnl restore the LIBS
				LIBS="$save_LIBS"
			])

			if test $ol_cv_cthread_all_load = yes ; then
				LTHREAD_LIBS="$LTHREAD_LIBS -all_load"
				ol_link_threads=mach
			fi
		fi

		if test $ol_link_threads != no ; then
			: check for cthread specific functionality here
			AC_DEFINE(HAVE_MACH_CTHREADS,1,
				[define if you have Mach Cthreads])
		else
			AC_MSG_ERROR([could not link with Mach CThreads])
		fi
	fi

	if test $ol_with_threads = mach ; then
		AC_MSG_ERROR([could not locate Mach CThreads])
	fi
fi

if test $ol_with_threads = auto -o $ol_with_threads = yes \
	-o $ol_with_threads = lwp ; then

	dnl check for SunOS5 LWP
	AC_CHECK_HEADERS(thread.h synch.h)
	if test $ac_cv_header_thread_h = yes -a $ac_cv_header_synch_h = yes ; then
		AC_CHECK_LIB(thread, thr_create, [have_thr=yes], [have_thr=no])

		if test $have_thr = yes ; then
			AC_DEFINE(HAVE_THR,1,
				[if you have Solaris LWP (thr) package])
			LTHREAD_LIBS="$LTHREAD_LIBS -lthread"
			ol_link_threads=thr

			if test $ol_with_yielding_select = auto ; then
				ol_with_yielding_select=yes
			fi

			dnl Check for setconcurreny functions
			AC_CHECK_FUNCS(	\
				thr_setconcurrency \
				thr_getconcurrency \
			)
		fi
	fi

	dnl check for SunOS4 LWP
	AC_CHECK_HEADERS(lwp/lwp.h)
	if test $ac_cv_header_lwp_lwp_h = yes ; then
		AC_CHECK_LIB(lwp, lwp_create, [have_lwp=yes], [have_lwp=no])

		if test $have_lwp = yes ; then
			AC_DEFINE(HAVE_LWP,1,
				[if you have SunOS LWP package])
			LTHREAD_LIBS="$LTHREAD_LIBS -llwp"
			ol_link_threads=lwp

			if test $ol_with_yielding_select = auto ; then
				ol_with_yielding_select=no
			fi
		fi
	fi
fi

if test $ol_with_yielding_select = yes ; then
	AC_DEFINE(HAVE_YIELDING_SELECT,1,
		[define if select implicitly yields])
fi

if test $ol_with_threads = manual ; then
	dnl User thinks he can manually configure threads.
	ol_link_threads=yes

	AC_MSG_WARN([thread defines and link options must be set manually])

	AC_CHECK_HEADERS(pthread.h sched.h)
	AC_CHECK_FUNCS(sched_yield pthread_yield)
	OL_LINUX_THREADS

	AC_CHECK_HEADERS(mach/cthreads.h)
	AC_CHECK_HEADERS(lwp/lwp.h)
	AC_CHECK_HEADERS(thread.h synch.h)
fi

if test $ol_link_threads != no ; then  
	dnl needed to get reentrant/threadsafe versions
	dnl
	AC_DEFINE(REENTRANT,1)
	AC_DEFINE(_REENTRANT,1)
	AC_DEFINE(THREAD_SAFE,1)
	AC_DEFINE(_THREAD_SAFE,1)
	AC_DEFINE(THREADSAFE,1)
	AC_DEFINE(_THREADSAFE,1)

	dnl this might cause the errno symbol to be
	dnl replaced with a function to get a thread specific errno.
	dnl check to see if everything needs to be compiled
	dnl with the thread libraries
	AC_CACHE_CHECK([for thread specific errno],
		[ol_cv_errno_thread_specific], [
		AC_TRY_LINK([#include <errno.h>], [int x = errno;],
			[ol_cv_errno_thread_specific=yes],
			[ol_cv_errno_thread_specific=no])
	])

	if test $ol_cv_errno_thread_specific != yes ; then
		LIBS="$LTHREAD_LIBS $LIBS"
		LTHREAD_LIBS=""
	fi

dnl When in thread environment, use 
dnl		#if defined( HAVE_REENTRANT_FUNCTIONS ) \ 
dnl			|| defined( HAVE_FUNC_R )
dnl			func_r(...);
dnl		#else
dnl		#	if defined( HAVE_THREADS ) 
dnl				/* lock */
dnl		#	endif
dnl				func(...);
dnl		#	if defined( HAVE_THREADS ) 
dnl				/* unlock */
dnl		#	endif
dnl		#endif
dnl
dnl HAVE_REENTRANT_FUNCTIONS is derived from:
dnl		_POSIX_REENTRANT_FUNCTIONS
dnl		_POSIX_THREAD_SAFE_FUNCTIONS
dnl		_POSIX_THREADSAFE_FUNCTIONS
dnl
dnl		and is currently defined in lthread.h
dnl
dnl HAVE_THREADS is defined by lthread.h iff -UNO_THREADS
dnl 
dnl libldap/*.c should only include <lthread.h> iff
dnl LDAP_R_COMPILE is defined.  ie:
dnl		#ifdef LDAP_R_COMPILE
dnl		#	include LDAP_R_COMPILE
dnl		#endif
dnl
dnl LDAP_R_COMIPLE is defined by libldap_r/Makefile.in
dnl specifically for compiling the threadsafe version of
dnl	the ldap library (-lldap_r).
dnl		
dnl	dnl check for reentrant/threadsafe functions
dnl	dnl
dnl	dnl note: these should only be used when linking
dnl	dnl       with $LTHREAD_LIBS
dnl	dnl
dnl	save_CPPFLAGS="$CPPFLAGS"
dnl	save_LIBS="$LIBS"
dnl	LIBS="$LTHREAD_LIBS $LIBS"
dnl	AC_CHECK_FUNCS(	\
dnl		gmtime_r \
dnl		gethostbyaddr_r gethostbyname_r \
dnl		feof_unlocked unlocked_feof \
dnl		putc_unlocked unlocked_putc \
dnl		flockfile ftrylockfile \
dnl	)
dnl	CPPFLAGS="$save_CPPFLAGS"
dnl	LIBS="$save_LIBS"
fi  

if test $ol_link_threads = no ; then
	if test $ol_with_threads = yes ; then
		AC_MSG_ERROR([no suitable thread support])
	fi

	if test $ol_with_threads = auto ; then
		AC_MSG_WARN([no suitable thread support, disabling threads])
		ol_with_threads=no
	fi

	AC_DEFINE(NO_THREADS,1,
		[define if you have (or want) no threads])
	LTHREAD_LIBS=""
fi

if test $ol_link_threads != no ; then
	AC_DEFINE(LDAP_API_FEATURE_X_OPENLDAP_THREAD_SAFE,1)
fi

dnl ----------------------------------------------------------------

ol_link_ldbm=no 
if test $ol_with_ldbm_api = auto -o $ol_with_ldbm_api = db2 ; then
	OL_BERKELEY_DB2

	if test $ol_cv_berkeley_db2 = yes ; then
		ol_link_ldbm=db2
		ol_with_ldbm_api=db2

		if test $ol_with_ldbm_type = hash ; then
			AC_DEFINE(LDBM_USE_DBHASH,1)
		else
			AC_DEFINE(LDBM_USE_DBBTREE,1)
		fi

		dnl $ol_cv_lib_db2 should be yes or -ldb
		dnl (it could be no, but that would be an error
		if test $ol_cv_lib_db2 != yes ; then
			LDBM_LIBS="$LDBM_LIBS $ol_cv_lib_db2"
		fi
	fi
fi

ol_link_bdb2=no
if test $ol_link_ldbm = db2 -a $ol_enable_bdb2 != no ; then
	ol_link_bdb2=yes
fi

if test $ol_with_ldbm_api = auto -o $ol_with_ldbm_api = db ; then
	OL_BERKELEY_DB

	if test $ol_cv_berkeley_db = yes ; then
		ol_link_ldbm=db
		ol_with_ldbm_api=db

		if test $ol_with_ldbm_type = hash ; then
			AC_DEFINE(LDBM_USE_DBHASH,1)
		else
			AC_DEFINE(LDBM_USE_DBBTREE,1)
		fi

		dnl $ol_cv_lib_db should be yes or -ldb
		dnl (it could be no, but that would be an error
		if test $ol_cv_lib_db != yes ; then
			LDBM_LIBS="$LDBM_LIBS $ol_cv_lib_db"
		fi
	fi
fi

if test $ol_with_ldbm_api = manual ; then
	dnl User thinks he can manually configure LDBM api.
	ol_link_ldbm=yes

	AC_MSG_WARN([LDBM defines and link options must be set manually])

	AC_CHECK_HEADERS(db.h db_185.h gdbm.h ndbm.h)
fi

if test $ol_link_ldbm = no -a $ol_with_ldbm_type = btree ; then
	AC_MSG_WARN(Could not find LDBM with BTREE support)
	ol_with_ldbm_api=none
fi

if test $ol_with_ldbm_api = auto -o $ol_with_ldbm_api = gdbm ; then
	OL_GDBM

	if test $ol_cv_gdbm = yes ; then
		ol_link_ldbm=gdbm
		ol_with_ldbm_api=gdbm

		if test $ol_cv_lib_gdbm != yes ; then
			LDBM_LIBS="$LDBM_LIBS $ol_cv_lib_gdbm"
		fi
	fi
fi

if test $ol_with_ldbm_api = auto ; then
	AC_MSG_WARN([skipping automatic checking for NDBM, must be manually enabled.])
elif test $ol_with_ldbm_api = ndbm ; then
	OL_NDBM

	if test $ol_cv_ndbm = yes ; then
		ol_link_ldbm=ndbm
		ol_with_ldbm_api=ndbm

		if test $ol_cv_lib_ndbm != yes ; then
			LDBM_LIBS="$LDBM_LIBS $ol_cv_lib_ndbm"
		fi
	fi
fi

if test $ol_link_ldbm = no -a $ol_enable_ldbm != no ; then
	AC_MSG_WARN(could not find suitable LDBM backend)
	if test $ol_enable_ldbm = yes ; then
		AC_MSG_ERROR(select appropriate LDBM options or disable)
	fi

	AC_MSG_WARN(disabling LDBM)
	ol_enable_ldbm=no
fi

if test $ol_enable_wrappers != no ; then
	AC_CHECK_HEADERS(tcpd.h)

	if test $ac_cv_header_tcpd_h != yes ; then
		have_wrappers=no
	else
		AC_CHECK_LIB(wrap, main,
			[have_wrappers=yes], [have_wrappers=no])
	fi

	if test $have_wrappers = yes ; then
		AC_DEFINE(HAVE_TCPD,1, [define if you have -lwrap])
		SLAPD_LIBS="$SLAPD_LIBS -lwrap"
	else
		AC_MSG_WARN(could not find -lwrap)
		if test $ol_enable_wrappers = yes ; then
			AC_MSG_ERROR(could not find wrappers, select appropriate options or disable)
		fi

		AC_MSG_WARN(disabling wrappers support)
		ol_enable_wrappers=no
	fi
fi

if test $ol_enable_syslog != no ; then
	AC_CHECK_FUNC(openlog)
	if test $ac_cv_func_openlog = no -a $ol_enable_syslog = yes; then
		AC_MSG_ERROR(could not find syslog, select appropriate options or disable)
	fi
	ol_enable_syslog=$ac_cv_func_openlog
fi

if test $ol_enable_dmalloc != no ; then
	AC_CHECK_HEADERS(dmalloc.h)
	AC_CHECK_LIB(dmalloc, dmalloc_shutdown)
fi

if test $ol_enable_tcl != no ; then
	AC_CHECK_HEADERS(tcl.h)

	if test $ac_cv_header_tcl_h != yes ; then
		have_tcl=no
	else
		AC_CHECK_LIB(tcl,main,
			[have_tcl=yes; SLAPD_LIBS="$SLAPD_LIBS -ltcl"],
			[have_tcl=no])

		if test $have_tcl != yes; then
			AC_CHECK_LIB(tcl7.6,main,
				[have_tcl=yes; SLAPD_LIBS="$SLAPD_LIBS -ltcl7.6"],
				[have_tcl=no])
		fi

		if test $have_tcl != yes; then
			AC_CHECK_LIB(tcl8.0,main,
				[have_tcl=yes; SLAPD_LIBS="$SLAPD_LIBS -ltcl8.0"],
				[have_tcl=no])
		fi
	fi

	if test $have_tcl != yes ; then
		AC_MSG_WARN([could not find -ltcl])
		if test $ol_enable_tcl = yes ; then
			AC_MSG_ERROR([could not find tcl, select appropriate options or disable])
		fi

		ol_enable_tcl=no
	fi
fi

# ud needs termcap (should insert check here)
ol_link_termcap=no
AC_CHECK_HEADERS(termcap.h ncurses.h)

if test $ol_link_termcap = no ; then
	AC_CHECK_LIB(termcap, tputs, [have_termcap=yes], [have_termcap=no])
	if test $have_termcap = yes ; then
		AC_DEFINE(HAVE_TERMCAP, 1, [define if you have -ltermcap])
		ol_link_termcap=yes
		TERMCAP_LIBS=-ltermcap
	fi
fi

if test $ol_link_termcap = no ; then
	AC_CHECK_LIB(ncurses, initscr, [have_ncurses=yes], [have_ncurses=no])
	if test $have_ncurses = yes ; then
		AC_DEFINE(HAVE_NCURSES, 1, [define if you have -lncurses])
		ol_link_termcap=yes
		TERMCAP_LIBS=-lncurses
	fi
fi

if test $ol_link_termcap = no ; then
	AC_DEFINE(NO_TERMCAP,1, [define if you have no termcap support])
	TERMCAP_LIBS=
fi

dnl
dnl Check for GNU readline
dnl
ol_link_readline=no
if test $ol_with_readline != no ; then
	AC_CHECK_HEADERS(readline/readline.h readline/history.h)

	if test $ac_cv_header_readline_readline_h = yes ; then
		save_LIBS="$LIBS"
		LIBS="$TERMCAP_LIBS $LIBS"
		AC_CHECK_LIB(readline, readline, 
			[have_readline=yes], [have_readline=no])
		LIBS="$save_LIBS"
			
		if test $have_readline = yes ; then
			ol_with_readline=found
			ol_link_readline=yes

			READLINE_LIBS="-lreadline"
		fi
	fi
fi

if test $ol_link_readline = yes ; then
	AC_DEFINE(HAVE_READLINE, 1, [define if you have -lreadline])
fi


# FreeBSD (and others) have crypt(3) in -lcrypt
if test $ol_enable_crypt != no ; then
	AC_CHECK_FUNC(crypt, [have_crypt=yes], [
		AC_CHECK_LIB(crypt, crypt, [LUTIL_LIBS="$LUTIL_LIBS -lcrypt"
			have_crypt=yes], [have_crypt=no])])

	if test $have_crypt = yes ; then
		AC_DEFINE(HAVE_CRYPT,1)
	else
		AC_MSG_WARN(could not find crypt)
		if test $ol_enable_crypt = yes ; then
			AC_MSG_ERROR(could not find crypt, select appropriate options or disable)
		fi

		AC_MSG_WARN(disabling crypt support)
		ol_enable_crypt=no
	fi
fi

# FreeBSD (and others) have setproctitle(3) in -lutil
if test $ol_enable_proctitle != no ; then
	AC_CHECK_FUNC(setproctitle,	[have_setproctitle=yes], [
		AC_CHECK_LIB(util, setproctitle,
			[have_setproctitle=yes
			LUTIL_LIBS="$LUTIL_LIBS -lutil"],
			[have_setproctitle=no
			LIBOBJS="$LIBOBJS setproctitle.o"])])

	if test $have_setproctitle = yes ; then
		AC_DEFINE(HAVE_SETPROCTITLE,1)
	fi
fi

dnl ----------------------------------------------------------------
dnl Checks for header files.
AC_HEADER_STDC

if test $ac_cv_header_stdc != yes; then
	AC_MSG_WARN([could not Standard C compliant headers])
fi

AC_HEADER_DIRENT
AC_HEADER_SYS_WAIT
AM_HEADER_TIOCGWINSZ_NEEDS_SYS_IOCTL
if test $am_cv_sys_posix_termios = yes ; then
	AC_DEFINE(HAVE_POSIX_TERMIOS,1,
		[define if you have POSIX termios])
fi

AC_CHECK_HEADERS(		\
	arpa/inet.h		\
	arpa/nameser.h		\
	crypt.h			\
	errno.h			\
	fcntl.h			\
	filio.h			\
	getopt.h		\
	libutil.h		\
	limits.h		\
	locale.h		\
	malloc.h		\
	memory.h		\
	regex.h			\
	psap.h			\
	pwd.h			\
	resolv.h		\
	sgtty.h			\
	stdarg.h		\
	stddef.h		\
	string.h		\
	strings.h		\
	sysexits.h		\
	sys/file.h		\
	sys/filio.h		\
	sys/errno.h		\
	sys/ioctl.h		\
	sys/param.h		\
	sys/resource.h		\
	sys/socket.h		\
	sys/syslog.h		\
	sys/time.h		\
	sys/types.h		\
	syslog.h		\
	termios.h		\
	unistd.h		\
)

dnl ----------------------------------------------------------------
dnl Checks for typedefs, structures, and compiler characteristics.
AC_TYPE_GETGROUPS dnl requires AC_TYPE_UID_T
AC_TYPE_MODE_T
AC_TYPE_OFF_T
AC_TYPE_PID_T
AM_TYPE_PTRDIFF_T
AC_TYPE_SIGNAL
OL_TYPE_SIG_ATOMIC_T
AC_TYPE_SIZE_T
AC_STRUCT_ST_BLKSIZE
AC_HEADER_TIME
AC_STRUCT_TM

OL_C_UPPER_LOWER
AC_C_CONST
OL_C_VOLATILE

if test $cross_compiling = yes ; then
	AC_DEFINE(CROSS_COMPILING, 1, [define if cross compiling])
else
	AC_C_BIGENDIAN
	AC_CHECK_SIZEOF(short) 
	AC_CHECK_SIZEOF(int) 
	AC_CHECK_SIZEOF(long)
fi

dnl ----------------------------------------------------------------
dnl Checks for library functions.
AC_FUNC_MEMCMP
dnl AM_FUNC_MKTIME dnl checks for sys/time.h and unistd.h
AC_FUNC_STRFTIME
dnl AM_FUNC_STRTOD
AC_FUNC_VPRINTF

if test $ac_cv_func_vprintf = yes ; then
	dnl check for vsnprintf
	AC_CHECK_FUNCS(vsnprintf vsprintf)
fi

AC_CHECK_FUNCS(		\
	bcopy			\
	flock			\
	getdtablesize		\
	gethostname		\
	getpass			\
	getpwuid		\
	gettimeofday		\
	lockf			\
	memcpy			\
	memmove			\
	mkstemp			\
	select			\
	setpwfile		\
	setsid			\
	signal			\
	sigset			\
	snprintf		\
	socket			\
	strdup			\
	strerror		\
	strpbrk			\
	strrchr			\
	strsep			\
	strstr			\
	strtol			\
	strtoul			\
	strspn			\
	sysconf			\
	waitpid			\
)

dnl We actually may need to replace more than this.
AC_REPLACE_FUNCS(getopt tempnam)

dnl ----------------------------------------------------------------
# Check Configuration
OL_SYS_ERRLIST

dnl ----------------------------------------------------------------
dnl Sort out defines

if test "$ol_enable_debug" != no ; then
	AC_DEFINE(LDAP_DEBUG,1)
fi
if test "$ol_enable_syslog" = yes ; then
	AC_DEFINE(LDAP_SYSLOG,1)
fi
if test "$ol_enable_libui" = yes ; then
	AC_DEFINE(LDAP_LIBUI,1)
fi
if test "$ol_enable_cache" = no ; then
	AC_DEFINE(LDAP_NOCACHE,1)
fi
if test "$ol_enable_dns" != no ; then
	AC_DEFINE(LDAP_API_FEATURE_X_OPENLDAP_V2_DNS,LDAP_VENDOR_VERSION)
fi
if test "$ol_enable_proctitle" != no ; then
	AC_DEFINE(LDAP_PROCTITLE,1)
fi
if test "$ol_enable_referrals" != no ; then
	AC_DEFINE(LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS,LDAP_VENDOR_VERSION)
fi
if test "$ol_enable_cldap" != no ; then
	AC_DEFINE(LDAP_CONNECTIONLESS,1)
fi

if test "$ol_enable_aclgroups" != no ; then
	AC_DEFINE(SLAPD_ACLGROUPS,1)
fi
if test "$ol_enable_crypt" != no ; then
	AC_DEFINE(SLAPD_CRYPT,1)
fi
if test "$ol_enable_cleartext" != no ; then
	AC_DEFINE(SLAPD_CLEARTEXT,1)
fi
if test "$ol_enable_phonetic" != no ; then
	AC_DEFINE(SLAPD_PHONETIC,1)
fi
if test "$ol_enable_rlookups" != no ; then
	AC_DEFINE(SLAPD_RLOOKUPS,1)
fi

if test "$ol_link_bdb2" != no ; then
	AC_DEFINE(SLAPD_BDB2,1)
	BUILD_SLAPD=yes
	BUILD_BDB2=yes
fi

if test "$ol_link_ldbm" != no ; then
	AC_DEFINE(SLAPD_LDBM,1)
	BUILD_SLAPD=yes
	BUILD_LDBM=yes
fi

if test "$ol_enable_passwd" != no ; then
	AC_DEFINE(SLAPD_PASSWD,1)
	BUILD_SLAPD=yes
	BUILD_PASSWD=yes
fi

if test "$ol_enable_perl" != no ; then
	AC_DEFINE(SLAPD_PERL,1)
	BUILD_SLAPD=yes
	BUILD_PERL=yes
fi

if test "$ol_enable_shell" != no ; then
	AC_DEFINE(SLAPD_SHELL,1)
	BUILD_SLAPD=yes
	BUILD_SHELL=yes
fi

if test "$ol_enable_tcl" != no ; then
	AC_DEFINE(SLAPD_TCL,1)
	BUILD_SLAPD=yes
	BUILD_TCL=yes
fi

if test "$ol_enable_slurpd" != no -a "$ol_link_threads" != no -a \
	$BUILD_SLAPD = yes ; then
	BUILD_SLURPD=yes
fi

if test "$ol_link_isode" != no ; then
	BUILD_LDAPD=yes
fi

dnl ----------------------------------------------------------------

AC_SUBST(BUILD_LDAPD)
AC_SUBST(BUILD_SLAPD)
  AC_SUBST(BUILD_BDB2)
  AC_SUBST(BUILD_LDBM)
  AC_SUBST(BUILD_PASSWD)
  AC_SUBST(BUILD_PERL)
  AC_SUBST(BUILD_SHELL)
  AC_SUBST(BUILD_TCL)
AC_SUBST(BUILD_SLURPD)

AC_SUBST(LDAP_LIBS)
AC_SUBST(LDAPD_LIBS)
AC_SUBST(SLAPD_LIBS)
AC_SUBST(SLURPD_LIBS)
AC_SUBST(LDBM_LIBS)
AC_SUBST(LTHREAD_LIBS)
AC_SUBST(LUTIL_LIBS)

AC_SUBST(SLAPD_PERL_CPPFLAGS)
AC_SUBST(SLAPD_PERL_LDFLAGS)

AC_SUBST(KRB_LIBS)
AC_SUBST(READLINE_LIBS)
AC_SUBST(TERMCAP_LIBS)
AC_SUBST(TLS_LIBS)

dnl ----------------------------------------------------------------
dnl final output
dnl

AC_OUTPUT( \
Makefile:build/top.mk:Makefile.in:build/dir.mk \
doc/Makefile:build/top.mk:doc/Makefile.in:build/dir.mk \
doc/man/Makefile:build/top.mk:doc/man/Makefile.in:build/dir.mk \
doc/man/man1/Makefile:build/top.mk:doc/man/man1/Makefile.in:build/man.mk \
doc/man/man3/Makefile:build/top.mk:doc/man/man3/Makefile.in:build/man.mk \
doc/man/man5/Makefile:build/top.mk:doc/man/man5/Makefile.in:build/man.mk \
doc/man/man8/Makefile:build/top.mk:doc/man/man8/Makefile.in:build/man.mk \
clients/Makefile:build/top.mk:clients/Makefile.in:build/dir.mk \
clients/finger/Makefile:build/top.mk:clients/finger/Makefile.in:build/rules.mk \
clients/fax500/Makefile:build/top.mk:clients/fax500/Makefile.in:build/rules.mk \
clients/gopher/Makefile:build/top.mk:clients/gopher/Makefile.in:build/rules.mk \
clients/mail500/Makefile:build/top.mk:clients/mail500/Makefile.in:build/rules.mk \
clients/rcpt500/Makefile:build/top.mk:clients/rcpt500/Makefile.in:build/rules.mk \
clients/ud/Makefile:build/top.mk:clients/ud/Makefile.in:build/rules.mk \
clients/tools/Makefile:build/top.mk:clients/tools/Makefile.in:build/rules.mk \
include/Makefile:build/top.mk:include/Makefile.in \
libraries/Makefile:build/top.mk:libraries/Makefile.in:build/dir.mk 	\
libraries/libavl/Makefile:build/top.mk:libraries/libavl/Makefile.in:build/lib.mk:build/lib-static.mk	\
libraries/liblber/Makefile:build/top.mk:libraries/liblber/Makefile.in:build/lib.mk:build/lib-shared.mk	\
libraries/libldap/Makefile:build/top.mk:libraries/libldap/Makefile.in:build/lib.mk:build/lib-shared.mk	\
libraries/libldap_r/Makefile:build/top.mk:libraries/libldap_r/Makefile.in:build/lib.mk:build/lib-shared.mk	\
libraries/libldbm/Makefile:build/top.mk:libraries/libldbm/Makefile.in:build/lib.mk:build/lib-static.mk	\
libraries/libldif/Makefile:build/top.mk:libraries/libldif/Makefile.in:build/lib.mk:build/lib-static.mk	\
libraries/liblutil/Makefile:build/top.mk:libraries/liblutil/Makefile.in:build/lib.mk:build/lib-static.mk	\
servers/Makefile:build/top.mk:servers/Makefile.in:build/dir.mk \
servers/ldapd/Makefile:build/top.mk:servers/ldapd/Makefile.in:build/srv.mk \
servers/slapd/Makefile:build/top.mk:servers/slapd/Makefile.in:build/srv.mk \
servers/slapd/back-bdb2/Makefile:build/top.mk:servers/slapd/back-bdb2/Makefile.in:build/srv.mk \
servers/slapd/back-ldbm/Makefile:build/top.mk:servers/slapd/back-ldbm/Makefile.in:build/srv.mk \
servers/slapd/back-passwd/Makefile:build/top.mk:servers/slapd/back-passwd/Makefile.in:build/srv.mk \
servers/slapd/back-perl/Makefile:build/top.mk:servers/slapd/back-perl/Makefile.in:build/srv.mk \
servers/slapd/back-shell/Makefile:build/top.mk:servers/slapd/back-shell/Makefile.in:build/srv.mk \
servers/slapd/back-tcl/Makefile:build/top.mk:servers/slapd/back-tcl/Makefile.in:build/srv.mk \
servers/slapd/shell-backends/Makefile:build/top.mk:servers/slapd/shell-backends/Makefile.in:build/srv.mk \
servers/slapd/tools/Makefile:build/top.mk:servers/slapd/tools/Makefile.in \
servers/slurpd/Makefile:build/top.mk:servers/slurpd/Makefile.in:build/srv.mk \
tests/Makefile:build/top.mk:tests/Makefile.in:build/dir.mk \
tests/progs/Makefile:build/top.mk:tests/progs/Makefile.in:build/rules.mk \
contrib/Makefile:build/top.mk:contrib/Makefile.in:build/dir.mk \
contrib/saucer/Makefile:build/top.mk:contrib/saucer/Makefile.in:build/rules.mk \
,[
date > stamp-h
echo Please \"make depend\" to build dependencies
])
