AC_INIT(mod_ldap_userdir.c)

AC_ARG_WITH(apxs,
            [AC_HELP_STRING(
              [--with-apxs],
              [Path to Apache's apxs command (version 1.3.x or 2.0.x)])
            ],
            APXS=$withval)
AC_ARG_WITH(activate,
            [AC_HELP_STRING(
              [--with-activate],
              [Activate mod_ldap_userdir in httpd.conf])
            ],
            test "$withval" != no && ACTIVATE=-a)
AC_ARG_WITH(sdk-headers,
            [AC_HELP_STRING(
              [--with-sdk-headers],
              [Specify the path to the LDAP SDK header files])
            ],
            [CPPFLAGS="-I$withval"])
AC_ARG_WITH(sdk-libdir,
            [AC_HELP_STRING(
              [--with-sdk-libdir],
              [Specify the path to the LDAP SDK libraries])
            ],
            [LDFLAGS="-L$withval"])

AC_PROG_CC
dnl We need to reset CFLAGS after AC_PROG_CC sets it. apxs will provide
dnl its own CFLAGS and gets confused when we try to feed ours to it.
CFLAGS=

if test -z "$APXS" || test "$APXS" = yes; then
	for location in /usr/local/apache/bin/apxs /usr/local/apache2/bin/apxs \
	                /opt/csw/apache2/sbin/apxs
	do
		if test -e $location; then
			APXS=$location
		fi
	done

	if test -z "$APXS"; then
		APXS=apxs
	fi
fi

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

# There must be a better way, but I've yet to find it.
if echo $APXS | grep apxs2 >/dev/null; then
	TARGET=.libs/mod_ldap_userdir.so
elif echo $APXS | grep apache2 >/dev/null; then
	TARGET=.libs/mod_ldap_userdir.so
else
	TARGET=mod_ldap_userdir.so
fi

AC_CHECK_LIB(lber, ber_init)
AC_CHECK_LIB(ldap, ldap_init, found_openldap_libs=yes; LIBS="-lldap $LIBS",
	AC_MSG_ERROR(You must have libldap to build mod_ldap_userdir.))

AC_CHECK_HEADERS(lber.h, ,
	AC_MSG_ERROR(You must have lber.h to build mod_ldap_userdir.))
AC_CHECK_HEADERS(ldap.h, ,
	AC_MSG_ERROR(You must have ldap.h to build mod_ldap_userdir.),
	[#include <lber.h>])

dnl AC_CHECK_MEMBER(struct ldap_url_desc.lud_scheme,
dnl 	[AC_DEFINE(HAVE_LDAPURLDESC_LUD_SCHEME, 1, [Define if your LDAPURLDesc has a lud_scheme member.])], ,
dnl 	[#include <lber.h>
dnl #include <ldap.h>])
AC_CHECK_MEMBER(LDAPURLDesc.lud_scheme,
	[AC_DEFINE(HAVE_LDAPURLDESC_LUD_SCHEME, 1, [Define if your LDAPURLDesc has a lud_scheme member.])], ,
	[#include <lber.h>
#include <ldap.h>])

AC_ARG_WITH(tls,
            [AC_HELP_STRING(
              [--without-tls],
              [Don't build with TLS (LDAP over SSL) support])
            ], ,
            [AC_CHECK_FUNC(ldap_start_tls_s, TLS=-DTLS)])

AC_SUBST(APXS TARGET)
AC_SUBST(CPPFLAGS LDFLAGS LIBS)
AC_SUBST(TLS ACTIVATE)

AC_CONFIG_HEADER(config.h)
AC_OUTPUT(Makefile)
