dnl Compile in the exec prefix to help kstddirs in finding dynamic libs
AC_DEFINE_UNQUOTED(__KDE_EXECPREFIX, "$exec_prefix", [execprefix or NONE if not set, for libloading])

dnl --------
dnl KSocketAddress/KExtendedSocket extra configuration
dnl --------
dnl
dnl This is to be merged with toplevel configure.in.in soon

kde_safe_LIBS="$LIBS"
LIBS="$LIBS $all_libraries $X_EXTRA_LIBS"
AC_CHECK_FUNCS(inet_ntop inet_pton getpeername getsockname getsockopt gethostbyname2_r gethostbyname_r gethostbyname2)
LIBS="$kde_safe_LIBS"

AC_MSG_CHECKING([for struct addrinfo])
AC_TRY_COMPILE(dnl
      [
        #include <netdb.h>
      ],
      [
        struct addrinfo ai;
      ],
      [
        AC_DEFINE(HAVE_STRUCT_ADDRINFO, 1, [Define if netdb.h defines struct addrinfo])
	AC_MSG_RESULT(yes)
      ],
      AC_MSG_RESULT(no)
)
AC_MSG_CHECKING([for GLIBC function backtrace])
AC_TRY_LINK(dnl
      [
        #include <execinfo.h>
      ],
      [
        void* trace[256];
        backtrace(trace, 256);
      ],
      [
        AC_DEFINE(HAVE_BACKTRACE, 1, [Define if execinfo.h exists and defines backtrace (GLIBC >= 2.1)])
	AC_MSG_RESULT(yes)
      ],
      AC_MSG_RESULT(no)
)

my_LIBS="$LIBS"
LIBS="$LIBS $LIBSOCKET"
AC_CHECK_FUNC(getaddrinfo,
  [
    AC_DEFINE(HAVE_GETADDRINFO, 1, [Define if getaddrinfo is present])

    dnl Even though we now know that getaddrinfo is there, make sure getnameinfo is there too
    kde_gai_ok=true
    AC_CHECK_FUNCS(freeaddrinfo getnameinfo gai_strerror, : , 
    [
      kde_gai_ok=false
      AC_DEFINE(HAVE_BROKEN_GETADDRINFO, 1, [Define if getaddrinfo is broken and should be replaced])
      AC_DEFINE(GETADDRINFO_RETURNS_UNIX, 1, [Define if getaddrinfo returns AF_UNIX sockets])
      break
    ])

    AC_MSG_CHECKING([if getaddrinfo works using numeric service with null host])
    dnl On AIX (4.3), getaddrinfo returns NULL if the hint
    dnl is AF_INET/SOCK_STREAM/AI_PASSIVE.
    dnl The error code reports "Host not found".
    dnl It only seems to return non-NULL if the port is known (eg. in
    dnl /etc/services).
    AC_TRY_RUN(dnl
    [
      #include <sys/types.h>
      #include <sys/socket.h>
      #include <netdb.h>
      #include <string.h>
      int main()
      {
        struct addrinfo hint, *res;
        int err;
        memset(&hint, 0, sizeof(hint));
        hint.ai_family = AF_INET;
        hint.ai_protocol = 0;
        hint.ai_socktype = SOCK_STREAM;
        hint.ai_flags = AI_PASSIVE;
        err = getaddrinfo(0, "18300", &hint, &res); /* kxmlrpc tries this */
        if (err != 0 || res == 0 || res->ai_family != AF_INET)
          return 1;
        return 0;
      }
    ],
    [
      AC_MSG_RESULT(yes)
    ],
    [
      AC_MSG_RESULT(no)
      AC_DEFINE(HAVE_BROKEN_GETADDRINFO, 1, [Define if getaddrinfo is broken and should be replaced])
    ],
    [
      AC_MSG_RESULT(cross compiling. We hope so)
    ])

    if $kde_gai_ok ; then
      AC_MSG_CHECKING([if getaddrinfo returns AF_UNIX])
      dnl At least one system has a bad implementation of getaddrinfo
      dnl FreeBSD's libc getaddrinfo code has AF_UNIX disabled
      dnl This is rather stupid, but since it's there...
      AC_TRY_RUN(dnl
      [
        #include <sys/types.h>
        #include <sys/socket.h>
        #include <netdb.h>
        #include <string.h>
	int main()
	{
	  struct addrinfo hint, *res;
	  int err;
	  memset(&hint, 0, sizeof(hint));
	  hint.ai_family = AF_UNSPEC;
	  hint.ai_protocol = 0;
	  hint.ai_socktype = SOCK_STREAM;
	  hint.ai_flags = 0;
	  err = getaddrinfo(0, "/tmp/conftest-sock", &hint, &res);
	  if (err != 0 || res == 0 || res->ai_family != AF_UNIX)
	    return 1;
	  return 0;
	}
      ],
      [
        AC_MSG_RESULT(yes)
	AC_DEFINE(GETADDRINFO_RETURNS_UNIX, 1, [Define if getaddrinfo returns AF_UNIX sockets])
      ],
      [
        AC_MSG_RESULT(no)
      ],
      [
        case "$target" in
	  *-*-freebsd*)
	    AC_MSG_RESULT(assuming it doesn't)
	    ;;
	  *)
	    AC_MSG_RESULT(assuming it does)
	    AC_DEFINE(GETADDRINFO_RETURNS_UNIX, 1, [Define if getaddrinfo returns AF_UNIX sockets])
	    ;;
	esac
      ]
      )
    fi		# test $kde_gai_broken
  ],
  [
    dnl Our getaddrinfo returns AF_UNIX sockets
    AC_DEFINE(GETADDRINFO_RETURNS_UNIX, 1, [Define if getaddrinfo returns AF_UNIX sockets])
  ]
)
case "$target" in
  *-*-openbsd2.*|*-*-openbsd3.[0-2])
    dnl OpenBSD's getaddrinfo is not thread-safe up to 3.2
    AC_DEFINE(HAVE_BROKEN_GETADDRINFO, 1, [Define if getaddrinfo is broken and should be replaced])
    ;;
esac
LIBS="$my_LIBS"

AC_MSG_CHECKING([if struct sockaddr has member sa_len])
AC_TRY_COMPILE(
  [
    #include <sys/types.h>
    #include <sys/socket.h>
  ],
  [
    struct sockaddr sa;
    sa.sa_len = sizeof(sa);
  ],
  [
    AC_DEFINE(HAVE_SOCKADDR_SA_LEN,1,[Define if struct sockaddr has member sa_len])
    AC_MSG_RESULT(yes)
  ],
  AC_MSG_RESULT(no)
)

AC_CHECK_HEADERS(netinet/in.h,
  [

    dnl Check for sockaddr_in6. We don't have to check for in6_addr
    dnl if sockaddr_in6 is there, so is in6_addr
    AC_MSG_CHECKING([for struct sockaddr_in6])
    AC_TRY_COMPILE(
      [
	#include <sys/types.h>
	#include <stdlib.h>
	#include <netinet/in.h>
      ],
      [
	struct sockaddr_in6 sin6;
      ],
      [
	AC_DEFINE(HAVE_SOCKADDR_IN6,1,[Define if we have struct sockaddr_in6 in netinet/in.h])
	AC_MSG_RESULT(yes)

	dnl Check if this system's sockaddr_in6 already supports sin6_scope_id
	dnl All systems should support it, but some still don't
	AC_MSG_CHECKING([if struct sockaddr_in6 has member sin6_scope_id])
	AC_TRY_COMPILE(
	  [
	    #include <sys/types.h>
	    #include <stdlib.h>
	    #include <netinet/in.h>
	  ],
	  [
	    struct sockaddr_in6 sin6;
	    sin6.sin6_scope_id = 0;
	  ],
	  [
	    AC_DEFINE(HAVE_SOCKADDR_IN6_SCOPE_ID,1,[Define if this system already has sin6_scope_id in sockaddr_in6])
	    AC_MSG_RESULT(yes)
	  ],
	  AC_MSG_RESULT(no)
	)
      ],
      AC_MSG_RESULT(no)
    )
  ]
)

AC_ARG_WITH(ipv6-lookup, 
[  --with-ipv6-lookup=mode Set the mode for IPv6 lookups: yes, auto and no],
[
  AC_MSG_CHECKING([how we should treat IPv6 lookups])
  if test "$withval" = yes -o "$withval" = always; then
    AC_DEFINE(KDE_IPV6_LOOKUP_MODE, 0, [Lookup mode for IPv6 addresses: 0 for always, 1 for check and 2 for disabled])
    AC_MSG_RESULT([always do the lookup])
  elif test "$withval" = auto; then
    AC_DEFINE(KDE_IPV6_LOOKUP_MODE, 1, [Lookup mode for IPv6 addresses: 0 for always, 1 for check and 2 for disabled])
    AC_MSG_RESULT([check for IPv6 stack])
  else
    AC_DEFINE(KDE_IPV6_LOOKUP_MODE, 2, [Lookup mode for IPv6 addresses: 0 for always, 1 for check and 2 for disabled])
    AC_MSG_RESULT([never do the lookup])
  fi
], [    
  AC_DEFINE(KDE_IPV6_LOOKUP_MODE, 1, [Lookup mode for IPv6 addresses: 0 for always, 1 for check and 2 for disabled])
])
  

dnl AC_OUTPUT(kdecore/kde-config.cpp)

SVGICONS=
	
KDE_FIND_PATH(libart2-config, LIBART_CONFIG, [${prefix}/bin ${exec_prefix}/bin /usr/local/bin /opt/local/bin], [
  AC_MSG_WARN([Could not find libart anywhere, check http://www.levien.com/libart/])
])

if test -n "$LIBART_CONFIG"; then
  vers=`$LIBART_CONFIG --version 2>/dev/null | awk 'BEGIN { FS = "."; } { printf "%d", ($1 * 1000 + $2) * 1000 + $3;}'`
  if test -n "$vers" && test "$vers" -ge 2003008
  then
     LIBART_LIBS="`$LIBART_CONFIG --libs`"
     LIBART_RPATH=
     for args in $LIBART_LIBS; do
	  case $args in
	    -L*)
  	       LIBART_RPATH="$LIBART_RPATH $args"
 	       ;;
          esac
     done
     LIBART_RPATH=`echo $LIBART_RPATH | sed -e "s/-L/-R/g"`
     LIBART_CFLAGS="`$LIBART_CONFIG --cflags`"
     SVGICONS=svgicons

     AC_DEFINE_UNQUOTED(HAVE_LIBART, 1, [Defines if your system has the libart library])
  else
     AC_MSG_WARN([You need at least libart 2.3.8])
  fi
fi


AC_SUBST(LIBART_LIBS)
AC_SUBST(LIBART_CFLAGS)
AC_SUBST(LIBART_RPATH)
AM_CONDITIONAL(include_SVGICONS, test -n "$SVGICONS")

AC_MSG_CHECKING([for revoke(tty) in unistd.h])
AC_TRY_LINK(dnl
      [
        #include <unistd.h>
      ],
      [
	revoke("/dev/tty");
      ],
      [
        AC_DEFINE(HAVE_REVOKE, 1, [Define if revoke(tty) is present in unistd.h])
        AC_MSG_RESULT(yes)
      ],
      AC_MSG_RESULT(no)
)
