AC_INIT(ldap-nss.c)
AC_CANONICAL_SYSTEM
AC_PREFIX_DEFAULT()

AM_INIT_AUTOMAKE(nss_ldap, 186)
AM_CONFIG_HEADER(config.h)

AC_PROG_CC
AC_PROG_CPP
AC_PROG_INSTALL

AC_ARG_ENABLE(rfc2307bis, [  --enable-rfc2307bis       use RFC2307bis schema ], [AC_DEFINE(RFC2307BIS)])
AC_ARG_ENABLE(schema-mapping, [  --enable-schema-mapping   enable attribute/objectclass mapping ], [AC_DEFINE(AT_OC_MAP)])
AC_ARG_ENABLE(proxy-auth, [  --enable-proxy-auth       enable proxy authentication for AIX ], [AC_DEFINE(PROXY_AUTH)])
AC_ARG_ENABLE(debugging, [  --enable-debugging        enable debug code ], [AC_DEFINE(DEBUG)])
AC_ARG_ENABLE(ssl, [  --disable-ssl             disable SSL/TSL support])
AC_ARG_WITH(ldap-lib, [  --with-ldap-lib=type      select ldap library [auto|netscape5|netscape4|netscape3|umich|openldap]])
AC_ARG_WITH(ldap-dir, [  --with-ldap-dir=DIR       base directory of ldap SDK])
AC_ARG_WITH(ldap-conf-file, [  --with-ldap-conf-file     path to LDAP configuration file], [AC_DEFINE_UNQUOTED(NSS_LDAP_PATH_CONF, "$with_ldap_conf_file")])
AC_ARG_WITH(ldap-secret-file, [  --with-ldap-secret-file   path to LDAP root secret file], [AC_DEFINE_UNQUOTED(NSS_LDAP_PATH_ROOTPASSWD, "$with_ldap_secret_file")])

AM_CONDITIONAL(GLIBC, test "$target_os" = "linux" -o "$target_os" = "linux-gnu")

case "$target_os" in
aix*) is_aix=yes ;;
*) is_aix=no ;;
esac

AM_CONDITIONAL(AIX, test "$is_aix" = yes)

if test "$ac_cv_prog_gcc" = "yes"; then CFLAGS="$CFLAGS -Wall -fPIC"; fi

dnl This is needed for the native Solaris LDAP SDK
CPPFLAGS="$CPPFLAGS -DLDAP_REFERRALS"

case "$target_os" in
freebsd*) CPPFLAGS="$CPPFLAGS -DPIC -D_REENTRANT" ;;
aix*) CPPFLAGS="$CPPFLAGS -D_THREAD_SAFE" ;;
*) CPPFLAGS="$CPPFLAGS -D_REENTRANT" ;;
esac

case "$target_os" in
aix*) if test "$ac_compiler_gnu" = yes; then
    nss_ldap_so_LDFLAGS="-Wl,-G -Wl,-bdynamic -Wl,-bnoentry -Wl,-bE:\$(srcdir)/exports.aix"
  else
    nss_ldap_so_LDFLAGS="-bM:SRE -bnoentry -bE:\$(srcdir)/exports.aix"
  fi 
  need_pthread=yes ;;
solaris*) nss_ldap_so_LDFLAGS="-Wl,-Bdynamic -Wl,-M -Wl,\$(srcdir)/mapfile -Wl,-G" ;;
*) nss_ldap_so_LDFLAGS="-shared -Wl,-Bdynamic" ;;
esac
AC_SUBST(nss_ldap_so_LDFLAGS)

if test -n "$with_ldap_dir"; then
  CPPFLAGS="$CPPFLAGS -I$with_ldap_dir/include"
  LDFLAGS="$LDFLAGS -L$with_ldap_dir/lib"
  case "$target_os" in  
  aix*) if test "$ac_compiler_gnu" = yes; then
      LDFLAGS="$LDFLAGS -Wl,-blibpath:$with_ldap_dir/lib"
    else
      LDFLAGS="$LDFLAGS -blibpath:$with_ldap_dir/lib"
    fi ;;
  solaris*) LDFLAGS="$LDFLAGS -Wl,-R$with_ldap_dir/lib" ;;  
  *) LDFLAGS="$LDFLAGS -Wl,-rpath,$with_ldap_dir/lib" ;;
  esac  
fi

AC_CHECK_HEADERS(lber.h)
AC_CHECK_HEADERS(ldap.h, , AC_MSG_ERROR(could not locate <ldap.h>))
AC_CHECK_HEADERS(ldap_ssl.h)
AC_CHECK_HEADERS(nss.h)
AC_CHECK_HEADERS(nsswitch.h)
AC_CHECK_HEADERS(irs.h)
AC_CHECK_HEADERS(thread.h)
AC_CHECK_HEADERS(pthread.h)
AC_CHECK_HEADERS(malloc.h)
AC_CHECK_HEADERS(shadow.h)
AC_CHECK_HEADERS(port_before.h)
AC_CHECK_HEADERS(port_after.h)
AC_CHECK_HEADERS(aliases.h)
AC_CHECK_HEADERS(netinet/if_ether.h)
AC_CHECK_HEADERS(netinet/ether.h)
AC_CHECK_HEADERS(ctype.h)
AC_CHECK_HEADERS(db.h)
AC_CHECK_HEADERS(db1/db.h)
AC_CHECK_HEADERS(db_185.h)
AC_CHECK_HEADERS(db3/db_185.h)
AC_CHECK_HEADERS(alignof.h)
AC_CHECK_HEADERS(rpc/rpcent.h)
AC_CHECK_HEADERS(sys/byteorder.h)
AC_CHECK_HEADERS(libc-lock.h)
AC_CHECK_HEADERS(bits/libc-lock.h)
AC_CHECK_HEADERS(sasl.h)
AC_CHECK_HEADERS(strings.h)

AC_CHECK_LIB(resolv, main)
AC_CHECK_LIB(nsl, main)

AC_CHECK_FUNCS(strtok_r)
AC_CHECK_FUNCS(sigset)
AC_CHECK_FUNCS(res_search)
AC_CHECK_FUNCS(dn_expand)
AC_CHECK_FUNCS(snprintf)
AC_CHECK_FUNCS(gethostbyname)
AC_CHECK_FUNCS(nsdispatch)
AC_CHECK_FUNCS(pthread_atfork)

dnl check which ldap library we have
if test -z "$with_ldap_lib"; then
  with_ldap_lib=auto
fi

AC_CHECK_LIB(dl, dlopen,[LIBS="-ldl $LIBS"],,$LIBS)
AC_CHECK_LIB(db, main,[LIBS="-ldb $LIBS"],,$LIBS)
dnl Following checks probably not strictly necessary.
dnl AC_CHECK_LIB(crypto, main,[LIBS="-lcrypto $LIBS"],,$LIBS)
dnl AC_CHECK_LIB(ssl, main,[LIBS="-lssl $LIBS"],,$LIBS)
dnl AC_CHECK_LIB(com_err, main,[LIBS="-lcom_err $LIBS"],,$LIBS)
dnl AC_CHECK_LIB(k5crypto, main,[LIBS="-lk5crypto $LIBS"],,$LIBS)
dnl AC_CHECK_LIB(krb5, main,[LIBS="-lkrb5 $LIBS"],,$LIBS)
dnl AC_CHECK_LIB(krb4, main,[LIBS="-lkrb4 $LIBS"],,$LIBS)
dnl AC_CHECK_LIB(sasl, sasl_client_init,[LIBS="-lsasl $LIBS"],,$LIBS)
if test -z "$found_ldap_lib" -a \( $with_ldap_lib = auto -o $with_ldap_lib = umich -o $with_ldap_lib = openldap \); then
  AC_CHECK_LIB(lber, main)
  AC_CHECK_LIB(ldap, main, [LIBS="-lldap $LIBS" found_ldap_lib=yes],,$LIBS)
fi
if test -z "$found_ldap_lib" -a \( $with_ldap_lib = auto -o $with_ldap_lib = netscape5 \); then
AC_CHECK_LIB(ldap50, main, LIBS="-lldap50 -lssldap50 -lssl3 -lnss3 -lnspr4 -lprldap50 -lplc4 -lplds4 $LIBS" found_ldap_lib=yes need_pthread=yes,, -lpthread)
fi
if test -z "$found_ldap_lib" -a \( $with_ldap_lib = auto -o $with_ldap_lib = netscape4 \); then
  AC_CHECK_LIB(ldapssl41, main, LIBS="-lldapssl41 -lplc3 -lplds3 -lnspr3 $LIBS" found_ldap_lib=yes need_pthread=yes,, -lpthread)
  if test -z "$found_ldap_lib"; then
    AC_CHECK_LIB(ldapssl40, main, LIBS="-lldapssl40 $LIBS" found_ldap_lib=yes need_pthread=yes,, -lpthread)
  fi
fi
if test -z "$found_ldap_lib" -a \( $with_ldap_lib = auto -o $with_ldap_lib = netscape3 \); then
  AC_CHECK_LIB(ldapssl30, main, LIBS="-lldapssl30 $LIBS" found_ldap_lib=yes need_pthread=yes,, -lpthread)
fi

if test -z "$found_ldap_lib"; then
  AC_MSG_ERROR(could not locate a valid LDAP library)
fi

if test "$need_pthread" = "yes"; then
  AC_CHECK_LIB(pthread, main)
fi

AC_CHECK_FUNCS(ldap_init ldap_get_lderrno ldap_parse_result ldap_memfree ldap_controls_free ldap_ld_free ldap_explode_rdn ldap_set_option ldap_get_option ldap_sasl_interactive_bind_s)
if test "$enable_ssl" \!= "no"; then
  AC_CHECK_FUNCS(ldapssl_client_init ldap_start_tls_s ldap_pvt_tls_set_option)
fi
AC_CHECK_FUNCS(ldap_initialize)
AC_CHECK_FUNCS(gethostbyname_r)

if test "$ac_cv_func_gethostbyname_r" = "yes"; then
AC_CACHE_CHECK(whether gethostbyname_r takes 6 arguments, nss_ldap_cv_gethostbyname_r_args, [
AC_TRY_COMPILE([
#include <netdb.h>], [gethostbyname_r(0, 0, 0, 0, 0, 0);], [nss_ldap_cv_gethostbyname_r_args=6], [nss_ldap_cv_gethostbyname_r_args=5]) ])
AC_DEFINE_UNQUOTED(GETHOSTBYNAME_R_ARGS, $nss_ldap_cv_gethostbyname_r_args)
fi

AC_CHECK_FUNCS(ldap_set_rebind_proc)
AC_CACHE_CHECK(whether ldap_set_rebind_proc takes 3 arguments, nss_ldap_cv_ldap_set_rebind_proc, [
AC_TRY_COMPILE([
#include <lber.h>
#include <ldap.h>], [ldap_set_rebind_proc(0, 0, 0);], [nss_ldap_cv_ldap_set_rebind_proc=3], [nss_ldap_cv_ldap_set_rebind_proc=2]) ])
AC_DEFINE_UNQUOTED(LDAP_SET_REBIND_PROC_ARGS, $nss_ldap_cv_ldap_set_rebind_proc)

AC_OUTPUT(Makefile)
