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

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

AM_CONFIG_HEADER(config.h)

AM_INIT_AUTOMAKE(imlib, 1.9.11)
AC_PROG_CC
AM_PROG_LIBTOOL

dnl Initialize maintainer mode
AM_MAINTAINER_MODE

dnl Give the user an option to compile without shared memory support,
dnl incase it is broken for example.
AC_ARG_ENABLE(shm, [  --enable-shm            support shared memory if available [default=yes]], echo $enable_shm, enable_shm="yes")

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

AC_MSG_CHECKING(whether to build gmodulized imlib)

AC_ARG_ENABLE(modules, [  --disable-modules       Disables dynamic module loading],[
	if test x$withval = xyes; then 
	    with_modules=yes
        else
            with_modules=no
	fi
])

dynworks=false
if test x$with_modules = xno; then
	AC_MSG_RESULT(no)
else 
	AC_MSG_RESULT(yes)

if test "x$GDK_IMLIB" != x; then
  oLIBS="$LIBS"
  oCFLAGS="$CFLAGS"
  LIBS="$LIBS `glib-config --libs gmodule`"
  CFLAGS="$CFLAGS `glib-config --cflags gmodule`"
  AC_TRY_RUN([
#include <glib.h>
#include <gmodule.h>
main ()
{
    if (g_module_supported ())
        exit (0);
    else
        exit (1);
}
], dynworks=true)
  LIBS="$oLIBS"
  CFLAGS="$oCFLAGS"
fi

dnl Now we check to see if our libtool supports shared lib deps
dnl (in a rather ugly way even)
if $dynworks; then
   imlib_libtool_config="libtool --config"
   imlib_deplibs_check=`$imlib_libtool_config | \
      grep '^[[a-z_]]*check[[a-z_]]*_method=[['\''"]]' | \
      sed 's/.*[['\''"]]\(.*\)[['\''"]]$/\1/'`
   if test "x$imlib_deplibs_check" = "xnone" || \
      test "x$imlib_deplibs_check" = "xunknown" || \
      test "x$imlib_deplibs_check" = "x"; then
      dynworks=false
   fi
fi

if $dynworks; then
   AC_DEFINE(USE_GMODULE)
   GMODULE_LIBS="`glib-config --libs gmodule`"
   GMODULE_FLAGS="`glib-config --cflags gmodule`"
   AC_SUBST(GMODULE_LIBS)
   AC_SUBST(GMODULE_FLAGS)
fi

fi

AC_SUBST(TIFFLIBS)
AC_SUBST(GIFLIBS)
AC_SUBST(JPEGLIBS)
AC_SUBST(PNGLIBS)
	

AC_SUBST(GDK_IMLIB)
AM_CONDITIONAL(DYNAMIC_MODULES_WORK, $dynworks)
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_CHECK_FUNCS(vsnprintf)
AC_CHECK_FUNCS(snprintf)
AC_CHECK_FUNCS(basename)

AC_CHECK_FUNCS(blumfrub)
AC_CHECK_FUNCS(buckets_of_erogenous_nym)
AC_CHECK_FUNCS(buttox)

AC_CHECK_HEADER(stdarg.h, AC_DEFINE(HAVE_STDARGS))

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

AC_PATH_X
AC_PATH_XTRA

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

dnl If we are compiling without shared memory support, then don\'t do
dnl the checks for XShm
if test "x$enable_shm" = "xyes"; then
  # Check for the Xext library (needed for XShm extension)
  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_xshm_ext=yes, $X_LIBS),
      $X_LIBS)
fi

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 -lm"
else
  CPPFLAGS="$CPPFLAGS $GTK_CFLAGS"
  GX_LIBS="$GTK_LIBS"
fi

CPPFLAGS="$CPPFLAGS -I$includedir -I$prefix/include"

SUPPORT_LIBS=""

dnl Test for shared memory headers and semantics, unless we are
dnl configured to build without shared memory extensions.
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)

  if test "x$no_sys_ipc" != "xyes" \
     && test "x$no_sys_shm" != "xyes" \
     && test "x$no_xshm_ext" != "xyes" ; then
    AC_DEFINE(HAVE_SHM)
  fi

  # 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
    # If we couldn't find XShmAttach earlier, don't use the header
    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

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 jpeglib.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 
    JPEGLIBS="-ljpeg"
    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,
    TIFFLIBS="-ltiff"
    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,
      TIFFLIBS="-ltiff -ljpeg -lz"
      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,
        TIFFLIBS="-ltiff34 -ljpeg -lz"
        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,
    GIFLIBS="-lungif"
    SUPPORT_LIBS="$SUPPORT_LIBS -lungif"; AC_DEFINE(HAVE_LIBGIF),
    AC_CHECK_LIB(gif, DGifOpenFileName,
      GIFLIBS="-lgif"
      SUPPORT_LIBS="$SUPPORT_LIBS -lgif"
      AC_DEFINE(HAVE_LIBGIF),
      AC_SUBST(GIFLIBS)
      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_set_IHDR;],
    png_ok=yes,
    png_ok=no)
  AC_MSG_RESULT($png_ok)
  if test "$png_ok" = no; then
    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

dnl If we HAVE libpng at all, we make this check :)
#if test "$png_ok" = yes; then
#    dnl Now we check for libpng 1.0.2.  Not the most elegant way, but hey...this is RasterCode!
#    AC_MSG_CHECKING([for libpng version other than 1.0.2])
#    AC_EGREP_CPP("1.0.2",[#include <png.h>
#    PNG_LIBPNG_VER_STRING],echo "********* WARNING *********** Your libpng version is 1.0.2. Build will not continue until you revert to 1.0.1 or a newer version.";exit,png_ok=yes)
#    AC_MSG_RESULT($png_ok)
#fi

if test "$png_ok" = yes; then
	PNGLIBS="-lpng -lz"
	AC_SUBST(HAVE_LIBPNG)
	SUPPORT_LIBS="$SUPPORT_LIBS -lpng -lz"; AC_DEFINE(HAVE_LIBPNG)
else
	AC_MSG_WARN(*** Native PNG support will NOT be built (PNG version us 1.0.2, get 1.0.3) ***)
fi

SUPPORT_LIBS="$SUPPORT_LIBS -lm"
GDK_SUPPORT_LIBS="$SUPPORT_LIBS"

if test $dynworks = false; then
   GIFLIBS=
   TIFFLIBS=
   PNGLIBS=
   JPEGLIBS=
else
   GDK_SUPPORT_LIBS=
fi

AM_CONDITIONAL(HAVE_GIF, test "x$GIFLIBS" != x)
AM_CONDITIONAL(HAVE_TIFF, test "x$TIFFLIBS" != x)
AM_CONDITIONAL(HAVE_PNG, test "x$PNGLIBS" != x)
AM_CONDITIONAL(HAVE_JPEG, test "x$JPEGLIBS" != x)

AC_SUBST(SUPPORT_LIBS)
AC_SUBST(GDK_SUPPORT_LIBS)
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([
imlib.spec
gdk_imlib/Makefile 
Imlib/Makefile 
utils/Makefile 
config/Makefile 
doc/Makefile 
Makefile 
imlib-config 
imlib.pc
imlibgdk.pc
], [
case "$CONFIG_FILES" in
 *imlib-config*)
    chmod +x imlib-config
    ;;
esac])
