-*- mode: m4 -*-
dnl We require Automake 1.7.2, which requires Autoconf 2.54.
dnl (It needs _AC_AM_CONFIG_HEADER_HOOK, for example.)
AC_PREREQ(2.54)

m4_define([goffice_version_epoch], [0])
m4_define([goffice_version_major], [0])
m4_define([goffice_version_minor], [4])
m4_define([goffice_version_extra], [])
m4_define([goffice_full_version],
    [goffice_version_epoch.goffice_version_major.goffice_version_minor[]goffice_version_extra])

dnl Emphasize some of the checks.
m4_define([BIG_CHECKING], [AC_MSG_CHECKING([
======== $1])])

AC_INIT([goffice], [goffice_full_version],
	[http://bugzilla.gnome.org/enter_bug.cgi?product=libgoffice])

AC_CONFIG_SRCDIR(goffice/goffice.h)
AM_INIT_AUTOMAKE([dist-bzip2])

dnl Version info for libraries = CURRENT:REVISION:AGE
dnl
dnl FIXME: This formula is wrong.  --kasal
dnl
AC_SUBST(GOFFICE_LIB_VERSION,
m4_eval(goffice_version_epoch + goffice_version_major)[]dnl
:goffice_version_minor:goffice_version_major)

dnl This one is created by autoheader, ...
AC_CONFIG_HEADERS(goffice/goffice-config.h)

dnl ... and this one is a small subset, maintained manually,
dnl which will be installed.
AC_CONFIG_HEADERS(goffice/goffice-features.h)
dnl
dnl Make sure these two won't clash.  Put the following to goffice-config.h:
AH_BOTTOM([/* Don't include goffice-features.h, it's a subset of goffice-config.h. */
#define GOFFICE_FEATURES_H])

AM_MAINTAINER_MODE

# Make --disable-static the default
AC_DISABLE_STATIC

dnl We need intltool >= 0.27.2 to extract the UTF-8 chars from source code
AC_PROG_INTLTOOL([0.27.2])

dnl We use $host_os:
AC_CANONICAL_HOST

AC_ISC_POSIX
AC_PROG_CC
AC_PROG_YACC
AM_PROG_LEX
AC_PROG_LN_S
AM_PROG_LIBTOOL
AC_STDC_HEADERS
AC_SYS_LARGEFILE

dnl Propagate Gnome-specific variable ACLOCAL_FLAGS to Makefile.
AC_SUBST(ACLOCAL_AMFLAGS, $ACLOCAL_FLAGS)

dnl The first call to PKG_CHECK_MODULES is inside an `if.'
dnl We have to call PKG_PROG_PKG_CONFIG explicitly; otherwise it would
dnl get expanded just before the first occurence of PKG_CHECK_MODULES
dnl and might escape execution.
PKG_PROG_PKG_CONFIG

dnl *****************************
goffice_reqs="
	glib-2.0		>= 2.4.8
	gobject-2.0		>= 2.4.8
	gmodule-2.0		>= 2.4.8
	libgsf-1		>= 1.12.2
	libxml-2.0		>= 2.4.12
	pango			>= 1.6.0
	pangoft2		>= 1.6.0
"
goffice_cairo_reqs="
	cairo			>= 0.5.0
"
goffice_gtk_reqs="
	gtk+-2.0		>= 2.4.14
	libglade-2.0		>= 2.3.6
	libgnomeprint-2.2	>= 2.8.2
	libart-2.0		>= 2.3.11
"
goffice_gnome_reqs="
	gconf-2.0
	libgnomeui-2.0		>= 2.0.0
	libgsf-gnome-1		>= 1.12.2
"

dnl *********************
dnl Should we use cairo ?
dnl *********************
goffice_with_cairo=false
AC_ARG_WITH(cairo, [  --{with,without}-cairo  Use cairo for graph rendering])
if test "x$with_cairo" = xyes; then
	cairo_msg="Enabled"
	PKG_CHECK_MODULES(CAIRO, [$goffice_cairo_reqs])
	AC_DEFINE(WITH_CAIRO, 1, [Define if goffice is build with cairo])
	goffice_reqs="$goffice_reqs $goffice_cairo_reqs"
	goffice_with_cairo=true
else
	cairo_msg="Disabled"
	goffice_with_cairo=false
fi 

dnl *******************
dnl Should we use gtk ?
dnl *******************
goffice_with_gtk=true
AC_ARG_WITH(gtk, [  --without-gtk           Build without UI])
if test "x$with_gtk" = xno; then
	ui_msg="None (Gtk disabled by request)"
	goffice_with_gtk=false
else
	dnl We shouldn't silently default to --without-gtk.
	dnl If the requirements are not met, fail.
	ui_msg="Gtk"
	PKG_CHECK_MODULES(GTK, [$goffice_gtk_reqs])
fi 

goffice_with_gnome=$goffice_with_gtk
if test "x$goffice_with_gtk" = "xtrue" ; then
	AC_DEFINE(WITH_GTK, 1, [Define if UI is built])
	goffice_reqs="$goffice_reqs $goffice_gtk_reqs"

	dnl ************************************
	dnl Are the GNOME extensions available ?
	dnl ************************************

	goffice_with_gnome=true
	AC_ARG_WITH(gnome,
		[  --{with,without}-gnome  Use GNOME extensions],
		[if test "x$withval" = xno; then
			goffice_with_gnome=false
			ui_msg="Gtk+ (Gnome disabled by request)"
		fi]
	)
	if test "x$goffice_with_gnome" = "xtrue"; then
		PKG_CHECK_MODULES(GNOME, [$goffice_gnome_reqs],
			[ui_msg="Gnome"],
			[ui_msg="Gtk (missing gnome dependencies)" ; goffice_with_gnome=false])
	fi

	if test "x$goffice_with_gnome" = "xtrue"; then
		AC_DEFINE(WITH_GNOME, 1, [Define if GNOME extensions are available])
		goffice_reqs="$goffice_reqs $goffice_gnome_reqs"
	fi
fi
AM_CONDITIONAL(WITH_CAIRO, $goffice_with_cairo)
AM_CONDITIONAL(WITH_GTK,   $goffice_with_gtk)
AM_CONDITIONAL(WITH_GNOME, $goffice_with_gnome)

dnl ****************************
dnl now that we have selected out libraries the whole collection in one
dnl shot so that we can have a nice neat compile/link line
dnl ****************************

PKG_CHECK_MODULES(GOFFICE, $goffice_reqs)

dnl  Checks for Xft/XRender
AC_CHECK_LIB(Xrender, XRenderFindFormat, 
	[AC_SUBST(RENDER_LIBS, "-lXrender -lXext")
	 AC_DEFINE(HAVE_RENDER, 1, [Define if libXrender is available.])],
	[AC_SUBST(RENDER_LIBS, "")],
	[-lXext])

dnl *****************************
AC_MSG_CHECKING([for Win32 platform])
with_win32=no
case $host_os in
  mingw* | pw32* | cygwin*)
    with_win32=yes
    GOFFICE_PLUGIN_LDFLAGS="-Wl,--enable-runtime-pseudo-relo,--export-all-symbols $GOFFICE_PLUGIN_LDFLAGS"
    AC_ARG_VAR(WINDRES, [The windres executable (used by win32 builds only).])
    AC_CHECK_TOOL(WINDRES, windres, :)
    ;;
esac
AC_MSG_RESULT($with_win32)
AM_CONDITIONAL(WITH_WIN32, test $with_win32 = yes)

dnl *****************************
dnl FIXME: perhaps declare with AC_ARG_VAR?
dnl	   If we use the initial value of a variable, we have to make it precious.
dnl
GOFFICE_PLUGIN_LDFLAGS="-avoid-version -no-undefined $GOFFICE_PLUGIN_LDFLAGS"
GOFFICE_PLUGIN_LIBADD="\$(top_builddir)/goffice/libgoffice-1.la $GOFFICE_LIBS $GOFFICE_PLUGIN_LIBADD"
AC_SUBST(GOFFICE_PLUGIN_LDFLAGS)
AC_SUBST(GOFFICE_PLUGIN_LIBADD)

dnl disable for in stable release, re-enable for development series
CFLAGS="$CFLAGS -DG_DISABLE_DEPRECATED"
CFLAGS="$CFLAGS -DPANGO_DISABLE_DEPRECATED"
if test "x$goffice_with_gtk" = "xtrue"; then
	CFLAGS="$CFLAGS -DGDK_PIXBUF_DISABLE_DEPRECATED"
	CFLAGS="$CFLAGS -DGDK_DISABLE_DEPRECATED"
	CFLAGS="$CFLAGS -DGDK_MULTIHEAD_SAFE"
	CFLAGS="$CFLAGS -DGTK_DISABLE_DEPRECATED"
	CFLAGS="$CFLAGS -DLIBGLADE_DISABLE_DEPRECATED"
fi
if test "x$goffice_with_gnome" = "xtrue"; then
	CFLAGS="$CFLAGS -DGNOME_DISABLE_DEPRECATED"
	CFLAGS="$CFLAGS -DBONOBO_DISABLE_DEPRECATED"
	CFLAGS="$CFLAGS -DBONOBO_UI_DISABLE_DEPRECATED"
fi

dnl ****************************
dnl prep the pixmap generator
dnl ****************************

AC_ARG_VAR(GLIB_GENMARSHAL, [The glib-genmarshal executable.])
AC_CHECK_PROG(GLIB_GENMARSHAL, glib-genmarshal, glib-genmarshal)

AC_ARG_VAR(GDK_PIXBUF_CSOURCE, [The gdk-pixbuf-csource executable.])
AC_CHECK_PROG(GDK_PIXBUF_CSOURCE, gdk-pixbuf-csource, gdk-pixbuf-csource)

## this should come after `AC_PROG_CC'
ifdef([GNOME_COMPILE_WARNINGS],
	[GNOME_COMPILE_WARNINGS] CFLAGS="$CFLAGS $WARN_CFLAGS",
	[]
)
set_more_warnings=yes
if test "$GCC" = "yes" -a "x$set_more_warnings" != "xno"; then
	for option in -Wsign-compare -Wpointer-arith -Wnested-externs -Wchar-subscripts -Wwrite-strings -Wdeclaration-after-statement -Wmissing-noreturn -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wmissing-format-attribute; do
		SAVE_CFLAGS="$CFLAGS"
		CFLAGS="$CFLAGS $option"
		AC_MSG_CHECKING([whether gcc understands $option])
		AC_TRY_COMPILE([], [],
			has_option=yes,
			has_option=no,)
		if test $has_option = no; then
		  CFLAGS="$SAVE_CFLAGS"
		fi
		AC_MSG_RESULT($has_option)
		unset has_option
		unset SAVE_CFLAGS
	done
	unset option
fi
AC_SUBST(WARN_CFLAGS)

AC_CHECK_DECL(fdopen, fdopen_works=yes, fdopen_works=no)
if test $fdopen_works = no ; then
	unset ac_cv_have_decl_fdopen
	CFLAGS="$CFLAGS -D_POSIX_SOURCE"
	AC_MSG_NOTICE([adding -D_POSIX_SOURCE to CFLAGS])
	AC_CHECK_DECL(fdopen, fdopen_works=yes, fdopen_works=no)
	if test $fdopen_works = no ; then
		AC_MSG_ERROR([fdopen is not available])
	fi
fi

# Unfortunately, -D_POSIX_SOURCE turns off struct timeval on Solaris
AC_MSG_CHECKING([whether struct timeval is available])
AC_TRY_COMPILE([#include <sys/time.h>], [struct timeval tv;], struct_timeval_works=yes, struct_timeval_works=no)
AC_MSG_RESULT($struct_timeval_works)

if test $struct_timeval_works = no ; then
	CFLAGS="$CFLAGS -D__EXTENSIONS__"
	AC_MSG_CHECKING([whether struct timeval is available with -D__EXTENSIONS__])
	AC_TRY_COMPILE([#include <sys/time.h>], [struct timeval tv;], struct_timeval_works=yes, struct_timeval_works=no)
	AC_MSG_RESULT($struct_timeval_works)
	if test $struct_timeval_works = no ; then
		AC_MSG_ERROR([struct timeval is not available])
	fi
fi

AC_CHECK_FUNC(gettimeofday,
	[AC_DEFINE(HAVE_GETTIMEOFDAY, 1,
		[Define if the gettimeofday function is available]
	)])

dnl M_PI
AC_MSG_CHECKING([whether M_PI is available])
AC_TRY_COMPILE([#include <math.h>], [double f = M_PI], works_without_bsd_source=yes, works_without_bsd_source=no)
AC_MSG_RESULT($works_without_bsd_source)

if test $works_without_bsd_source = no ; then
	CFLAGS="$CFLAGS -D_BSD_SOURCE"
	AC_MSG_CHECKING([whether M_PI is available with -D_BSD_SOURCE])
	AC_TRY_COMPILE([#include <math.h>], [double f = M_PI], m_pi_works=yes, m_pi_works=no)
	AC_MSG_RESULT($m_pi_works)
	if test $m_pi_works = no ; then
		AC_MSG_ERROR([M_PI is not available])
	fi
fi

dnl **************************************************
AC_SUBST(ALL_LINGUAS,
"am az bg ca cs da de el en_CA en_GB es et fi fr ga gl he hr hu it ja ko lv ml mr ms nb nl nn no pl pt pt_BR ro ru sk sr sr@Latn sv tr uk vi zh_CN zh_TW")

AC_SUBST(GETTEXT_PACKAGE, goffice)
AC_DEFINE_UNQUOTED(GETTEXT_PACKAGE, "$GETTEXT_PACKAGE",
	[The package name, for gettext])
AM_GLIB_GNU_GETTEXT

dnl
dnl On Solaris finite() needs ieeefp.h
dnl Either of these seem to signal IEEE754 math, see mathfunc.c
dnl
AC_CHECK_HEADERS(ieeefp.h ieee754.h)

dnl Check for some functions
AC_CHECK_FUNCS(random drand48 finite memmove mkdtemp uname times sysconf)

dnl FIXME: Does this really belong here?
AC_CHECK_FUNC(bind_textdomain_codeset,,[AC_CHECK_LIB(intl,bind_textdomain_codeset)])

dnl isfinite is a macro on HPUX
AC_TRY_COMPILE([#include <math.h>], [int a = isfinite(0.0)],
	[AC_DEFINE(HAVE_ISFINITE, 1,
		[Define if the isfinite() macro is available]
	 )
	], [])

dnl
dnl On BSD, we seem to need -lm for finite
dnl
if test $ac_cv_func_finite = no; then
  AC_CHECK_LIB(m, finite,
        [AC_DEFINE(HAVE_FINITE, 1,
		[Define if the finite function is available]
	 )
         LIBS="$LIBS -lm"])
fi

dnl check for complete locale implementation
AC_CHECK_HEADERS(langinfo.h)

dnl Handle systems that have stuff in -lm.
AC_CHECK_FUNCS(log)
if test $ac_cv_func_log = no; then
  AC_CHECK_LIB(m, log,
        [AC_DEFINE(HAVE_LOG, 1,
		[Define if the log function is available]
	 )
         LIBS="$LIBS -lm"])
fi

AC_DEFUN([GOFFICE_CHECK_FUNC],
[AC_CHECK_FUNC([$1],
	[],
	[AC_DEFINE([GOFFICE_SUPPLIED_]AS_TR_CPP([$1]),
		1,
		[Define if G Office supplies $1.])])dnl
])
GOFFICE_CHECK_FUNC(log1p)
GOFFICE_CHECK_FUNC(expm1)
GOFFICE_CHECK_FUNC(asinh)
GOFFICE_CHECK_FUNC(acosh)
GOFFICE_CHECK_FUNC(atanh)

float_msg=no
EXTRA_LIBS= EXTRA_INCLUDES=
AC_C_LONG_DOUBLE
if test $ac_cv_c_long_double = yes; then
    have_mandatory_funcs=yes
    need_sunmath=0
    sunmathlinkstuff='-L/opt/SUNWspro/lib -R/opt/SUNWspro/lib -lsunmath'
    for ldfunc in fabsl logl log10l ceill floorl powl isnanl finitel; do
	    AC_CHECK_FUNC($ldfunc,
		  ,
		  [AC_CHECK_LIB(m,
		      $ldfunc,
		      ,
		      [AC_CHECK_LIB(sunmath,
			  $ldfunc,
			  [ if test $need_sunmath = 0; then
				# FIXME: better idea?
				LDFLAGS="$LDFLAGS $sunmathlinkstuff"
				sunmathinclude=`ls -d /opt/SUNWspro/*/include/cc | sed '$!d'`
				CPPFLAGS="$CPPFLAGS -I$sunmathinclude"
			    fi
			    need_sunmath=1 ],
			  [have_mandatory_funcs=no],
			  [-L/opt/SUNWspro/lib $GOFFICE_LIBS])])])
    done
    if test $need_sunmath = 1; then
	EXTRA_LIBS="$EXTRA_LIBS $sunmathlinkstuff"
	EXTRA_INCLUDES="$EXTRA_INCLUDES -I$sunmathinclude"
	AC_CHECK_HEADERS([sunmath.h floatingpoint.h],
			 ,
			 [AC_MSG_WARN([Long doubles require the $ac_header header.])
			  have_mandatory_funcs=no])
    fi
    unset need_sunmath
    unset sunmathlinkstuff
    unset sunmathinclude

    GOFFICE_CHECK_FUNC(modfl)
    GOFFICE_CHECK_FUNC(ldexpl)
    GOFFICE_CHECK_FUNC(frexpl)

    AC_CHECK_FUNCS(strtold)
    if test "$ac_cv_func_strtold" = yes; then
	AC_MSG_CHECKING([if we must prototype strtold ourselves])
	AC_TRY_RUN([#include <stdlib.h>
		int main ()
		{
			const char *s = "+3.1415e+0";
			char *theend;
			long double res = strtold (s, &theend);
			return !(*theend == 0 && finitel (res) &&
				 res >= 3.14 && res <= 3.15);
		}],
		[AC_MSG_RESULT(no)],
		[AC_MSG_RESULT(yes)
		 AC_DEFINE([GOFFICE_SUPPLIED_STRTOLD], 1,
			   [Define if G Office supplies strtold.])
		],
		[AC_MSG_RESULT(assuming not)])
    else 
	AC_DEFINE([GOFFICE_SUPPLIED_STRTOLD], 1,
		  [Define if G Office supplies strtold.])
	AC_CHECK_FUNCS(string_to_decimal decimal_to_quadruple)
	if test "x$ac_cv_func_string_to_decimal" != "xyes" || \
	   test "x$ac_cv_func_decimal_to_quadruple" != "xyes" || \
	   test "x$ac_cv_header_floatingpoint_h" != "xyes"; then
		AC_MSG_WARN([You lack the strtold function -- precision will be impaired])
	fi
    fi
    if test "$have_mandatory_funcs" = yes; then
	float_msg=yes
	AC_DEFINE([GOFFICE_WITH_LONG_DOUBLE], 1,
		  [Define if G Office supports long double.])
    else
	AC_MSG_WARN([Long double support disabled because of library problems])
    fi
    unset have_mandatory_funcs
fi
AC_SUBST(EXTRA_LIBS)
AC_SUBST(EXTRA_INCLUDES)

dnl ******************
dnl * Config defaults
dnl ******************
dnl
dnl These are changed in goffice.c for WIN32 packages
AC_SUBST(goffice_datadir, '${datadir}/goffice/${VERSION}')
AC_SUBST(goffice_libdir, '${libdir}/goffice/${VERSION}')
AC_SUBST(goffice_icondir, '${datadir}/pixmaps/goffice')
AC_SUBST(goffice_localedir, '${prefix}/${DATADIRNAME}/locale')
dnl
AC_SUBST(goffice_plugindir, '${goffice_libdir}/plugins')
AC_SUBST(goffice_gladedir, '${goffice_datadir}/glade')

dnl Export to goffice-config.h and goffice-features.h
AC_DEFINE(GOFFICE_VERSION, "goffice_full_version",
	[The version number of this release, possibly with additional suffix])
AC_DEFINE(GO_VERSION_EPOCH, goffice_version_epoch,
	[The Epoch of this release])
AC_DEFINE(GO_VERSION_MAJOR, goffice_version_major,
	[The Major version number of this release])
AC_DEFINE(GO_VERSION_MINOR, goffice_version_minor,
	[The Minor version number of this release])
AC_DEFINE(GO_VERSION_EXTRA, "goffice_version_extra",
	[Extra, possibly empty tag for this release])

AC_CONFIG_FILES([
Makefile
libgoffice-1.pc
goffice/Makefile
goffice/app/Makefile
goffice/utils/Makefile
goffice/data/Makefile
goffice/gtk/Makefile
goffice/graph/Makefile
goffice/drawing/Makefile
goffice/ms-compat/Makefile
goffice/cut-n-paste/Makefile
goffice/cut-n-paste/foocanvas/Makefile
goffice/cut-n-paste/pcre/Makefile
plugins/Makefile
plugins/plot_barcol/Makefile
plugins/plot_pie/Makefile
plugins/plot_radar/Makefile
plugins/plot_xy/Makefile
plugins/plot_surface/Makefile
plugins/plot_boxes/Makefile
plugins/reg_linear/Makefile
pixmaps/Makefile
po/Makefile.in
tests/Makefile
])
AC_OUTPUT

echo "

Configuration:

	Source code location:	${srcdir}
	Compiler:		${CC}
	Compiler flags:		${CFLAGS}
	Long double support:	${float_msg}

	Cairo support:		${cairo_msg}
	UI:			${ui_msg}
"
