AC_DEFUN(AC_CHECK_CUPS_VERSION,
[
kde_save_cflags="$CFLAGS"
kde_save_libs="$LIBS"
LIBS="$all_libraries $USER_LDFLAGS -lcups $LIBDL $LIBSOCKET"
CFLAGS="$CFLAGS $all_includes $USER_INCLUDES"
AC_MSG_CHECKING(CUPS version >= $1)
kde_save_link=$ac_link
ac_link='${CC} $CFLAGS -o conftest conftest.c $LIBS 1>&5'
cat > conftest.c <<EOF
#include <cups/cups.h>
#include <stdio.h>
int main()
{
  if (CUPS_VERSION <
EOF
echo "$1" >> conftest.c
cat >> conftest.c <<EOF
)
    printf("no\n");
  else
    printf("yes\n");
}
EOF
if AC_TRY_EVAL(ac_link) && test -x conftest; then
  ac_have_new_cups=`./conftest`
else
  AC_MSG_ERROR([Unable to compile test program. See config.log for more informations])
  echo "configure: failed program was:" >&AC_FD_CC
  cat conftest.c >&AC_FD_CC
  ac_have_new_cups="no"
fi
rm -f conftest*
ac_link="$kde_save_link"
CFLAGS="$kde_save_cflags"
LIBS="$kde_save_libs"
AC_MSG_RESULT($ac_have_new_cups)
])

dnl CUPS stuffs (to be changed)
ac_use_cups="yes"
ac_have_new_cups="yes"
ac_cups_libs=0
CUPSSUBDIR=
LIB_CUPS=
AC_ARG_ENABLE(cups,[  --disable-cups          disable CUPS support [default=auto]],ac_use_cups=$enableval, ac_use_cups=yes)
if test "$ac_use_cups" = "yes"; then
  ac_CPPFLAGS_save="$CPPFLAGS"
  CPPFLAGS="$CPPFLAGS $all_includes"
  ac_LDFLAGS_save="$LDFLAGS"
  LDFLAGS="$LDFLAGS $all_libraries"
  AC_CHECK_LIB(cups,cupsServer,[ac_cups_libs=1],[ac_cups_libs=0],$LIBDL $LIBSOCKET)
  if test "$ac_cups_libs" = "0"; then
    AC_MSG_WARN(CUPS library not found. CUPS support disabled)
    ac_use_cups="no"
  fi
  if test "$ac_use_cups" = "yes"; then
    dnl check existence of CUPS header files
    ac_cups_headers=0
    AC_CHECK_HEADER(cups/cups.h,[ac_cups_headers=1])
    if test "$ac_cups_headers" = "0"; then
      AC_MSG_WARN(CUPS headers not found. CUPS support disabled)
      ac_use_cups="no"
    else
      dnl check that version is at least 1.1.9
      AC_CHECK_CUPS_VERSION(1.0109)
      if test "$ac_have_new_cups" = "no"; then
        AC_MSG_WARN(CUPS version too old. You should upgrade to version >= 1.1.9)
	ac_use_cups="no"
      fi
    fi
  fi
  LDFLAGS="$ac_LDFLAGS_save"
  CPPFLAGS="$ac_CPPFLAGS_save"
fi
if test "$ac_use_cups" = "yes"; then
  CUPSSUBDIR="cups"
  LIB_CUPS="-lcups $LIBDL $LIBSOCKET"
  AC_DEFINE(HAVE_CUPS, 1, [Defines if you have CUPS (Common UNIX Printing System)])
fi
AM_CONDITIONAL(include_cups_SUBDIR, test -n "$CUPSSUBDIR")
AC_SUBST(LIB_CUPS)

dnl check for Qt-3.1 feature: QPrinter::setMargins()
AC_MSG_CHECKING(Qt contains QPrinter::setMargins())
kdeprint_use_margins="no"
AC_LANG_SAVE
AC_LANG_CPLUSPLUS
kdeprint_save_cxxflags="$CXXFLAGS"
CXXFLAGS="$CXXFLAGS $all_includes"
AC_TRY_COMPILE(
[
#include <qprinter.h>
],
[
QPrinter prt;
prt.setMargins(0, 0, 0, 0);
],
kdeprint_use_margins="yes"
)
AC_LANG_RESTORE
CXXFLAGS="$kdeprint_save_cxxflags"
AC_MSG_RESULT($kdeprint_use_margins)
if test "$kdeprint_use_margins" = "yes"; then
  AC_DEFINE(KDEPRINT_USE_MARGINS, 1, [Defines if KDEPrint should include user-specified margins support])
fi
