# 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)

KDE_SET_PREFIX

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

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 sys/cdefs.h fnmatch.h sysent.h strings.h sys/stat.h sys/select.h sys/socket.h linux/socket.h socketbits.h sigaction.h paths.h malloc.h monetary.h limits.h sys/soundcard.h dlfcn.h)

DCOPIDL2CPP="compiled"
DCOPIDL="compiled"
MEINPROC="compiled"
KDECONFIG="compiled"

dnl Checks for libraries etc
AC_BASE_PATH_KDE([don't test]) dnl kdelibs is a special case
AC_CREATE_KFSSTND(default)
AC_SUBST_KFSSTND

KDE_CREATE_LIBS_ALIASES

AC_DEFUN([KDE_DO_UIC_AGAIN],
[
AC_REQUIRE([AC_PATH_QT_MOC_UIC])
UIC=$UIC_PATH
if test x$ac_uic_supports_nounload = xyes; then
    UIC="$UIC -nounload"
fi
AC_SUBST(UIC)
])
KDE_DO_UIC_AGAIN

# configure would do this very late. Too late for us!
test "x$prefix" = xNONE && prefix=$ac_default_prefix

AC_DEFINE_UNQUOTED(KDEDIR, "$prefix", [The prefix to use as fallback])

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)
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_MKSTEMP
AC_CHECK_MKSTEMPS
KDE_CHECK_STRLCPY
AC_CHECK_FUNCS(strtoll socket seteuid setegid strfmon stpcpy gettimeofday)

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_LANG_SAVE
AC_LANG_C
dnl the vfork test in autoconf has a bug with C++ compilers (it declares
dnl the prototype for sparc_address_test() correctly but calls it without
dnl arguments), so we switch languages here.
AC_FUNC_VFORK
AC_LANG_RESTORE

AC_CHECK_INITGROUPS
AC_CHECK_USLEEP
AC_CHECK_GETHOSTNAME
AC_CHECK_RANDOM
AC_CHECK_S_ISSOCK
dnl AC_CHECK_SOCKLEN_T already in kde_misc_tests

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

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

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_KDECORE, '$(top_builddir)/kdecore/libkdecore.la')
AC_SUBST(LIB_KDEUI, '$(top_builddir)/kdeui/libkdeui.la')
AC_SUBST(LIB_KIO, '$(top_builddir)/kio/libkio.la')
AC_SUBST(LIB_KPARTS, '$(top_builddir)/kparts/libkparts.la')

AC_SUBST(EXTRA_SUBDIRS)

AC_MSG_CHECKING(if malloc debugging is wanted)
AC_ARG_WITH(dmalloc,
[  --with-dmalloc          use dmalloc, as in
			  http://dmalloc.com/releases/],
[if test "$withval" = yes; then
  AC_MSG_RESULT(yes)
  AC_DEFINE(WITH_DMALLOC,1,
            [Define if using the dmalloc debugging malloc package])
  LIB_DMALLOC="-ldmalloc"
  LDFLAGS="$LDFLAGS -g"
else
  AC_MSG_RESULT(no)
fi], [AC_MSG_RESULT(no)])

AC_SUBST(LIB_DMALLOC)

AC_MSG_CHECKING(if MIT-SHM support is wanted)
AC_ARG_ENABLE(mitshm,
[  --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_SUBST(ICE_SUBDIR, 'ICE')
AC_SUBST(ICE_RLIB, 'ICE/libkICE.la')

AC_SUBST(DCOPIDL, '$(top_builddir)/dcop/dcopidl/dcopidl')
AC_SUBST(DCOPIDL2CPP, '$(top_builddir)/dcop/dcopidl2cpp/dcopidl2cpp')

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

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,
[  --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)

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