dnl Process this -*- autoconf -*- file with autoconf to produce a 
dnl configure script.
AC_INIT(dia, 0.94, http://bugzilla.gnome.org/enter_bug.cgi?product=dia)
AC_CONFIG_SRCDIR(app/diagram.c)
AM_INIT_AUTOMAKE(AC_PACKAGE_NAME,AC_PACKAGE_VERSION)

AC_PREREQ(2.50)
AC_REVISION($Revision: 1.238 $)

dnl Specify a header configuration file
AM_CONFIG_HEADER(config.h)

dnl do this check now, to keep autogen quiet.
AC_PROG_CC 
dnl also c++
AC_PROG_CXX

dnl Initialize maintainer mode
AM_MAINTAINER_MODE

dnl Initialize libtool
AC_DISABLE_STATIC
AC_PROG_LIBTOOL

dnl Build time sanity check
AM_SANITY_CHECK

dnl Checks for programs.
dnl AC_PROG_CC # already done 
AC_PROG_INSTALL
AC_PROG_LN_S
AC_PROG_MAKE_SET
AC_ISC_POSIX

dnl Checks for header files.
AC_HEADER_DIRENT
AC_HEADER_STDC
AC_CHECK_HEADERS(stddef.h fcntl.h unistd.h utime.h)

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

dnl Checks for library functions.
AC_CHECK_FUNCS(select strcspn strdup strtol snprintf)

dnl stuff for intltool
AC_PROG_INTLTOOL(0.21) 

APP_LIBS=""
AC_SUBST(APP_LIBS)


dnl Checks for libraries

AM_PATH_GLIB_2_0(2.0.0)
AM_PATH_GTK_2_0(2.0.0)

GTK_MODULES="gtk+-2.0 libxml-2.0 gdk-pixbuf-2.0"
PKG_CHECK_MODULES(PANGOVERSION, pango >= 1.1.5, , 
                  AC_MSG_ERROR([Need Pango version 1.1.5 or higher]))
PKG_CHECK_MODULES(PANGOFT2,pangoft2,have_pangoft2=true,have_pangoft2=false)
if test "$have_pangoft2" = "true"; then
  dnl On Solaris with Forte C, at least, need to link app/dia with -lfreetype.
  dnl It's not enough that -lpangoft2 implicitly pulls it in.
  have_freetype=false
  AC_CHECK_LIB(freetype,FT_Init_FreeType,have_freetype=true,have_freetype=false,`freetype-config --libs`)
  if test "$have_freetype" = "true"; then
    dnl Need 2.0.9, as a bug was fixed for us there.
    dnl However, freetype-config doesn't give a meaningful version, so we must
    dnl do it like this.
    AC_MSG_CHECKING([if FreeType version is 2.0.9 or higher])
    old_CPPFLAGS="$CPPFLAGS"
    CPPFLAGS="$CPPFLAGS `freetype-config --cflags`"
    AC_TRY_CPP([#include <ft2build.h>
#include FT_FREETYPE_H
#if (FREETYPE_MAJOR*1000+FREETYPE_MINOR)*1000+FREETYPE_PATCH < 2000009
#error Freetype version too low.
#endif
],
        [AC_MSG_RESULT(yes)
	 FREETYPE_LIBS=`freetype-config --libs`
	 AC_SUBST(FREETYPE_LIBS)
	 FREETYPE_CFLAGS=`freetype-config --cflags`
	 AC_SUBST(FREETYPE_CFLAGS)
	 GTK_MODULES="$GTK_MODULES pangoft2"
	 AC_DEFINE(HAVE_FREETYPE,1,[Define if you have the FreeType2 library])]
    ,
	[AC_MSG_ERROR([Need FreeType library version 2.0.9 or higher])])
    CPPFLAGS="$old_CPPFLAGS"
  else
   AC_MSG_ERROR(Can't find FreeType library)
  fi
else
   AC_MSG_ERROR(Can't find PangoFT2 library)
fi

CFLAGS="$FREETYPE_CFLAGS $CFLAGS"

dnl Try for libart
PKG_CHECK_MODULES(LIBART,libart-2.0,have_libart=yes,have_libart=no)
if test "$have_libart" = "yes" ; then
   GTK_MODULES="$GTK_MODULES libart-2.0"
   AC_DEFINE(HAVE_LIBART,1,[Define if building with LIBART support])
fi

AC_MSG_CHECKING([whether GNOME specific code should be used])
AC_ARG_ENABLE(gnome,[  --enable-gnome          enable gnome code],
              GNOME=$enableval, GNOME=no)
if test "$GNOME" = "yes" ; then
    GTK_MODULES="$GTK_MODULES libgnome-2.0 libgnomeui-2.0"
    AC_DEFINE(GNOME,1,[Define if building with GNOME support])
    AC_DEFINE_UNQUOTED(GNOME_ICONDIR, "${prefix}/share/pixmaps", [GNOME icon directory])
    have_gnome=true
else
    have_gnome=false
fi
AM_CONDITIONAL(HAVE_GNOME, $have_gnome)
AC_MSG_RESULT($GNOME)

dnl GTK2:     libgnomecanvas-2.0 ?

PKG_CHECK_MODULES(GTK,$GTK_MODULES)
AM_CONDITIONAL(HAVE_BONOBO,false)

dnl Look for Cairo
AC_ARG_WITH(cairo,
[  --with-cairo           compile cairo plug-in],,with_cairo=no)
if test "x$with_cairo" != "xno"; then
  PKG_CHECK_MODULES(CAIRO, cairo,have_cairo=yes,have_cairo=no)
  AC_SUBST(CAIRO_LIBS)
  AC_SUBST(CAIRO_CFLAGS)
  AC_DEFINE(HAVE_CAIRO,1,[Define if building with Cairo support])
fi
AM_CONDITIONAL(WITH_CAIRO, test "x$with_cairo" != "xno")

dnl XSLT

# Check if XSLT is available at all
XSLT_DIR=""
PKG_CHECK_MODULES(XSLT, libxslt, have_xslt=true, have_xslt=false)
AM_CONDITIONAL(HAVE_XSLT, $have_xslt)
if test "$have_xslt" = "true"; then
  XSLT_DIR="xslt"
  XSLT_LIBS="`$PKG_CONFIG --libs libxslt`"
  XSLT_CFLAGS="`$PKG_CONFIG --cflags libxslt`"
fi

AC_SUBST(XSLT_DIR)
AC_SUBST(XSLT_LIBS)
AC_SUBST(XSLT_CFLAGS)

# Figure out a prefix for where to find libxslt at runtime.
AC_ARG_WITH(xslt-prefix, 
            [  --with-xslt-prefix=DIR     Specify where to find the optional XSLT libraries
                             (default=/usr/lib)], 
	    libxslt_prefix=$withval,
	    libxslt_prefix=/usr/lib)
AC_DEFINE_UNQUOTED(LIBXSLT_PREFIX,"$libxslt_prefix", [Place to look for XSLT libs])

dnl
dnl Popt
dnl

AC_CHECK_LIB(popt, poptSetOtherOptionHelp, [
                AC_DEFINE(HAVE_LIBPOPT,,[popt library available])
		   POPT_LIBS="-lpopt"])
AC_CHECK_HEADERS(popt.h)
AC_SUBST(POPT_LIBS)
APP_LIBS="$APP_LIBS $POPT_LIBS"

dnl
dnl libpng
dnl
dnl this likely already comes from GTK

dnl Something in here pollutes $LIBS with -lpng.  Make sure to
dnl avoid that.

old_LIBS="${LIBS}"

png_ok=no
AC_CHECK_HEADER(png.h, png_ok=yes, png_ok=no)
AC_CHECK_LIB(png, png_read_info, ,png_ok=no, -lz -lm)
if test "$png_ok" = yes; then
  AC_MSG_CHECKING([for png_structp in png.h])
  dnl Why can't AC_LINK_IFELSE take extra libs?
  old_LDFLAGS="${LDFLAGS}"
  LDFLAGS="${LDFLAGS} -lz -lm"
  AC_LINK_IFELSE([#include <png.h>
                  png_structp pp;
		  png_infop info; 
		  png_colorp cmap; 
		  main() { void*foo = png_create_read_struct; }],
                 png_ok=yes,
                 png_ok=no)
  LDFLAGS="${old_LDFLAGS}"
  AC_MSG_RESULT($png_ok)
  if test "$png_ok" = yes; then
    PNG_LIBS='-lpng -lz -lm'
    AC_DEFINE(HAVE_LIBPNG,,[PNG library available])
  fi
fi
AC_SUBST(PNG_LIBS)

LIBS="${old_LIBS} ${PNG_LIBS}"

dnl
dnl Debugging
dnl
AC_ARG_ENABLE(debug, 
[  --enable-debug=[no/minimum/yes] turn on debugging [default=minimum]],
  ,
  enable_debug=minimum)

if test "x$enable_debug" = "xyes"; then
  DEBUG_FLAGS="-g -DENABLE_DEBUG"
else
  if test "x$enable_debug" = "xno"; then
    DEBUG_FLAGS=""
  else
    DEBUG_FLAGS=""
  fi
fi
AC_SUBST(DEBUG_FLAGS)

dnl
dnl i18n
dnl

GETTEXT_PACKAGE=dia
AC_SUBST(GETTEXT_PACKAGE)
AC_DEFINE_UNQUOTED(GETTEXT_PACKAGE,"$GETTEXT_PACKAGE",[Needed for glib gettext])
ALL_LINGUAS="am az be ca cs da de el en_CA en_GB es fi fr ga gu hr hu is it ja ko mk mn ml ms nl nn no pl pt pt_BR ro ru sk sl sq sr sr@Latn sv tr uk vi zh_CN zh_TW"
AM_GLIB_GNU_GETTEXT

dnl INTLOBJS doesn't seem to always get subst'ed, but I believe it's
dnl idempotent
AC_SUBST(INTLOBJS)

localedir='${prefix}/${DATADIRNAME}/locale'
AC_SUBST(localedir)

dnl
dnl Locate the gnome-xml library
dnl
dnl GTK2: remove support for libxml1. move check for libxml2 in 
dnl AM_PATH_GTK_2_0's fourth argument (module is libxml-2.0)

found_libxml=false
if test $found_libxml = false; then
  AC_CHECK_PROG(XML2_CONFIG, xml2-config, xml2-config)
  if test "x$XML2_CONFIG" != x ; then
    AC_MSG_CHECKING(for libxml >= 2.3.9)
    vers=`$XML2_CONFIG --version | sed -e "s/libxml //" | awk 'BEGIN {FS=".";} { printf "%d", ($1 * 1000 + $2) * 1000 + $3;}'`

    if test "$vers" -ge 2003009; then
      AC_MSG_RESULT(found)
      found_libxml=true
      XML_LIBS="`$XML2_CONFIG --libs`"
      XML_CFLAGS="`$XML2_CONFIG --cflags`"

      if test "$enable_gnome_print" = "yes"
      then
          gnome-config --cflags print | \
          grep gnome-xml 2>&1 >/dev/null && \
          AC_MSG_ERROR(libxml2 found but gnome-print uses libxml1 ; this is a conflict.\nPlease do either provide libxml1 or disable gnome-print.)
      fi
      echo $GNOME_CFLAGS | \
      grep gnome-xml 2>&1 >/dev/null && \
      AC_MSG_ERROR(libxml2 found but one GNOME component uses libxml1 ; this is a conflict.\nPlease do either provide libxml1 or disable the offending GNOME component.)
        
    else
      AC_MSG_RESULT(not found)
    fi
  fi
fi

if test $found_libxml = false; then
AC_CHECK_PROG(XML_CONFIG, xml-config, xml-config)
if test "x$XML_CONFIG" != x ; then
  AC_MSG_CHECKING(for libxml >= 1.8.14)
  vers=`$XML_CONFIG --version | sed -e "s/libxml //" | awk 'BEGIN {FS=".";} { printf "%d", ($1 * 1000 + $2) * 1000 + $3;}'`

  dnl 1.18.14 is required for xmlUseNewParser()    
  if test "$vers" -ge 1008014; then
    AC_MSG_RESULT(found)
    found_libxml=true
    XML_LIBS="`$XML_CONFIG --libs`"
    XML_CFLAGS="`$XML_CONFIG --cflags`"
  else
    AC_MSG_RESULT(not found)
  fi
fi
fi

if test $found_libxml = false; then
    AC_MSG_ERROR(Neither libxml1 or libxml2 found. You must install one (libxml1, preferably))
fi

AC_SUBST(XML_LIBS)
AC_SUBST(XML_CFLAGS)

CFLAGS="$XML_CFLAGS $CFLAGS"

dnl LibZ
AC_CHECK_HEADERS(zlib.h,
    AC_CHECK_LIB(z, gzread,[
        AC_DEFINE(HAVE_LIBZ,, we have the libz compression library)
        if test "x${Z_DIR}" != "x"; then
            Z_CFLAGS="-I${Z_DIR}/include"
            Z_LIBS="-L${Z_DIR}/lib -lz"
        else
            Z_LIBS="-lz"
        fi]))

AC_SUBST(Z_LIBS)

CFLAGS="${CFLAGS} ${Z_CFLAGS}"
LIBS="${LIBS} ${Z_LIBS}"

dnl Platform-specific fixes.
dnl 

dnl Sun Forte C provides isinf() in the unbundled -lsunmath. Solaris
dnl declares finite() in ieeefp.h. It's preferrable not to link 
dnl to -lsunmath as it's not present on all machines.
have_isinf=no
AC_CHECK_HEADERS(math.h,
        AC_CHECK_LIB(m,isinf,
                have_isinf=yes))

if test "$have_isinf" = "no"; then
    AC_CHECK_HEADER(ieeefp.h,
             [AC_CHECK_FUNC(finite, 
	               AC_DEFINE(HAVE_IEEEFP_H,1,
		                 [Define if finite() is defined in ieeefp.h]),
                       AC_MSG_ERROR([Can't find a definition of neither 
                                     finite nor isinf]))],
             [AC_MSG_ERROR(Can't find neither a definition of
	                   isinf nor ieeefp.h)])
else
    AC_DEFINE([HAVE_ISINF], 1, 
              [Define if the isinf() function is available])
fi

dnl Alpha needs -mieee or we get a segfault
dnl This shouldn't be needed but is here until a real solution is found
case "$host" in
  alpha*-*-linux*)
    CFLAGS="$CFLAGS -mieee"
    ;;
esac

if test "$GCC" = yes; then
  dnl Relieve register pressure on i386. Other architectures may become unable
  dnl to debug, and usually have more registers anyway.
  dnl case "$host" in 
  dnl  i?86-pc-*)
  dnl    DIA_CHECK_CFLAG(-fomit-frame-pointer)
  dnl    ;;
  dnl esac
  dnl (it looks like gdb is very confused by this now even on x86. We might be
  dnl getting poor stack traces with bug-buddy because of this)

  DIA_CHECK_CFLAG(-Wall)
  DIA_CHECK_CFLAG(-Wunused)
  dnl Premature optimization is the root of all evil.  Uncomment this if
  dnl you want ti figure out functions that should have been inlined but aint.
  dnl DIA_CHECK_CFLAG(-Winline)
  DIA_CHECK_CFLAG(-Wmissing-prototypes)
  DIA_CHECK_CFLAG(-Wmissing-declarations)
  DIA_CHECK_CFLAG(-finline-functions)
  DIA_CHECK_CFLAG(-fstrict-aliasing)
fi

AM_CONDITIONAL(HAVE_GCC, test "$GCC" = yes)

dnl Do we have the db2html script to build the documentation?
AC_ARG_ENABLE(db2html,
[  --enable-db2html        don't rebuild documentation],,enable_db2html=no)
AC_PATH_PROG(DB2HTML, db2html, no)
AM_CONDITIONAL(HAVE_DB2HTML, 
               test "x$enable_db2html" != xno && test "x$DB2HTML" != xno)

dnl Only enable man page generation if we have db2man (in maintainer mode)

dnl Have to have the nwalsh docbook.xsl, which does manpages.
dnl RedHat installs it in a version-numbered place.  Grumpf.
db2man_file=
for file in \
  /usr/share/sgml/docbook/stylesheet/xsl/nwalsh/manpages/docbook.xsl \
  /usr/share/sgml/docbook/xsl-stylesheets-\*/manpages/docbook.xsl; do
  if test -f $file; then db2man_file=$file; break; fi
done
AM_CONDITIONAL(HAVE_DB2MAN, test "x$db2man_file" != x)
AC_SUBST(DB2MAN, "$db2man_file")

dnl Handling for Python
AC_ARG_WITH(python,
[  --with-python           compile python plug-in],,with_python=no)

if test "x$with_python" != xno; then
  AM_PATH_PYTHON(1.5.2)
  AM_CHECK_PYMOD(gtk,,,[AC_MSG_ERROR(could not find pygtk module)])
  AM_CHECK_PYTHON_HEADERS
  AM_CHECK_PYTHON_LIB

  if test -z "$PYTHON_INCLUDES" || test -z "$PYTHON_LIBS"; then
    AC_MSG_ERROR([could not find files required to build python plugin])
  fi
fi
AM_CONDITIONAL(WITH_PYTHON, test "x$with_python" != "xno")

AC_OUTPUT(
Makefile
lib/Makefile
lib/pixmaps/Makefile
po/Makefile.in
objects/Makefile
dnl objects/sybase/Makefile
objects/bondgraph/Makefile
objects/chronogram/Makefile
objects/custom/Makefile
objects/ER/Makefile
objects/flowchart/Makefile
objects/FS/Makefile
objects/GRAFCET/Makefile
objects/Istar/Makefile
objects/Jackson/Makefile
objects/KAOS/Makefile
objects/Misc/Makefile
objects/network/Makefile
objects/SADT/Makefile
objects/standard/Makefile
objects/UML/Makefile
shapes/Makefile
shapes/Assorted/Makefile
shapes/ChemEng/Makefile
shapes/Circuit/Makefile
shapes/Cisco/Makefile
shapes/Civil/Makefile
shapes/Contact/Makefile
shapes/Cybernetics/Makefile
shapes/Electric/Makefile
shapes/flowchart/Makefile
shapes/jigsaw/Makefile
shapes/Logic/Makefile
shapes/Map/Makefile
shapes/Map/Isometric/Makefile
shapes/Misc/Makefile
shapes/MSE/Makefile
shapes/network/Makefile
shapes/Pneumatic/Makefile
shapes/RDP/Makefile
shapes/SDL/Makefile
shapes/sybase/Makefile
app/Makefile
app/pixmaps/Makefile
samples/Makefile
sheets/Makefile
sheets/ER/Makefile
sheets/GRAFCET/Makefile
sheets/Istar/Makefile
sheets/Jackson/Makefile
sheets/KAOS/Makefile
sheets/UML/Makefile
plug-ins/Makefile
plug-ins/cgm/Makefile
plug-ins/cairo/Makefile
plug-ins/dxf/Makefile
plug-ins/hpgl/Makefile
plug-ins/metapost/Makefile
plug-ins/pixbuf/Makefile
plug-ins/pstricks/Makefile
plug-ins/python/Makefile
plug-ins/shape/Makefile
plug-ins/svg/Makefile
plug-ins/wmf/Makefile
plug-ins/wpg/Makefile
plug-ins/xfig/Makefile
plug-ins/xslt/Makefile
doc/Makefile
doc/en/Makefile
doc/pl/Makefile
)

dnl GTK2: simplify this, we will have much less options.
echo "

Configuration:
	Source code location:	${srcdir}
	Compiler:		${CC}

	Gnome support:				${GNOME}
	Python support:				${with_python}
	Libart support (PNG export):		${have_libart}
	Cairo support (experimental):		${have_cairo}
"

echo "Now type make to build dia..."
