KDE_FIND_PATH(xmkmf, XMKMF, [], [AC_MSG_ERROR([xmkmf/imake not found. Please make sure it's in PATH!])])

dnl ask imake about various X settings
AC_MSG_CHECKING([X paths])
imkv=8
test "$kde_cv_defines_imake_version" = $imkv || unset kde_cv_defines_imake
AC_CACHE_VAL(kde_cv_defines_imake, [
  rm -fr conftestdir
  if mkdir conftestdir; then
    cd conftestdir
    cat > Imakefile <<'EOF'[

acimake:
	@echo "XBINDIR=\"/usr/bin\" XLIBDIR=\"$(LIBDIR)\""

]EOF
    if imake -I/usr/lib/X11/config -DTOPDIR=/etc/X11 -DCURDIR=. /etc/X11 >&5 2>&1 && test -f Makefile; then
	kde_cv_defines_imake=`${MAKE-make} acimake 2> /dev/null | grep -v "^make"`
	kde_cv_defines_imake_version=$imkv
    else
        AC_MSG_RESULT([failed])
	AC_MSG_ERROR([$XMKMF (imake) failed.
Make sure you have all necessary X development packages installed.
On some systems a missing /lib/cpp symlink is at fault.])
    fi
    cd ..
    rm -fr conftestdir
  else
    AC_MSG_RESULT([failed])
    AC_MSG_ERROR([cannot create temporary directory])
  fi
])
AC_MSG_RESULT([done])
eval "$kde_cv_defines_imake"
AC_DEFINE_UNQUOTED(XBINDIR, "$XBINDIR", [X binaries directory])
AC_DEFINE_UNQUOTED(XLIBDIR, "$XLIBDIR", [X libraries directory])

if test -f /etc/ttys; then
  AC_DEFINE(BSD_INIT, 1, [Define if the system uses a BSD-style init])
fi

AC_CHECK_FUNCS([getttyent])
case $host_os in
	linux*) ac_cv_func_getutxent=no;;
	darwin*) ac_cv_func_getutxent=no;;
	kfreebsd*-gnu) ac_cv_func_getutxent=no;;
	*) AC_CHECK_FUNC([getutxent]);;
esac
if test $ac_cv_func_getutxent = yes; then
	AC_DEFINE(HAVE_UTMPX, 1, [Define if the system uses extended utmp])
else
	AC_CHECK_FUNC([getutent], ,
		[AC_DEFINE(BSD_UTMP, 1, [Define if the system has no getutent])])
fi

AC_CHECK_MEMBERS([struct utmp.ut_user], , , [#include <utmp.h>])
AC_CHECK_MEMBERS([struct passwd.pw_expire], , , [#include <pwd.h>])
AC_CHECK_MEMBERS([struct sockaddr_in.sin_len], , , [
#include <sys/socket.h>
#include <netinet/in.h>
])

ac_save_libs=$LIBS
LIBS="$LIBS $LIBUTIL"
AC_CHECK_FUNCS([setlogin setusercontext getusershell login_getclass auth_timeok])
LIBS=$ac_save_libs

dnl is getifaddrs always available without additional libs?
AC_CHECK_FUNCS([mkstemp setproctitle sysinfo strnlen getifaddrs])

AC_CHECK_FUNCS([arc4random], ,
	[
dnl assume that /dev/random is non-blocking if /dev/urandom does not exist
for i in urandom random; do
  if test -c /dev/$i; then
    AC_DEFINE_UNQUOTED(DEV_RANDOM, "/dev/$i", [Define the system's entropy device])
    break
  fi
done
	])

AC_CHECK_FUNC(vsyslog, [
  AC_DEFINE(USE_SYSLOG, 1, [Define if tdm should be built with syslog support])])

tdm_no_Xau=false
tdm_no_Xdmcp=false

AC_CHECK_LIB(Xau, main, [:], 
	[
	  tdm_no_Xau=true
	  DO_NOT_COMPILE="$DO_NOT_COMPILE tdm"
	], 
	  $X_LDFLAGS -lX11 $LIBSOCKET)

AC_ARG_WITH(xdmcp,
        AC_HELP_STRING([--without-xdmcp],[build tdm without xdmcp support [default=with xdmcp]]), ,
	[with_xdmcp=yes])
if test "x$with_xdmcp" = xyes; then
  AC_CHECK_LIB(Xdmcp, main, [LIBXDMCP="-lXdmcp"], , $X_LDFLAGS -lX11 $LIBSOCKET)
  if test -n "$LIBXDMCP"; then
    cppflags_safe=$CPPFLAGS
    CPPFLAGS="$CPPFLAGS $X_INCLUDES"
    AC_CHECK_HEADER(X11/Xdmcp.h, [HAVE_X11_XDMCP_H=1], , [#include <X11/Xmd.h>])
    CPPFLAGS=$cppflags_safe
  fi
  if test -z "$HAVE_X11_XDMCP_H"; then
    tdm_no_Xdmcp=true
    DO_NOT_COMPILE="$DO_NOT_COMPILE tdm"
  fi
  AC_DEFINE(XDMCP, 1, [Define if tdm should be built with XDMCP support])
  ac_save_libs=$LIBS
  LIBS="$LIBS $LIBXDMCP"
  AC_CHECK_FUNC(XdmcpWrap, [
    AC_DEFINE(HASXDMAUTH, 1, [Define if tdm should be built with XDMAUTH support])
  ])
  LIBS=$ac_save_libs
fi
AC_SUBST(LIBXDMCP)

KRB4_INCS=
KRB4_LIBS=
KRB4_RPATH=

AC_MSG_CHECKING(whether to use Kerberos v4)
AC_ARG_WITH(krb4,
AC_HELP_STRING([--with-krb4=PATH],[Compile in Kerberos v4 support]),
[ test "x$with_krb4" = xyes && with_krb4=/usr/kerberos ],
[ with_krb4=no ]
)
case "$with_krb4" in
no)
  AC_MSG_RESULT(no)
  ;;
*)
  AC_MSG_RESULT(yes)
  AC_DEFINE_UNQUOTED(KRB4, 1, [define if you have Kerberos IV])
  KRB4_INCS="-I$with_krb4/include"
  KRB4_LIBS="-L$with_krb4/lib -lkrb -ldes"
  if test "$USE_RPATH" = "yes" ; then
    KRB4_RPATH="-R $with_krb4/lib"
  fi
  AC_CHECK_LIB(resolv, dn_expand, KRB4_LIBS="$KRB4_LIBS -lresolv")
  ;;
esac

AC_MSG_CHECKING(whether to use AFS)
AC_ARG_WITH(afs,
  AC_HELP_STRING([--with-afs],[Compile in AFS support (requires KTH krb4)]), , 
  [ with_afs=no ])
if test "$with_afs" = no; then
  AC_MSG_RESULT(no)
else
  if test "$with_krb4" = no; then
    AC_MSG_RESULT(no)
    AC_MSG_WARN("AFS requires Kerberos v4 support.")
    with_afs=no
  else
    AC_MSG_RESULT(yes)
    AC_DEFINE_UNQUOTED(AFS, 1, [define if you have KTH Kerberos IV and AFS])
    KRB4_LIBS="$KRB4_LIBS -lkafs"
    if test -n "$os_aix"; then
      KRB4_LIBS="$KRB4_LIBS -lld"
    fi
  fi
fi

AC_SUBST(KRB4_INCS)
AC_SUBST(KRB4_LIBS)
AC_SUBST(KRB4_RPATH)

AC_CHECK_LIB(s, main, [LIB_LIBS="-ls"]) dnl for AIX
AC_SUBST(LIB_LIBS)

AC_CHECK_LIB(posix4, sched_yield, [LIBPOSIX4=-lposix4])
AC_SUBST(LIBPOSIX4)

KRB5_INCS=
KRB5_LIBS=
KRB5_RPATH=

AC_MSG_CHECKING([whether to use Kerberos5 for Xauth cookies in tdm])
AC_ARG_WITH(krb5auth,
  AC_HELP_STRING([--with-krb5auth=PATH],[Use Kerberos5 for Xauth cookies in tdm]), ,
  [ with_krb5auth=no ])
if test "x$with_krb5auth" = xno; then
  AC_MSG_RESULT(no)
else
  AC_MSG_RESULT(yes)
  if test "x$with_krb5auth" != xyes; then
    KRB5_INCS="-I$with_krb5auth/include"
    KRB5_LIBS="-L$with_krb5auth/lib"
    if test "$USE_RPATH" = "yes" ; then
      KRB5_RPATH="-R $with_krb5auth/lib"
    fi
  fi
  KRB5_LIBS="$KRB5_LIBS -lkrb5" dnl -lk5crypto -lcom_err -lresolv
  keepcflags=$CFLAGS
  CFLAGS="$KRB5_INCS $CFLAGS"
  AC_CHECK_HEADER(krb5/krb5.h, 
    [ AC_DEFINE(K5AUTH, 1, [Define if tdm should use Kerberos 5 for Xauth cookies.]) ],
    [ AC_MSG_ERROR([--with-krb5auth requires Kerberos5 header files.
Due to a problem with X includes you probably have to run "ln -s . krb5"
in the directory where the krb5.h include resides to make things actually work.])])
  CFLAGS="$keepcflags"
fi

AC_SUBST(KRB5_INCS)
AC_SUBST(KRB5_LIBS)
AC_SUBST(KRB5_RPATH)

AC_MSG_CHECKING([whether to use Sun's secure RPC for Xauth cookies in tdm])
AC_ARG_WITH(rpcauth,
  AC_HELP_STRING([--with-rpcauth],[Use Sun's secure RPC for Xauth cookies in tdm.]), , 
  [ with_rpcauth=no ])
if test "x$with_rpcauth" = xno; then
  AC_MSG_RESULT(no)
else
  AC_MSG_RESULT(yes)
  AC_CHECK_HEADER(rpc/rpc.h, 
    [ AC_DEFINE(SECURE_RPC, 1, [Define if tdm should use Sun's secure RPC for Xauth cookies.]) ],
    [ AC_MSG_ERROR([--with-rpcauth requires Sun RPC header files.])])
fi

if test "x$use_pam" = xyes; then
    AC_DEFINE(USE_PAM, 1, [Define if tdm should use PAM])
elif test "x$use_shadow" = xyes; then
    AC_DEFINE(USESHADOW, 1, [Define if tdm should use shadow passwords])
fi
if test "x$with_krb4" != xno; then
    AC_DEFINE(KERBEROS, 1, [Define if tdm should use Kerberos IV])
    if test "x$with_afs" = xno; then
        AC_DEFINE(NO_AFS, 1, [Define if tdm should not use AFS])
    fi
fi

AC_ARG_WITH(tdm-xconsole,
	AC_HELP_STRING([--with-tdm-xconsole],[build tdm with built-in xconsole [default=no]]), ,
	[with_tdm_xconsole=no])
if test "x$with_tdm_xconsole" = xyes; then
    AC_DEFINE(WITH_TDM_XCONSOLE, 1, [Build tdm with built-in xconsole])
fi

########### Check for DBus

  AC_MSG_CHECKING(for DBus)

  dbus_inc=NOTFOUND
  dbus_lib=NOTFOUND
  dbus=NOTFOUND

  search_incs="$kde_includes $kde_extra_includes /usr/include /usr/include/dbus-1.0 /usr/local/include /usr/local/include/dbus-1.0"
  AC_FIND_FILE(dbus/dbus.h, $search_incs, dbus_incdir)

  search_incs_arch_deps="$kde_includes $kde_extra_includes /usr/lib$tdelibsuff/dbus-1.0/include /usr/local/lib$tdelibsuff/dbus-1.0/include"
  AC_FIND_FILE(dbus/dbus-arch-deps.h, $search_incs_arch_deps, dbus_incdir_arch_deps)

  if test -r $dbus_incdir/dbus/dbus.h && test -r $dbus_incdir_arch_deps/dbus/dbus-arch-deps.h ; then
    DBUS_INCS="-I$dbus_incdir -I$dbus_incdir_arch_deps"
    dbus_inc=FOUND
  fi

  search_libs="$kde_libraries $kde_extra_libs /usr/lib$tdelibsuff /usr/local/lib$tdelibsuff"
  AC_FIND_FILE(libdbus-1.so, $search_libs, dbus_libdir)

  if test -r $dbus_libdir/libdbus-1.so ; then
    DBUS_LIBS="-L$dbus_libdir -ldbus-1"
    dbus_lib=FOUND
  fi

  if test $dbus_inc != FOUND || test $dbus_lib != FOUND ; then 
    KDE_PKG_CHECK_MODULES( DBUS, "dbus-1", [ DBUS_INCS=$DBUS_CFLAGS; dbus_inc=FOUND; dbus_lib=FOUND; ] , AC_MSG_RESULT( Nothing found on PKG_CONFIG_PATH ) )
  fi

  dbus_bus_var=`pkg-config --variable=system_bus_default_address dbus-1 2>/dev/null`
  if test -z "$dbus_bus_var"; then
        dbus_bus_var="unix:path=/var/run/dbus/system_bus_socket"
  fi
  AC_DEFINE_UNQUOTED(DBUS_SYSTEM_BUS, "$dbus_bus_var", [Define the unix domain path for dbus system bus])

  if test $dbus_inc = FOUND && test $dbus_lib = FOUND ; then
    AC_MSG_RESULT(headers $DBUS_INCS libraries $DBUS_LIBS)
    dbus=FOUND
  else
    AC_MSG_RESULT(searched but not found)
  fi

  AC_SUBST(DBUS_INCS)
  AC_SUBST(DBUS_LIBS)

########### Check for DBus

  AC_MSG_CHECKING(for DBus)

  dbus_inc=NOTFOUND
  dbus_lib=NOTFOUND
  dbus=NOTFOUND

  search_incs="$kde_includes $kde_extra_includes /usr/include /usr/include/dbus-1.0 /usr/local/include /usr/local/include/dbus-1.0"
  AC_FIND_FILE(dbus/dbus.h, $search_incs, dbus_incdir)

  search_incs_arch_deps="$kde_includes $kde_extra_includes /usr/lib$tdelibsuff/dbus-1.0/include /usr/local/lib$tdelibsuff/dbus-1.0/include"
  AC_FIND_FILE(dbus/dbus-arch-deps.h, $search_incs_arch_deps, dbus_incdir_arch_deps)

  if test -r $dbus_incdir/dbus/dbus.h && test -r $dbus_incdir_arch_deps/dbus/dbus-arch-deps.h ; then
    DBUS_INCS="-I$dbus_incdir -I$dbus_incdir_arch_deps"
    dbus_inc=FOUND
  fi

  search_libs="$kde_libraries $kde_extra_libs /usr/lib$tdelibsuff /usr/local/lib$tdelibsuff"
  AC_FIND_FILE(libdbus-1.so, $search_libs, dbus_libdir)

  if test -r $dbus_libdir/libdbus-1.so ; then
    DBUS_LIBS="-L$dbus_libdir -ldbus-1"
    dbus_lib=FOUND
  fi

  if test $dbus_inc != FOUND || test $dbus_lib != FOUND ; then 
    KDE_PKG_CHECK_MODULES( DBUS, "dbus-1", [ DBUS_INCS=$DBUS_CFLAGS; dbus_inc=FOUND; dbus_lib=FOUND; ] , AC_MSG_RESULT( Nothing found on PKG_CONFIG_PATH ) )
  fi

  dbus_bus_var=`pkg-config --variable=system_bus_default_address dbus-1 2>/dev/null`
  if test -z "$dbus_bus_var"; then
        dbus_bus_var="unix:path=/var/run/dbus/system_bus_socket"
  fi
  AC_DEFINE_UNQUOTED(DBUS_SYSTEM_BUS, "$dbus_bus_var", [Define the unix domain path for dbus system bus])

  if test $dbus_inc = FOUND && test $dbus_lib = FOUND ; then
    AC_MSG_RESULT(headers $DBUS_INCS libraries $DBUS_LIBS)
    dbus=FOUND
  else
    AC_MSG_RESULT(searched but not found)
  fi

  AC_SUBST(DBUS_INCS)
  AC_SUBST(DBUS_LIBS)

dnl AC_OUTPUT(tdm/kfrontend/sessions/kde.desktop)


AC_ARG_WITH(libaudit,
  [  --with-libaudit=[auto/yes/no]  Add Linux audit support [default=auto]],,
  with_libaudit=auto)

# Check for Linux auditing API
#
# libaudit detection
if test x$with_libaudit = xno ; then
    have_libaudit=no;
else
    # See if we have audit daemon library
    AC_CHECK_LIB(audit, audit_log_user_message,
                 have_libaudit=yes, have_libaudit=no)
fi

AM_CONDITIONAL(HAVE_LIBAUDIT, test x$have_libaudit = xyes)

if test x$have_libaudit = xyes ; then
    EXTRA_DAEMON_LIBS="$EXTRA_DAEMON_LIBS -laudit"
    AC_DEFINE(HAVE_LIBAUDIT,1,[linux audit support])
fi

