AC_INIT(README)
AM_CONFIG_HEADER(config.h)
AM_INIT_AUTOMAKE(gal,0.15)

AM_MAINTAINER_MODE
AM_ACLOCAL_INCLUDE(macros)

dnl libtool versioning

dnl gal-0.15 = 0
dnl increment if any interfaces have been added; set to 0
dnl  if any interfaces have been removed. removal has 
dnl  precedence over adding, so set to 0 if both happened.
dnl AKA Micro
GAL_AGE=0

dnl gal-0.15 = 0
dnl increment any time the source changes; set to 
dnl  0 if you increment CURRENT
dnl AKA Minor
GAL_REVISION=0

dnl gal-0.15 = 15
dnl increment if the interface has additions, changes, removals.
dnl AKA Major
GAL_CURRENT=15

AC_SUBST(GAL_CURRENT)
AC_SUBST(GAL_REVISION)
AC_SUBST(GAL_AGE)

AC_ISC_POSIX
AC_PROG_CC
AC_PROG_YACC
AM_PROG_LEX
AC_STDC_HEADERS
AC_ARG_PROGRAM
AM_PROG_LIBTOOL

AM_PROG_XML_I18N_TOOLS

GNOME_INIT

## this should come after `AC_PROG_CC'
GNOME_COMPILE_WARNINGS
GNOME_X_CHECKS
GNOME_XML_CHECK

AC_DEFINE_UNQUOTED(GNOME_EXPLICIT_TRANSLATION_DOMAIN, "gal")

dnl
dnl alloca tests
dnl;
AC_FUNC_ALLOCA
if test $ac_cv_header_alloca_h = yes; then
        GAL_HAVE_ALLOCA_H=1
else
        GAL_HAVE_ALLOCA_H=0
fi
AC_SUBST(GAL_HAVE_ALLOCA_H)

AC_SUBST(CFLAGS)
AC_SUBST(CPPFLAGS)
AC_SUBST(LDFLAGS)

ALL_LINGUAS="az ca cs da de en_GB es fi fr gl hu it ja ko lt nn no pl pt pt_BR ro ru sk sv ta tr uk zh_TW"
AM_GNOME_GETTEXT

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

dnl ******************************
dnl gnome-xml checking
dnl ******************************
AC_MSG_CHECKING(for libxml (aka gnome-xml) libraries 1.8.8 <= version < 2.0.0)
if xml-config --libs > /dev/null 2>&1; then 
    vers=`xml-config --version | sed -e "s/libxml //" | awk 'BEGIN { FS = "."; } { printf "%d", ($1 * 1000 + $2) * 1000 + $3;}'`
    if test "$vers" -ge 1008008; then
	if test "$vers" -lt 2000000; then
	    AC_MSG_RESULT(found)
	else
	    AC_MSG_ERROR(This version of GAL does not support libxml (aka gnome-xml) 2.x.x yet.)
	fi
    else
        AC_MSG_ERROR(You need at least libxml (aka gnome-xml) 1.8.8 for this version of GAL.)
    fi
else
    AC_MSG_ERROR(Did not find libxml (aka gnome-xml) installed)
fi

dnl ******************************
dnl iconv checking
dnl ******************************
have_iconv=no
AC_ARG_WITH(libiconv, [  --with-libiconv         Prefix where libiconv is installed])
case $withval in
/*)
    ICONV_CFLAGS="-I$withval/include"
    ICONV_LIBS="-L$withval/lib"
    ;;
esac

save_CFLAGS="$CFLAGS"
save_LDFLAGS="$LDFLAGS"
CFLAGS="$CFLAGS $ICONV_CFLAGS"
LDFLAGS="$LDFLAGS $ICONV_LIBS -liconv"
AC_CACHE_CHECK(for iconv in -liconv, ac_cv_libiconv, AC_TRY_LINK([
#include <iconv.h>
], [
	iconv_t cd;
	cd = iconv_open ("UTF-8", "ISO-8859-1");
], ac_cv_libiconv=yes, ac_cv_libiconv=no))
if test $ac_cv_libiconv = yes; then
	ICONV_LIBS="$ICONV_LIBS -liconv"
	have_iconv=yes
else
	CFLAGS="$save_CFLAGS"
	LDFLAGS="$save_LDFLAGS"
	AC_CHECK_FUNC(iconv, have_iconv=yes, have_iconv=no)
fi

if test $have_iconv = yes; then
	if test $ac_cv_libiconv = no; then
		AC_CHECK_FUNCS(gnu_get_libc_version)
	fi
	AC_CACHE_CHECK([if iconv() handles UTF-8], ac_cv_libiconv_utf8, AC_TRY_RUN([
#include <iconv.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_GNU_GET_LIBC_VERSION
#include <gnu/libc-version.h>
#endif

int main (int argc, char **argv)
{
	const char *jp = "\x1B\x24\x42\x46\x7C\x4B\x5C\x38\x6C";
	const char *utf8 = "\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E";
	char *transbuf = malloc (10), *trans = transbuf;
	iconv_t cd;
	size_t jp_len = strlen (jp), utf8_len = 10;
	size_t utf8_real_len = strlen (utf8);

#ifdef HAVE_GNU_GET_LIBC_VERSION
	/* glibc 2.1.2's iconv is broken in hard to test ways. */
	if (!strcmp (gnu_get_libc_version (), "2.1.2"))
		exit (1);
#endif

	cd = iconv_open ("UTF-8", "ISO-2022-JP");
	if (!cd)
		exit (1);
	if (iconv (cd, &jp, &jp_len, &trans, &utf8_len) == -1 || jp_len != 0)
		exit (1);
	if (memcmp (utf8, transbuf, utf8_real_len) != 0)
		exit (1);

	exit (0);
}], ac_cv_libiconv_utf8=yes, [ac_cv_libiconv_utf8=no; have_iconv=no], [ac_cv_libiconv_utf8=no; have_iconv=no]))
fi
CFLAGS="$save_CFLAGS"
LDFLAGS="$save_LDFLAGS"

if test "$have_iconv" = no; then
	AC_MSG_ERROR([You need to install a working iconv implementation, such as http://clisp.cons.org/~haible/packages-libiconv.html])
fi
AC_SUBST(ICONV_CFLAGS)
AC_SUBST(ICONV_LIBS)

dnl Check for nl_langinfo and CODESET

AC_MSG_CHECKING([for nl_langinfo (CODESET)])
AC_TRY_COMPILE([#include <langinfo.h>],
	[char *codeset = nl_langinfo (CODESET);],
   AC_DEFINE(HAVE_CODESET,1,[Have nl_langinfo (CODESET)])
   have_codeset=yes,
   have_codeset=no)
AC_MSG_RESULT($have_codeset)

dnl ******************************
dnl GnomePrint checking
dnl ******************************
AC_MSG_CHECKING(for GnomePrint libraries >= 0.22)
if gnome-config --libs print > /dev/null 2>&1; then 
    vers=`gnome-config --modversion print | sed -e "s/gnome-print-//" -e 's/cvs$//' -e 's/pre$//' | \
        awk 'BEGIN { FS = "."; } { print $1 * 1000 + $2;}'`
    if test "$vers" -ge 21; then
        AC_MSG_RESULT(found)
    else
        AC_MSG_ERROR(You need at least GNOME print 0.21pre for this version of GAL)
    fi
else
    AC_MSG_ERROR(Did not find GnomePrint installed)
fi

dnl ******************************
dnl LibGlade checking
dnl ******************************
AC_MSG_CHECKING(for Glade libraries >= 0.13)
if gnome-config --libs libglade > /dev/null 2>&1; then 
    vers=`gnome-config --modversion libglade | awk 'BEGIN { FS = "."; } { print $1 * 1000 + $2;}'`
    if test "$vers" -ge 13; then
        AC_MSG_RESULT(found)
    else
        AC_MSG_ERROR(You need at least libglade 0.13 for this version of GAL)
    fi
else
    AC_MSG_ERROR(Did not find libGlade installed)
fi

dnl ******************************
dnl gtk+ checking
dnl ******************************
AC_MSG_CHECKING(for GTK >= 1.2.7)
if $GTK_CONFIG --version > /dev/null 2>&1; then 
    dnl We need the "%d" in order not to get e-notation on hpux.
    vers=`"$GTK_CONFIG" --version | awk 'BEGIN { FS = "."; } { printf "%d", ($1 * 1000 + $2) * 1000 + $3;}'`
    if test "$vers" -ge 1002007; then
        AC_MSG_RESULT(found)
    else
        AC_MSG_ERROR(You need at least GTK+ 1.2.7 for this version of GAL)
    fi
else
    AC_MSG_ERROR(Did not find GTK+ installed)
fi

dnl ***************************************

dnl ******************************
dnl Gnome-VFS checking
dnl ******************************
AC_MSG_CHECKING(for GNOME-VFS)
if gnome-config --libs vfs > /dev/null 2>&1; then
	vers=`gnome-config --modversion vfs`
	case $vers
	in
	    gnome-vfs-0.[012]) vfs_ok=false ;;
	    *) vfs_ok=true ;;
	esac
else
	vfs_ok=false
fi

if $vfs_ok; then
	AC_MSG_RESULT($vers)

	GNOME_VFS_LIBS="`gnome-config --libs vfs`"
	GNOME_VFS_CFLAGS="`gnome-config --cflags vfs`"
	AC_SUBST(GNOME_VFS_LIBS)
	AC_SUBST(GNOME_VFS_CFLAGS)

	have_vfs=true
else
	AC_MSG_RESULT(Gnome-VFS not found.  Not compiling test-tree-2.)
	have_vfs=false
fi
AM_CONDITIONAL(GAL_HAVE_VFS,$have_vfs)

dnl
dnl Gtk-doc
dnl
AC_ARG_ENABLE(gtk-doc, [  --enable-gtk-doc  Use gtk-doc to build documentation [default=auto]], enable_gtk_doc="$enableval", enable_gtk_doc=auto)

if test x$enable_gtk_doc = xauto ; then
  if test x$GTKDOC = xtrue ; then
    enable_gtk_doc=yes
  else
    enable_gtk_doc=no
  fi
fi
dnl NOTE: We need to use a separate automake conditional for this
dnl       to make this work with the tarballs.
AM_CONDITIONAL(ENABLE_GTK_DOC, test x$enable_gtk_doc = xyes)

AC_ARG_WITH(html-dir, [  --with-html-dir=PATH path to installed docs ])
if test "x$with_html_dir" = "x" ; then
  HTML_DIR='${datadir}/gal/html'
else
  HTML_DIR=$with_html_dir
fi

AC_SUBST(HTML_DIR)

dnl
dnl For the various galConf.sh scripts generation.
dnl
GAL_LIBDIR='-L${libdir}'
GAL_INCLUDEDIR=" -I${includedir} `$GNOME_CONFIG --cflags gnomeui gdk_pixbuf print libglade gnomecanvaspixbuf` $ICONV_CFLAGS"
GAL_LIBS="-Wl,-R${libdir} -lgal `$GNOME_CONFIG --libs gnomeui gdk_pixbuf print libglade gnomecanvaspixbuf` $ICONV_LIBS"
AC_SUBST(GAL_LIBDIR)
AC_SUBST(GAL_INCLUDEDIR)
AC_SUBST(GAL_LIBS)

EXTRA_GNOME_LIBS=`gnome-config --libs gnomeui print libart libglade gdk_pixbuf gnomecanvaspixbuf`
EXTRA_GNOME_CFLAGS=`gnome-config --cflags gnomeui print libart libglade gdk_pixbuf gnomecanvaspixbuf`
AC_SUBST(EXTRA_GNOME_LIBS)
AC_SUBST(EXTRA_GNOME_CFLAGS)

AC_OUTPUT([
Makefile
intl/Makefile
po/Makefile.in
macros/Makefile
gal/Makefile
gal/util/Makefile
gal/unicode/Makefile
gal/widgets/Makefile
gal/e-text/Makefile
gal/e-paned/Makefile
gal/e-table/Makefile
gal/shortcut-bar/Makefile
gal/menus/Makefile
gal/shortcut-bar/Makefile
tests/Makefile
docs/Makefile
art/Makefile
gal.spec
stamp.h
],[sed -e "/POTFILES =/r po/POTFILES" po/Makefile.in > po/Makefile])
