AC_INIT(auth_ldap.c)
AC_ARG_WITH(apxs,          --with-apxs                   Path to Apache's apxs command, test $withval = no || APXS=$withval)
AC_ARG_WITH(ldap-sdk,      --with-ldap-sdk=netscape|openldap  Specify the LDAP SDK to use)
AC_ARG_WITH(sdk-directory, --with-sdk-headers            Specify the path to the LDAP SDK header files)
AC_ARG_WITH(sdk-directory, --with-sdk-libs               Specify the path to the LDAP SDK libraries)

AC_ARG_WITH(ssl,           --with-ssl                    Build with ssl support, test $withval = no || SSL=-DWITH_SSL)
AC_ARG_WITH(shared-cache,  --with-shared-cache           Build with shared cache support, 
test $withval = no || SHARED_CACHE=-DWITH_SHARED_LDAP_CACHE, SHARED_CACHE=-DWITH_SHARED_LDAP_CACHE)
AC_ARG_WITH(activate,      --with-activate               Activate auth_ldap.so in http.conf, 
test $withval = yes && ACTIVATE=-a)
AC_ARG_WITH(frontpage,     --with-frontpage              Enable FrontPage hack, 
test $withval=no || FRONTPAGE=-DAUTH_LDAP_FRONTPAGE_HACK)

if test -z "$APXS"; then
	APXS=apxs
fi
test $APXS = yes && APXS=false
MYCC='`$(APXS) -q CC`'

case $target in
    *aix*)
	AIX_EXP=auth_ldap.exp
	AIX_LDFLAGS_SHLIB=-bE:`echo $@|sed -e 's:\.so$$:.exp:'`
	;;
esac

AC_SUBST(MYCC)
AC_SUBST(OPENLDAP)
AC_SUBST(SSL)
AC_SUBST(SHARED_CACHE)
AC_SUBST(ACTIVATE)
AC_SUBST(FRONTPAGE)
AC_SUBST(AIX_EXP)
AC_SUBST(AIX_LDFLAGS_SHLIB)

test -z "$with_ldap_sdk" && with_ldap_sdk=openldap

AC_CHECK_PROG(APXS, apxs, apxs, $with_apxs)

AC_MSG_CHECKING(whether apxs works)
test -z "$APXS" && AC_MSG_ERROR(Cannot build auth_ldap without a valid apxs program)
$APXS -q CFLAGS > /dev/null
if test $? = 0; then
   AC_MSG_RESULT(yes)
else
   AC_MSG_RESULT(no)
   AC_MSG_ERROR(Cannot build auth_ldap without a valid apxs program)
fi

if test -n "$with_sdk_headers"; then
   CPPFLAGS="-I$with_sdk_headers $CPPFLAGS"
fi
if test -n "$with_sdk_libs"; then
   LDFLAGS="-L$with_sdk_libs $LDFLAGS"
fi

check_netscape_lib()
{
	if test $found_netscape_libs = no; then
	  thelib=$1
	  extralibs=$2

	  unset ac_cv_lib_${thelib}_ldap_init
	  found_netscape_libs=yes
	  AC_CHECK_LIB($thelib, ldap_init, LIBS="-l$thelib $extralibs $LIBS", 
	               found_netscape_libs=no, $extralibs)
	fi
}

case $with_ldap_sdk in
    openldap)
	OPENLDAP=-DWITH_OPENLDAP
        AC_CHECK_LIB(lber, ber_init, , , -lnsl)
        AC_CHECK_LIB(ldap, ldap_init)
        ;;

    netscape)
	OPENLDAP=
	found_netscape_libs=no

	check_netscape_lib ldapssl41 '-lnspr3 -lplc3 -lplds3'	# Generic
	check_netscape_lib ldapssl41 '-lnspr3 -lplc3 -lplds3 -lsocket -lnsl -lmt' # Solaris
	check_netscape_lib ldapssl41 '-lnspr3 -lplc3 -lplds3 -lpthread' # Linux
	check_netscape_lib ldapssl40
	check_netscape_lib ldapssl40 -lpthread
	check_netscape_lib ldapssl30
	check_netscape_lib ldapssl30 -lpthread
	check_netscape_lib ldapssl20
	check_netscape_lib ldapssl20 -lpthread

	test $found_netscape_libs = no && AC_MSG_ERROR(could not find any Netscape library)
	;;
esac

dnl Checks for header files.
AC_HEADER_STDC

dnl Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST

dnl Checks for library functions.
AC_FUNC_VPRINTF
AC_CHECK_FUNCS(strdup strerror)

AC_OUTPUT(Makefile)
