# Original Author was Kalle@kde.org
# I lifted it in some mater. (Stephan Kulow)
# I used much code from Janos Farkas

dnl Process this file with autoconf to produce a configure script.
AC_INIT(acinclude.m4) dnl a source file from your sub dir

dnl This is so we can use kde-common
AC_CONFIG_AUX_DIR(admin)

dnl Setting the prefix for tdelibs uses a special version of KDE_SET_PREFIX
dnl (since that one normally looks for existing kde installations)
KDE_SET_PREFIX_CORE


AC_CANONICAL_SYSTEM
AC_ARG_PROGRAM

dnl Automake doc recommends to do this only here. (Janos)
AM_INIT_AUTOMAKE(@MODULENAME@, @VERSION@) dnl searches for some needed programs

AM_MAINTAINER_MODE

AC_PROG_INSTALL

dnl generate the config header
AM_CONFIG_HEADER(config.h) dnl at the distribution this done

dnl Checks for programs.
AC_CHECK_COMPILERS
AC_LIBLTDL_CONVENIENCE

AC_ENABLE_SHARED(yes)
AC_ENABLE_STATIC(no)
KDE_PROG_LIBTOOL

AC_LANG_SAVE
AC_LANG_C
KDE_LIB_LTDL
AC_LANG_RESTORE

dnl for NLS support. Call them in this order!
dnl WITH_NLS is for the po files, GNU_GETTEXT for the sources
AM_KDE_WITH_NLS
AM_KDE_GNU_GETTEXT

dnl Checks for header files.
KDE_CHECK_STL
AC_HEADER_DIRENT
AC_HEADER_STDC
AC_CHECK_HEADERS(sys/param.h sys/mman.h sys/time.h sysent.h strings.h sys/stat.h sys/select.h paths.h malloc.h limits.h sys/soundcard.h dlfcn.h termios.h sys/prctl.h)

AC_ARG_WITH(acl,
  [AC_HELP_STRING(--with-acl,
    [enable support for POSIX ACLs @<:@default=check@:>@])],
  [], with_acl=check)

ACL_LIBS=""
if test "x$with_acl" != xno; then
  USE_POSIX_ACL=yes
  HAVE_NON_POSIX_ACL_EXTENSIONS=no
  KDE_CHECK_HEADER(sys/acl.h, ,[USE_POSIX_ACL="no"])
  HAVE_SYS_XATTR_H=no
  KDE_CHECK_HEADER(sys/xattr.h,[HAVE_SYS_XATTR_H="yes"],)
  if test "$HAVE_SYS_XATTR_H" = "yes" ; then
    AC_DEFINE([HAVE_SYS_XATTR_H], 1, [Define if system has the sys/xattr.h header.])
  fi

  case $host in
      *-*-freebsd*)
              KDE_CHECK_LIB(c,acl_init,[ACL_LIBS="-lc"],[USE_POSIX_ACL="no"])
              ;;
      *)
              KDE_CHECK_HEADER(attr/libattr.h, ,[USE_POSIX_ACL="no"])
              KDE_CHECK_LIB(acl,acl_init,[ACL_LIBS="-lacl -lattr"],[USE_POSIX_ACL="no"])
              KDE_CHECK_HEADER(acl/libacl.h,[HAVE_NON_POSIX_ACL_EXTENSIONS="yes"],)
              if test "$HAVE_SYS_XATTR_H" = "no" ; then
                USE_POSIX_ACL="no"
              fi
              ;;
  esac

  if test "$USE_POSIX_ACL" = "yes" ; then
    AC_DEFINE([USE_POSIX_ACL], 1, [Define if system has POSIX ACL support.])
    if test "$HAVE_NON_POSIX_ACL_EXTENSIONS" = "yes" ; then
      AC_DEFINE([HAVE_NON_POSIX_ACL_EXTENSIONS], 1, [Define if system has non-POSIX extensions to the ACL support.])
    fi
  fi

  if test "x$with_acl" != xcheck && test "$USE_POSIX_ACL" != "yes"; then
    AC_MSG_ERROR([--with-acl was given, but test for POSIX ACLs failed])
  fi
fi
AC_SUBST(ACL_LIBS)

DCOPIDL2CPP="compiled"
DCOPIDL="compiled"
DCOPIDLNG="compiled"
MEINPROC="compiled"
KDECONFIG="compiled"
KCONFIG_COMPILER="compiled"
MAKEKDEWIDGETS="compiled"

dnl Checks for libraries etc

ac_cpp_safe=$ac_cpp
ac_CPPFLAGS_safe=$CPPFLAGS
CPPFLAGS="$CPPFLAGS $X_INCLUDES"
ac_cpp='$CXXCPP $CPPFLAGS'
KDE_CHECK_HEADERS(X11/extensions/shape.h,,,
[
#include <X11/Xlib.h>
#include <X11/Xutil.h>
])
KDE_CHECK_HEADERS(X11/extensions/XShm.h, 
  [have_mitshm=yes], [have_mitshm=no], [#include <X11/Xlib.h>])
KDE_CHECK_HEADERS(X11/ICE/ICElib.h)
KDE_CHECK_HEADER(sys/sendfile.h, [have_sendfile=yes], )
ac_cpp=$ac_cpp_safe
CPPFLAGS=$ac_CPPFLAGS_safe

dnl Checks for typedefs, structures, and compiler characteristics.
AC_HEADER_TIME

AC_LANG_SAVE
AC_LANG_C
dnl AC_C_BIGENDIAN has a bug (one of its tests uses "main()" instead of
dnl "int main()") so C++ compilers would break. Thats why we switch languages
AC_C_BIGENDIAN
AC_LANG_RESTORE

dnl check if the compiler has bool
AC_CHECK_BOOL

dnl Checks for library functions.
AC_CHECK_SETENV
AC_CHECK_UNSETENV
AC_CHECK_RANDOM
AC_CHECK_MKSTEMPS
AC_CHECK_MKSTEMP
AC_CHECK_MKDTEMP
AC_CHECK_FUNCS(strtoll socket seteuid setegid strfmon stpcpy gettimeofday readdir_r setpriority tcgetattr tcsetattr)

AH_BOTTOM([
/* provide a definition for a 32 bit entity, usable as a typedef, possibly
   extended by "unsigned" */
#undef INT32_BASETYPE
#ifdef SIZEOF_INT
#if SIZEOF_INT == 4
#define INT32_BASETYPE int
#endif
#endif
#if !defined(INT32_BASETYPE) && defined(SIZEOF_LONG)
#if SIZEOF_LONG == 4
#define INT32_BASETYPE long
#endif
#endif
#ifndef INT32_BASETYPE
#define INT32_BASETYPE int
#endif

#ifndef HAVE_SETEUID
#define HAVE_SETEUID
#define HAVE_SETEUID_FAKE
#ifdef __cplusplus
extern "C"
#endif
int seteuid(INT32_BASETYPE euid); /* defined in fakes.c */
#endif
])

AC_CHECK_USLEEP
AC_CHECK_GETHOSTNAME
AC_CHECK_RANDOM
AC_CHECK_S_ISSOCK
AC_CHECK_INITGROUPS

AC_MSG_CHECKING([for mount tab file])
AC_CACHE_VAL(kde_cv_mtab_file,
[
    kde_cv_mtab_file=no

    for ac_file in    \
                      \
        /etc/mtab \
        /etc/mnttab \
     ; \
    do
    if test -r "$ac_file"; then
       kde_cv_mtab_file=$ac_file
       break
    fi
    done
])

AC_MSG_RESULT($kde_cv_mtab_file)
if test "$kde_cv_mtab_file" != "no"; then
    AC_DEFINE_UNQUOTED(MTAB_FILE, "$kde_cv_mtab_file", [Define the file for mount entries])
fi

dnl Check for Qt
AC_PATH_QT

if test -n "$qt_includes"; then
  QNAMESPACE_H="$qt_includes/qnamespace.h"
fi
AC_SUBST(QNAMESPACE_H)

AC_LANG_SAVE
AC_LANG_CPLUSPLUS
AC_MSG_CHECKING([if getmntinfo() uses struct statvfs])
AC_TRY_LINK([#include <sys/types.h>
#include <sys/statvfs.h>
],[int flags = 0; struct statvfs *mntbufp; return getmntinfo(&mntbufp, flags);],
[AC_MSG_RESULT(yes)
AC_DEFINE(GETMNTINFO_USES_STATVFS,,[getmntinfo() uses struct statvfs])],
AC_MSG_RESULT(no))
AC_LANG_RESTORE

dnl output files
AC_SUBST(x_includes)
AC_SUBST(x_libraries)
AC_SUBST(LIBSOCKET)
AC_SUBST(LIBCOMPAT)
AC_SUBST(LIBUTIL)
AC_SUBST(LIBRESOLV)
AC_SUBST(LIBICE)

AC_SUBST(LIB_TQT, '$(top_builddir)/qtinterface/libtqt.la')

AC_SUBST(EXTRA_SUBDIRS)

AC_MSG_CHECKING(if sendfile() is wanted)
AC_ARG_ENABLE(sendfile,
AC_HELP_STRING([--enable-sendfile],[enable the use of sendfile() (Linux only)]),
[if test "$enableval" = yes; then
  AC_MSG_RESULT(yes)
  want_sendfile="yes"
else
  AC_MSG_RESULT(no)
fi], [AC_MSG_RESULT(no)])

if test "$want_sendfile" = "yes" -a "$have_sendfile" = "yes"; then
  AC_SUBST(HAVE_SENDFILE)
  AC_DEFINE(HAVE_SENDFILE,1,[Define if you want sendfile() support])
fi

AC_MSG_CHECKING(if MIT-SHM support is wanted)
AC_ARG_ENABLE(mitshm,
AC_HELP_STRING([--enable-mitshm],[use MIT-SHM for pixmap loading/saving]),
[if test "$enableval" = yes; then
  AC_MSG_RESULT(yes)
  want_mitshm="yes"
else
  AC_MSG_RESULT(no)
fi], [AC_MSG_RESULT(yes); want_mitshm="yes"])

if test "$want_mitshm" = "yes" -a "$have_mitshm" = "yes"; then
  AC_SUBST(HAVE_MITSHM)
  AC_DEFINE(HAVE_MITSHM,1,[Define if you want MIT-SHM support])
fi

AC_CHECK_FUNC(posix_fadvise,[AC_DEFINE(HAVE_FADVISE,1,[Define is posix_fadvise is supported] )])

ac_save_LIBS="$LIBS"
LIBS="$LIBS $X_LDFLAGS -lICE"
AC_CHECK_FUNCS(_IceTransNoListen)
LIBS="$ac_save_LIBS"

if test "x$kde_use_qt_emb" != "xyes"; then
   CXXFLAGS="$CXXFLAGS $USE_RTTI"
fi

AC_ARG_WITH([rgbfile],
  AC_HELP_STRING([--with-rgbfile=path], [Define custom path for rgb.txt. (default: \$(x11libdir)/X11/rgb.txt)]),
  [rgb_file=$withval], [rgb_file="$x_libraries/X11/rgb.txt"])

AC_DEFINE_UNQUOTED(X11_RGBFILE, "$rgb_file", [where rgb.txt is in])

AC_MSG_CHECKING([for Compiler version])
case "$CXX" in
xlC*)
  # The __IBMCPP__ macro contains the C++ compiler version in decimal notation.
  echo "__IBMCPP__" > conftest.C

  # Preprocess and at least make sure it's returned a number.
  changequote(, )dnl
  $CXX -E conftest.C 2>&1 | grep "^[0-9][0-9][0-9]" > conftest.i
  changequote([, ])dnl

  # "502" would signify version 5.0.2.
  clean_compiler_version="$CXX version "`cut -b1 conftest.i`"."`cut -b2 conftest.i`"."`cut -b3 conftest.i`
  rm -f conftest.C conftest.i
 ;;
KCC*)
  # KCC doesn't give it's version, if nothing is compiled (the frontend
  # isn't called in that case)
  rm -f conftest.C conftest.o
  echo "void f(void) {}" > conftest.C
  clean_compiler_version=`$CXX -V -c conftest.C -o conftest.o 2>&1 | sed 's/--.*//'`
  rm -f conftest.C conftest.o
 ;;
*)
  for flag in '-v' '-V' '--version' '-version'; do
    compiler_version=`$CXX $flag 2>&1 | egrep -v "Reading specs|Using built-?in specs|Configured with|Thread model" | head -n 1`
    clean_compiler_version=`echo $compiler_version | egrep -vi 'Usage|ERROR|unknown option|WARNING|missing|###'`
    test -n "$clean_compiler_version" && break
  done
 ;;
esac

# Quote backslashesm, as we are going to make this a string.
clean_compiler_version=`echo $clean_compiler_version | sed 's/\([\\\\]\)/\\\\\\1/g'`
if test -z "$clean_compiler_version"; then
  clean_compiler_version="unknown"
fi
AC_DEFINE_UNQUOTED(KDE_COMPILER_VERSION, "$clean_compiler_version", [what C++ compiler was used for compilation])
AC_MSG_RESULT($clean_compiler_version)

AC_MSG_CHECKING([for uname])
case `uname -s` in
AIX)
  IBM_ARCH=PowerPC #good default
  if [ -x /usr/sbin/lsdev -a -x /usr/sbin/lsattr ]; then
    IBM_CPU_ID=`/usr/sbin/lsdev -C -c processor -S available | head -n 1 | awk '{ print $1 }'`
    if /usr/sbin/lsattr -El ${IBM_CPU_ID} | grep ' POWER' >/dev/null 2>&1; then
      IBM_ARCH=POWER
    fi
  fi
  uname_string=`uname -svr | awk '{ print $1" "$3"."$2 }'`" ${IBM_ARCH}"
 ;;
*)
  uname_string=`uname -smr`
 ;;
esac
AC_DEFINE_UNQUOTED(KDE_COMPILING_OS, "$uname_string", [what OS used for compilation])
AC_MSG_RESULT($uname_string)

AC_MSG_CHECKING([for distribution channel])
AC_ARG_WITH(distribution,
AC_HELP_STRING([--with-distribution],[indicate the distribution in bug reports]),
[kde_distribution_text="$withval"
], [kde_distribution_text='compiled sources'])
AC_DEFINE_UNQUOTED(KDE_DISTRIBUTION_TEXT, "$kde_distribution_text", [Distribution Text to append to OS])
AC_MSG_RESULT($kde_distribution_text)

AC_ARG_WITH(utempter,
[AC_HELP_STRING([--with-utempter], [use utempter for utmp management @<:@default=check@:>@])],
[], with_utempter=check)
if test "x$with_utempter" != xno; then
  kde_have_utempter=yes
  AC_CHECK_LIB(utempter, addToUtmp, [LIBUTEMPTER=-lutempter], kde_have_utempter=no)
  AC_SUBST(LIBUTEMPTER)
  if test "$kde_have_utempter" = "yes"; then
    AC_DEFINE_UNQUOTED(HAVE_UTEMPTER, 1, [Define if you have the utempter helper for utmp managment])
  fi
  if test "x$with_utempter" != xcheck && test "x$kde_have_utempter" != xyes; then
    AC_MSG_ERROR([--with-utempter was given, but test for utempter failed])
  fi
fi

KDE_INIT_DOXYGEN([The KDE API Reference], [Version $VERSION])
KDE_CHECK_BINUTILS