# Process this file with autoconf to produce a configure script.
AC_INIT(gdk/gdktypes.h)

# Save this value here, since automake will set cflags later
cflags_set=${CFLAGS+set}

GTK_MAJOR_VERSION=1
GTK_MINOR_VERSION=0
GTK_MICRO_VERSION=4
GTK_VERSION=$GTK_MAJOR_VERSION.$GTK_MINOR_VERSION.$GTK_MICRO_VERSION

# For automake.
VERSION=$GTK_VERSION
PACKAGE=gtk+

# Configure glib
AC_CONFIG_SUBDIRS(glib)

dnl Initialize automake stuff
AM_INIT_AUTOMAKE($PACKAGE, $VERSION, no-define)

AC_SUBST(GTK_MAJOR_VERSION)
AC_SUBST(GTK_MINOR_VERSION)
AC_SUBST(GTK_MICRO_VERSION)
AC_SUBST(GTK_VERSION)

# Save this value here, since automake will set cflags later
cflags_set=${CFLAGS+set}

# Specify a configuration file
AM_CONFIG_HEADER(config.h)

dnl Initialize libtool
AM_PROG_LIBTOOL

dnl Initialize maintainer mode
AM_MAINTAINER_MODE

AC_CANONICAL_HOST

AC_ARG_ENABLE(shm, [  --enable-shm            support shared memory if available [default=yes]],
		   echo $enable_shm, enable_shm="yes")
AC_ARG_ENABLE(debug, [  --enable-debug=[no/minimum/yes] turn on debugging [default=minimum]],,enable_debug=minimum)
AC_ARG_ENABLE(ansi, [  --enable-ansi           turn on strict ansi [default=no]],
		    , enable_ansi=no)
AC_ARG_ENABLE(xim, [  --enable-xim            support XIM [default=yes]],
			echo $enable_xim, enable_xim="yes")
AC_ARG_WITH(locale, [  --with-locale=LOCALE    locale name you want to use ])

AC_ARG_WITH(xinput, [  --with-xinput=[no/gxi/xfree] support XInput ])

if test "x$enable_debug" = "xyes"; then
  test "$cflags_set" = set || CFLAGS="$CFLAGS -g"
  CFLAGS="$CFLAGS -DG_ENABLE_DEBUG"
else
  if test "x$enable_debug" = "xno"; then
    CFLAGS="$CFLAGS -DG_DISABLE_ASSERT -DG_DISABLE_CHECKS -DGTK_NO_CHECK_CASTS"
  else
    CFLAGS="$CFLAGS -DGTK_NO_CHECK_CASTS"
  fi
fi

# Build time sanity check...
AM_SANITY_CHECK

# Checks for programs.
AC_PROG_CC
AM_PROG_CC_STDC
AC_PROG_INSTALL
AC_PROG_MAKE_SET

if test "x$GCC" = "xyes"; then
  if test -z "`echo "$CFLAGS" | grep "\-Wall" 2> /dev/null`" ; then
    CFLAGS="$CFLAGS -Wall"
  fi

  if test "x$enable_ansi" = "xyes"; then
    if test -z "`echo "$CFLAGS" | grep "\-ansi" 2> /dev/null`" ; then
      CFLAGS="$CFLAGS -ansi"
    fi

    if test -z "`echo "$CFLAGS" | grep "\-pedantic" 2> /dev/null`" ; then
      CFLAGS="$CFLAGS -pedantic"
    fi
  fi
fi

if test "x$enable_xim" = "xyes"; then
  CFLAGS="$CFLAGS -DUSE_XIM"
fi

# Find the X11 include and library directories
AC_PATH_X
AC_PATH_XTRA

if test "x$no_x" = "xyes"; then 
  AC_MSG_ERROR([
*** X libraries or include files not found. Check 'config.log' for 
*** more details.])
fi

if test "x$x_includes" = "x"; then
  x_includes="/usr/include"
fi

saved_cflags="$CFLAGS"
saved_ldflags="$LDFLAGS"

CFLAGS="$CFLAGS $X_CFLAGS"
LDFLAGS="$LDFLAGS $X_LDFLAGS $X_LIBS"

# Checks for libraries.
# Check for the X11 library
AC_CHECK_LIB(X11, XOpenDisplay, x_libs="-lX11 $X_EXTRA_LIBS",
  AC_MSG_ERROR([*** libX11 not found. Check 'config.log' for more details.]),
  $X_EXTRA_LIBS)

if test "x$enable_shm" = "xyes"; then
  # Check for the Xext library (needed for XShm extention)
  AC_CHECK_LIB(Xext, XShmAttach, 
      x_libs="-lXext $x_libs", 
      # On AIX, it is in XextSam instead, but we still need -lXext
      AC_CHECK_LIB(XextSam, XShmAttach, 
          x_libs="-lXextSam -lXext $x_libs", 
          no_xext_lib=yes, $x_libs),
      $x_libs)
fi

# Check for shaped window extension

AC_CHECK_LIB(Xext, XShapeCombineMask,
      if test -z "`echo $x_libs | grep "\-lXext" 2> /dev/null`"; then
           x_libs="-lXext $x_libs"
      fi
      AC_DEFINE(HAVE_SHAPE_EXT),, $x_libs)

x_cflags="$X_CFLAGS"
x_ldflags="$X_LDFLAGS $X_LIBS"

# set up things for XInput

if test "x$with_xinput" = "xgxi" || test "x$with_xinput" = "xyes"; then
  AC_DEFINE(XINPUT_GXI)
  xinput_progs=gxid
  x_libs="-lXi $x_libs"
elif test "x$with_xinput" = "xxfree"; then
  AC_DEFINE(XINPUT_XFREE)
  x_libs="-lXi $x_libs"
else
  AC_DEFINE(XINPUT_NONE)
fi

AC_SUBST(x_cflags)
AC_SUBST(x_includes)
AC_SUBST(x_ldflags)
AC_SUBST(x_libs)
AC_SUBST(xinput_progs)

CFLAGS="$saved_cflags"
LDFLAGS="$saved_ldflags"

if test "x$enable_shm" = "xyes"; then
  # Check for shared memory
  AC_CHECK_HEADER(sys/ipc.h, AC_DEFINE(HAVE_IPC_H), no_sys_ipc=yes)
  AC_CHECK_HEADER(sys/shm.h, AC_DEFINE(HAVE_SHM_H), no_sys_shm=yes)

  # Check whether shmctl IPC_RMID allowes subsequent attaches
  if test "$ac_cv_header_sys_shm_h" = "yes"; then
    AC_MSG_CHECKING(whether shmctl IPC_RMID allowes subsequent attaches)
    AC_TRY_RUN([
          #include <sys/types.h>
          #include <sys/ipc.h>
          #include <sys/shm.h>
          int main()
          {
            int id;
            char *shmaddr;
          id = shmget (IPC_PRIVATE, 4, IPC_CREAT | 0777);
          if (id == -1)
            exit (2);
            shmaddr = shmat (id, 0, 0);
            shmctl (id, IPC_RMID, 0);
            if ((char*) shmat (id, 0, 0) == (char*) -1)
            {
              shmdt (shmaddr);
              exit (1);
            }
            shmdt (shmaddr);
            shmdt (shmaddr);
            exit (0);
          }
      ],
      AC_DEFINE(IPC_RMID_DEFERRED_RELEASE)
        AC_MSG_RESULT(yes),
      AC_MSG_RESULT(no),
      AC_MSG_RESULT(assuming no))
  fi

  # Check for the X shared memory extension header file
  AC_MSG_CHECKING(X11/extensions/XShm.h)
  if test "x$no_xext_lib" = "xyes"; then
    AC_MSG_RESULT(no)
    no_xshm=yes
  else
    if test -f "$x_includes/X11/extensions/XShm.h"; then
      AC_MSG_RESULT(yes)
      AC_DEFINE(HAVE_XSHM_H)
    else
      AC_MSG_RESULT(no)
      no_xshm=yes
    fi
  fi
fi

# Check for private display resource base variable
AC_MSG_CHECKING(resource base field in XDisplay)
AC_CACHE_VAL(gtk_cv_display_resource_base,
[AC_TRY_RUN([
#define XLIB_ILLEGAL_ACCESS
#include <X11/Xlib.h>

int
main ()
{
  Display *display;

  return 0;

  display->resource_base;
}],
gtk_cv_display_resource_base="resource_base",
gtk_cv_display_resource_base="private3")])
AC_MSG_RESULT($gtk_cv_display_resource_base)
AC_DEFINE_UNQUOTED(RESOURCE_BASE, gdk_display->$gtk_cv_display_resource_base)

# Check if X_LOCALE definition is necessary

AC_MSG_CHECKING(need -DX_LOCALE)

AC_TRY_RUN([
#include <stdio.h>
#include <locale.h>

int
main ()
{
  return setlocale (LC_ALL, "${with_locale}") == NULL;
}],
need_x_locale=no,
need_x_locale=yes)
AC_MSG_RESULT($need_x_locale)

if test $need_x_locale = yes; then
  CFLAGS="$CFLAGS -DX_LOCALE"
fi

# Checks for header files.
AC_HEADER_STDC

# Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST

# Checks for library functions.
AC_TYPE_SIGNAL
AC_FUNC_MMAP

# Check for sys/select.h

AC_MSG_CHECKING([fd_set and sys/select])
AC_TRY_COMPILE([#include <sys/types.h>],
        [fd_set readMask, writeMask;], gtk_ok=yes, gtk_ok=no)
if test $gtk_ok = no; then
    AC_HEADER_EGREP(fd_mask, sys/select.h, gtk_ok=yes)
    if test $gtk_ok = yes; then
        AC_DEFINE(HAVE_SYS_SELECT_H)
    fi
fi
AC_MSG_RESULT($gtk_ok)
if test $gtk_ok = no; then
    AC_DEFINE(NO_FD_SET)
fi

AC_OUTPUT([
Makefile
gtk-config
docs/Makefile
gdk/Makefile
gtk/Makefile
gtk/gtkfeatures.h
],
[chmod +x gtk-config])
