AC_MSG_CHECKING(whether to enable GSSAPI support)
AC_ARG_WITH(gssapi,
[  --with-gssapi=PATH      Set path for GSSAPI files [default=check]],
[ case "$withval" in
  yes)
    with_gssapi=CHECK
    ;;
  esac ],
[ with_gssapi=CHECK ]
)dnl

if test "x$with_gssapi" = "xCHECK" ; then
  with_gssapi=NOTFOUND
  KDE_FIND_PATH(krb5-config, KRB5_CONFIG, [${prefix}/bin ${exec_prefix}/bin /usr/bin /usr/local/bin /opt/local/bin /usr/lib/mit/bin], [
    AC_MSG_WARN([Could not find krb5-config])
  ])

  if test -n "$KRB5_CONFIG"; then
    kde_save_cflags="$CFLAGS"
    unset CFLAGS
    GSSAPI_INCS="`$KRB5_CONFIG --cflags gssapi`"
    GSSAPI_LIBS="`$KRB5_CONFIG --libs gssapi`"
    CFLAGS="$kde_save_cflags"
    if test "$USE_RPATH" = yes; then
      for args in $GSSAPI_LIBS; do
        case $args in
          -L/usr/lib) ;;
          -L*)
             GSSAPI_RPATH="$GSSAPI_RPATH $args"
             ;;
        esac
      done
      GSSAPI_RPATH=`echo $GSSAPI_RPATH | sed -e "s/-L/-R/g"`
    fi
    gssapi_incdir="$GSSAPI_INCS"
    gssapi_libdir="$GSSAPI_LIBS"
    with_gssapi=FOUND
    if $KRB5_CONFIG --vendor | grep "Massachusetts" > /dev/null; then
      gssapi_flavor=MIT
    else
      gssapi_flavor=HEIMDAL
    fi
  else
    search_incs="$kde_includes /usr/include /usr/local/include"
    AC_FIND_FILE(gssapi.h, $search_incs, gssapi_incdir)
    if test -r $gssapi_incdir/gssapi.h ; then
      test "x$gssapi_incdir" != "x/usr/include" && GSSAPI_INCS="-I$gssapi_incdir"
      with_gssapi=FOUND
    fi
    if test $with_gssapi = FOUND ; then
      with_gssapi=NOTFOUND
      for ext in la so sl a dylib ; do
        AC_FIND_FILE(libgssapi.$ext, $kde_libraries /usr/lib /usr/local/lib,
          gssapi_libdir)
        if test -r $gssapi_libdir/libgssapi.$ext ; then
          if test "x$gssapi_libdir" != "x/usr/lib" ; then
            GSSAPI_LIBS="-L$gssapi_libdir "
            test "$USE_RPATH" = yes && GSSAPI_RPATH="-R $gssapi_libdir"
          fi
          GSSAPI_LIBS="${GSSAPI_LIBS} -lgssapi -lkrb5 -lasn1 -lcrypto -lroken -lcrypt ${LIBRESOLV}"
          with_gssapi=FOUND
          gssapi_flavor=HEIMDAL
          break
        fi
      done
    fi
  fi
fi

case "$with_gssapi" in
no) AC_MSG_RESULT(no) ;;
framework)
  GSSAPI_LIBS="-Xlinker -framework -Xlinker Kerberos"
  AC_DEFINE_UNQUOTED(HAVE_LIBGSSAPI, 1, [Define if you have GSSAPI libraries])
  GSSAPI_SUBDIR="gssapi"
  AC_MSG_RESULT(Apple framework)
  ;;
NOTFOUND) AC_MSG_RESULT(searched but not found) ;;
*)
  if test "x$with_gssapi" = "xFOUND" ; then
    msg="incs=$gssapi_incdir libs=$gssapi_libdir"
  else
    msg="$with_gssapi"
    GSSAPI_ROOT="$with_gssapi"
    if test "x$GSSAPI_ROOT" != "x/usr" ; then
      GSSAPI_INCS="-I${GSSAPI_ROOT}/include"
      GSSAPI_LIBS="-L${GSSAPI_ROOT}/lib "
      if test "$USE_RPATH" = "yes" ; then
        GSSAPI_RPATH="-R ${GSSAPI_ROOT}/lib"
      fi
    fi
    if test -f ${GSSAPI_ROOT}/include/gssapi/gssapi.h ; then
      gssapi_flavor=MIT
      GSSAPI_LIBS="${GSSAPI_LIBS}-lgssapi_krb5 -lkrb5 -lk5crypto -lcom_err ${LIBRESOLV}"
    else
      gssapi_flavor=HEIMDAL
      GSSAPI_LIBS="${GSSAPI_LIBS}-lgssapi -lkrb5 -lasn1 -lcrypto -lroken -lcrypt ${LIBRESOLV}"
    fi
  fi
  if test "x$gssapi_flavor" = "xMIT" ; then
    AC_DEFINE_UNQUOTED(GSSAPI_MIT, 1, [Define if you have the MIT Kerberos libraries])
  fi
  AC_DEFINE_UNQUOTED(HAVE_LIBGSSAPI, 1, [Define if you have GSSAPI libraries])
  AC_MSG_RESULT($msg)
  ;;
esac

AC_SUBST(GSSAPI_INCS)
AC_SUBST(GSSAPI_LIBS)
AC_SUBST(GSSAPI_RPATH)
