AC_MSG_CHECKING(for LDAP support)
AC_ARG_WITH(ldap,
AC_HELP_STRING([--with-ldap=PATH],[Set path for LDAP files [default=check]]),
[ case "$withval" in
  yes)
    with_ldap=CHECK
    ;;
  esac ],
[ with_ldap=CHECK ]
)dnl

if test "x$with_ldap" = "xCHECK" ; then
  with_ldap=NOTFOUND
  search_incs="$kde_includes /usr/include /usr/local/include"
  AC_FIND_FILE(ldap.h, $search_incs, ldap_incdir)
  if test -r $ldap_incdir/ldap.h ; then
    test "x$ldap_incdir" != "x/usr/include" && LDAP_INCS="-I$ldap_incdir"
    with_ldap=FOUND
  fi
  if test $with_ldap = FOUND ; then
    with_ldap=NOTFOUND
    for ext in la so sl a dylib ; do
      AC_FIND_FILE(libldap.$ext, $kde_libraries /usr/lib /usr/local/lib /usr/lib64,
        ldap_libdir)
      if test -r $ldap_libdir/libldap.$ext ; then
        if test "x$ldap_libdir" != "x/usr/lib" ; then
          LDAP_LIBS="-L$ldap_libdir "
          test "$USE_RPATH" = yes && LDAP_RPATH="-R $ldap_libdir"
        fi
        LDAP_LIBS="${LDAP_LIBS}-lldap"
        with_ldap=FOUND
        break
      fi
    done
  fi
fi

case "$with_ldap" in
no) AC_MSG_RESULT(no) ;;
framework)
  LDAP_LIBS="-Xlinker -framework -Xlinker LDAP"
  AC_DEFINE_UNQUOTED(HAVE_LIBLDAP, 1, [Define if you have LDAP libraries])
  LDAP_SUBDIR="ldap"
  AC_MSG_RESULT(Apple framework)
  ;;
FOUND)
  AC_MSG_RESULT(incs=$ldap_incdir libs=$ldap_libdir)
  ;;
NOTFOUND) AC_MSG_RESULT(searched but not found) ;;
*)
  AC_MSG_RESULT($with_ldap)
  ;;
esac

LIB_LBER=
KDE_CHECK_LIB(lber, ber_alloc, [LIB_LBER=-llber], [], -L$ldap_libdir)
AC_SUBST(LIB_LBER)

AC_MSG_CHECKING(whether LDAP support can be compiled)

  if test "x$with_ldap" != "xFOUND" ; then
    LDAP_ROOT="$with_ldap"
    if test "x$LDAP_ROOT" != "x/usr" ; then
      LDAP_INCS="-I${LDAP_ROOT}/include"
      LDAP_LIBS="-L${LDAP_ROOT}/lib "
      if test "$USE_RPATH" = "yes" ; then
        LDAP_RPATH="-R ${LDAP_ROOT}/lib"
      fi
    fi
    LDAP_LIBS="${LDAP_LIBS}-lldap"
  fi
  LDAP_LIBS="${LDAP_LIBS} ${LIB_LBER} ${LIBRESOLV}"

  kde_safe_LIBS="$LIBS"
  kde_safe_CFLAGS="$CFLAGS"
  LIBS="$LIBS $all_libraries $LDAP_LIBS $KRB4_LIBS $X_EXTRA_LIBS"
  CFLAGS="$CFLAGS $all_includes $LDAP_INCS $KRB4_INCS"
  AC_LANG_SAVE
  AC_LANG_C
  AC_TRY_LINK(dnl
  [
  #include <ldap.h>
  #if LDAP_API_VERSION < 2004
  #error LDAP version too old, please upgrade to a library supporting API 2004 or higher
  #endif
  ],
  [
  LDAP *ldap;
  ],
  , with_ldap=no
  )
  AC_LANG_RESTORE
  CFLAGS=$kde_safe_CFLAGS
  LIBS=$kde_safe_LIBS
  if test "$with_ldap" = "no" ; then
    LDAP_INCS=
    LDAP_LIBS=
    LDAP_RPATH=
    LDAP_SUBDIR=
    AC_MSG_RESULT(no (but first try gave $msg))
  else
    AC_DEFINE_UNQUOTED(HAVE_LIBLDAP, 1, [Define if you have LDAP libraries])
    LDAP_SUBDIR="ldap"
    AC_MSG_RESULT(yes)
  fi

AC_SUBST(LDAP_INCS)
AC_SUBST(LDAP_LIBS)
AC_SUBST(LDAP_RPATH)

AM_CONDITIONAL(include_kioslave_ldap, test -n "$LDAP_SUBDIR")
