AC_INIT(gdk_imlib/gdk_imlib.h)

AM_CONFIG_HEADER(config.h)

AM_INIT_AUTOMAKE(imlib, 1.8)

AM_PROG_LIBTOOL
AC_PROG_RANLIB
AC_PROG_CC


AM_PATH_GTK(1.0.0, 
  GDK_IMLIB="gdk_imlib utils",
  GDK_IMLIB="" 
  AC_MSG_WARN([*** gdk_imlib will not be built ***]))

AC_SUBST(GDK_IMLIB)

AC_PATH_PROG(DJPEG_PROG, djpeg)
AC_PATH_PROG(CJPEG_PROG, cjpeg)

AC_PATH_PROG(CONVERT_PROG, convert)
echo "CONVERT_PROG is $CONVERT_PROG"
CONVERT_PATH=`echo $CONVERT_PROG | sed 's/\/convert//'`
echo "CONVERT_PATH is $CONVERT_PATH"
AC_SUBST(CONVERT_PATH)

AC_PATH_PROG(GIFTOPNM_PROG, giftopnm)
echo "GIFTOPNM_PROG is $GIFTOPNM_PROG"
NETPBM_PATH=`echo $GIFTOPNM_PROG | sed 's/\/giftopnm//'`
echo "NETPBM_PATH is $NETPBM_PATH"
AC_SUBST(NETPBM_PATH)

AC_HEADER_STDC
AC_HEADER_DIRENT
AC_HEADER_SYS_WAIT
AC_TYPE_SIGNAL
AC_FUNC_VPRINTF
AC_FUNC_WAIT3
AC_CHECK_FUNCS(snprintf)
AC_CHECK_FUNCS(blumfrub)
AC_CHECK_FUNCS(buckets_of_erogenous_nym)
AC_CHECK_FUNCS(buttox)

AC_CHECK_HEADER(string.h)
AC_CHECK_HEADER(strings.h)

AC_PATH_X
AC_PATH_XTRA
if test -z "$GDK_IMLIB"; then
  # see macros/gnome-x-checks.m4, and gtk.m4
  CPPFLAGS="$CPPFLAGS $X_CFLAGS"
  GX_LIBS="$X_PRE_LIBS $X_LIBS -lX11 -lXext -lm"
else
  CPPFLAGS="$CPPFLAGS $GTK_CFLAGS"
  GX_LIBS="$GTK_LIBS"
fi

SUPPORT_LIBS=""

dnl Test for libjpeg
AC_CHECK_LIB(jpeg, jpeg_destroy_decompress,
  jpeg_ok=yes,
  jpeg_ok=no
  AC_MSG_WARN(*** Native JPEG support will not be built (JPEG library not found) ***), $GX_LIBS)
if test "$jpeg_ok" = yes; then
  AC_MSG_CHECKING([for jpeg.h])
  AC_TRY_CPP(
[#include <stdio.h>
#undef PACKAGE
#undef VERSION
#include <jpeglib.h>],
    jpeg_ok=yes,
    jpeg_ok=no)
  AC_MSG_RESULT($jpeg_ok)
  if test "$jpeg_ok" = yes; then
    SUPPORT_LIBS="$SUPPORT_LIBS -ljpeg"; AC_DEFINE(HAVE_LIBJPEG)
  else
    AC_MSG_WARN(*** Native JPEG support will not be built (JPEG header file not found) ***)
  fi
fi

dnl Test for libtiff
AC_CHECK_LIB(tiff, TIFFReadScanline,
  AC_CHECK_HEADER(tiffio.h,
    SUPPORT_LIBS="$SUPPORT_LIBS -ltiff"; AC_DEFINE(HAVE_LIBTIFF),
    AC_MSG_WARN(*** Native TIFF support will not be built (TIFF header files not found) ***)),
  AC_CHECK_LIB(tiff, TIFFWriteScanline,
    AC_CHECK_HEADER(tiffio.h,
      SUPPORT_LIBS="$SUPPORT_LIBS -ltiff -ljpeg -lz"; AC_DEFINE(HAVE_LIBTIFF),
      AC_MSG_WARN(*** Native TIFF support will not be built (TIFF header files not found) ***)),
    AC_CHECK_LIB(tiff34, TIFFFlushData,
      AC_CHECK_HEADER(tiffio.h,
        SUPPORT_LIBS="$SUPPORT_LIBS -ltiff34 -ljpeg -lz"; AC_DEFINE(HAVE_LIBTIFF),
        AC_MSG_WARN(*** Native TIFF support will not be built (TIFF header files not found) ***)),
    AC_MSG_WARN(*** Native TIFF support will not be built (TIFF library not found) ***), -ljpeg -lz $GX_LIBS), -ljpeg -lz $GX_LIBS), $GX_LIBS)


AC_CHECK_HEADER(gif_lib.h,
  AC_CHECK_LIB(ungif, DGifOpenFileName,
    SUPPORT_LIBS="$SUPPORT_LIBS -lungif"; AC_DEFINE(HAVE_LIBGIF),
    AC_CHECK_LIB(gif, DGifOpenFileName,
      SUPPORT_LIBS="$SUPPORT_LIBS -lgif"; AC_DEFINE(HAVE_LIBGIF),
      AC_MSG_WARN(*** Native GIF support will not be built (GIF library file not found) ***), 
      $GX_LIBS),
    $GX_LIBS),
  AC_MSG_WARN(*** Native GIF support will not be built (GIF header not found) ***))

AC_CHECK_LIB(png, png_read_info,
  AC_CHECK_HEADER(png.h,
    png_ok=yes,
    png_ok=no),
  AC_MSG_WARN(*** Native PNG support will not be built (PNG library not found) ***), -lz -lm $GX_LIBS)
if test "$png_ok" = yes; then
  AC_MSG_CHECKING([for png_structp in png.h])
  AC_TRY_COMPILE([#include <png.h>],
    [png_structp pp; png_infop info; png_colorp cmap; png_create_read_struct
;],
    png_ok=yes,
    png_ok=no)
  AC_MSG_RESULT($png_ok)
  if test "$png_ok" = yes; then
    SUPPORT_LIBS="$SUPPORT_LIBS -lpng -lz"; AC_DEFINE(HAVE_LIBPNG)
  else
    AC_MSG_WARN(*** Native PNG support will not be built (PNG library is too old) ***)
  fi
else
  AC_MSG_WARN(*** Native PNG support will not be built (PNG header file not found) ***)
fi

LIBS="$SUPPORT_LIBS $LIBS -lm"

AC_SUBST(CPPFLAGS)
AC_SUBST(X_CFLAGS)
AC_SUBST(LDFLAGS)
AC_SUBST(X_PRE_LIBS)
AC_SUBST(X_LIBS)
AC_SUBST(X_EXTRA_LIBS)

AC_OUTPUT([
gdk_imlib/Makefile 
Imlib/Makefile 
utils/Makefile 
config/Makefile 
Makefile 
imlib-config 
], [chmod +x imlib-config])
