-*- mode: m4 -*-
AC_PREREQ(2.52)

m4_define([gnumeric_version_epoch], [1])
m4_define([gnumeric_version_major], [4])
m4_define([gnumeric_version_minor], [3])
m4_define([gnumeric_version_extra], [])
m4_define([gnumeric_full_version],
    [gnumeric_version_epoch.gnumeric_version_major.gnumeric_version_minor()gnumeric_version_extra])

AC_INIT([gnumeric], [gnumeric_full_version],
	[http://bugzilla.gnome.org/enter_bug.cgi?product=gnumeric])

AC_CONFIG_SRCDIR(src/sheet.h)
AM_INIT_AUTOMAKE([dist-bzip2])
AM_CONFIG_HEADER(gnumeric-config.h)

AM_MAINTAINER_MODE

# Make --disable-static the default
AC_DISABLE_STATIC

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

AC_ISC_POSIX
AC_PROG_CC
AC_PROG_YACC
AM_PROG_LEX
AC_PROG_LN_S
AM_PROG_LIBTOOL
AC_STDC_HEADERS

AC_SUBST(ACLOCAL_FLAGS)

if test "$GCC" = "yes"; then
  gcc_check=`$CC -v 2>&1 | grep '^gcc version 2\.96'`
  if test "$gcc_check" != ""; then
    # I know a dead parrot when I see one, and I am looking at
    # one now.
    AC_MSG_ERROR([You cannot compile Gnumeric with RedHat gcc 2.96.
We have seen too many subtle compiler errors.
Please use another compiler, for example a gcc 2.95 series compiler.])
    # Mate, this parrot wouldn't "vooom!" if you put four million
    # Volts through it!
    #
    # Actually, the real problem here is that there are so many
    # different compilers calling themselves 2.96.  RedHat, listen
    # up!  That's a MAJOR BLUNDER!  If the user just thinks of it
    # as "gcc 2.96" then how the H*** are we going to get bug
    # reports we can use?  Chances are the newer 2.96 compilers
    # are just fine, but how can we know?
  fi
fi

dnl *****************************
# Check for zlib.
_cppflags="${CPPFLAGS}"
_ldflags="${LDFLAGS}"

AC_ARG_WITH(zlib,
	[[  --with-zlib=DIR       use libz in DIR]],
	[case x"$withval" in
	 xyes|xno) ;;
	 *)	Z_DIR=$withval
		CPPFLAGS="${CPPFLAGS} -I$withval/include"
		LDFLAGS="${LDFLAGS} -L$withval/lib"
		;;
	 esac])

if test "x$with_zlib" != xno; then
	with_zlib=no
	AC_CHECK_HEADER(zlib.h, [AC_CHECK_LIB(z, gzread, [with_zlib=yes])])
fi

if test "$with_zlib" = no; then
	AC_MSG_ERROR([*** zlib is required])
fi

if test "x${Z_DIR}" != "x"; then
	Z_CPPFLAGS="-I${Z_DIR}/include"
	case ${host} in
	*-*-solaris*)	Z_LIBS="-L${Z_DIR}/lib -R${Z_DIR}/lib -lz" ;;
	*)		Z_LIBS="-L${Z_DIR}/lib -lz" ;;
	esac
else
	Z_LIBS="-lz"
fi
AC_DEFINE([HAVE_LIBZ], [], [Have compression library])
AC_SUBST(Z_CPPFLAGS)
AC_SUBST(Z_LIBS)

CPPFLAGS=${_cppflags}
LDFLAGS=${_ldflags}

dnl *****************************
gnumeric_reqs="
	glib-2.0		>= 2.4.0
	gobject-2.0		>= 2.4.0
	gmodule-2.0		>= 2.4.0
	libgsf-1		>= 1.10.0
	libxml-2.0		>= 2.4.12
	pango			>= 1.4.0
	pangoft2		>= 1.4.0
"
gnumeric_gtk_reqs="
	gtk+-2.0		>= 2.4.0
	libglade-2.0		>= 2.3.6
	libgnomecanvas-2.0	>= 2.0.0
	libgnomeprint-2.2	>= 2.5.2
	libgnomeprintui-2.2	>= 2.5.2
	libart-2.0		>= 2.3.11
"
gnumeric_gnome_reqs="
	gconf-2.0
	libbonobo-2.0		>= 2.2.0
	libbonoboui-2.0		>= 2.2.0
	libgnome-2.0		>= 2.0.0
	libgnomeui-2.0		>= 2.0.0
	libgsf-gnome-1		>= 1.10.0
"

ui_msg=

dnl *******************
dnl Should we use gtk ?
dnl *******************
gnumeric_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)"
	gnumeric_with_gtk=false
fi

if test "x$gnumeric_with_gtk" = "xtrue" ; then
	PKG_CHECK_MODULES(GTK, [$gnumeric_gtk_reqs],
		[ui_msg="Gtk"],
		[ui_msg="None (missing gtk dependencies)" ; gnumeric_with_gtk=false ])
fi 

gnumeric_with_gnome=$gnumeric_with_gtk
if test "x$gnumeric_with_gtk" = "xtrue" ; then
	AC_DEFINE(WITH_GTK, 1, [Define if UI is built])
	gnumeric_reqs="$gnumeric_reqs $gnumeric_gtk_reqs"

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

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

	if test "x$gnumeric_with_gnome" = "xtrue"; then
		AC_DEFINE(WITH_GNOME, 1, [Define if GNOME extensions are available])
		gnumeric_reqs="$gnumeric_reqs $gnumeric_gnome_reqs"
	fi
fi
AM_CONDITIONAL(WITH_GTK,   $gnumeric_with_gtk)
AM_CONDITIONAL(WITH_GNOME, $gnumeric_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(GNUMERIC, $gnumeric_reqs)
GNUMERIC_CFLAGS="$GNUMERIC_CFLAGS -I\$(top_srcdir)/src/cut-n-paste-code"
AC_SUBST(GNUMERIC_LIBS)
AC_SUBST(GNUMERIC_CFLAGS)

win32=no
case $host_os in
*mingw* | pw32* | cygwin*)
    win32=yes
    GNUMERIC_PLUGIN_LDFLAGS="-no-undefined $GNUMERIC_LIBS -Wl,--enable-runtime-pseudo-relo,\$(top_builddir)/src/gnumeric.exe.a -s"
    AC_CHECK_TOOL(WINDRES, windres, :)
    ;;
esac
AM_CONDITIONAL(WITH_WIN32, test x"$win32" = "xyes")

GNUMERIC_PLUGIN_LDFLAGS="-avoid-version $GNUMERIC_PLUGIN_LDFLAGS"
AC_SUBST(GNUMERIC_PLUGIN_LDFLAGS)

dnl disable for in stable release, re-enable for development series
CFLAGS="$CFLAGS -DG_DISABLE_DEPRECATED"
CFLAGS="$CFLAGS -DPANGO_DISABLE_DEPRECATED"
if test "x$gnumeric_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$gnumeric_with_gnome" = "xtrue"; then
	CFLAGS="$CFLAGS -DGNOME_DISABLE_DEPRECATED"
	CFLAGS="$CFLAGS -DBONOBO_DISABLE_DEPRECATED"
	CFLAGS="$CFLAGS -DBONOBO_UI_DISABLE_DEPRECATED"
fi

dnl ==============================================
dnl GNOME Specific extensions
with_corba=false
if test "x${gnumeric_with_gnome}" = "xtrue"; then

	dnl ==============================================
	dnl Special GConf section  (stolen from libgnome)
	dnl ==============================================

	AC_ARG_VAR(GCONFTOOL, [Absolute path of the gconftool-2 executable (part of GConf).])
	AC_PATH_PROG(GCONFTOOL, gconftool-2)
	if test x"$GCONFTOOL" = x; then
		AC_MSG_ERROR([gconftool-2 executable not found in your path - should be installed with GConf])
	fi
	AM_GCONF_SOURCE_2

	dnl *************
	dnl Corba support
	dnl *************
	PKG_CHECK_MODULES(CORBA_LIBS,  [ORBit-2.0 >= 2.4.2
					libbonobo-2.0 >= 2.0.0
					bonobo-activation-2.0 >= 1.0.2
					],[with_corba=true], [with_corba=false])
	if test "x$with_corba" = "xtrue" ; then
		ORBIT="`$PKG_CONFIG --variable=orbit_idl ORBit-2.0`"
		BONOBO_IDL_DIR="`$PKG_CONFIG --variable=idldir libbonobo-2.0`"
		BONOBO_ACTIVATION_IDL_DIR="`$PKG_CONFIG --variable=idldir bonobo-activation-2.0`"
		ORBIT_IDL="${ORBIT} -I${BONOBO_IDL_DIR} -I${BONOBO_ACTIVATION_IDL_DIR}"
		AC_SUBST(ORBIT_IDL)
	fi
fi
AM_CONDITIONAL(GCONF_SCHEMAS_INSTALL, $gnumeric_with_gnome)
AM_CONDITIONAL(WITH_CORBA, $with_corba)

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

AC_ARG_VAR(GLIB_GENMARSHAL, [Absolute path of the glib-genmarshal executable.])
AC_PATH_PROG(GLIB_GENMARSHAL, glib-genmarshal)
AC_ARG_VAR(GDK_PIXBUF_CSOURCE, [Absolute path of the gdk-pixbuf-csource executable.])
AC_PATH_PROG(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; 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_ARG_ENABLE(ssconvert, [--disable-ssconvert		Do not build ssconvert (cmd line spreadsheet conversion tool)],
	[ ], [enable_ssconvert=yes])
AM_CONDITIONAL(ENABLE_SSCONVERT, test x"$enable_ssconvert" = "xyes")
AC_ARG_ENABLE(ssindex, [--enable-ssindex		Build ssindex (spreadsheet indexer for beagle)],
	[ ], [enable_ssindex=no])
AM_CONDITIONAL(ENABLE_SSINDEX, test x"$enable_ssindex" = "xyes")

dnl ****************************
dnl GDA Plugin
dnl ****************************
try_gda=true
gda_msg=yes
gnomedb_msg=no
AC_ARG_WITH(gda,
	[--{with,without}-gda      Compile the Gnome Database Access plugin],
	if test "x$withval" != xyes; then
		try_gda=false
		gda_msg="Disabled by request"
	fi
)
if test "$try_gda" = "true"; then
	PKG_CHECK_MODULES(GDA, [libgda >= 1.0.1 ],
		[gda_msg=yes],
		[gda_msg="NO.  libgda problem"])
	if test "$gda_msg" = "yes"; then
		PKG_CHECK_MODULES(GNOMEDB, [libgnomedb >= 1.0.1 ],
			[gnomedb_msg="yes"],
			[gnomedb_msg="NO. libgnomedb problem"])
		if test "$gnomedb_msg" = "yes"; then
			AC_DEFINE(HAVE_LIBGNOMEDB, 1, [ Define if libgnomedb support is compiled in])
		fi
	fi
fi
AM_CONDITIONAL(WITH_GDA, test x"$gda_msg" = "xyes")
AC_SUBST(GDA_CFLAGS)
AC_SUBST(GDA_LIBS)

AM_CONDITIONAL(WITH_GNOMEDB, test x"$gnomedb_msg" = "xyes")
AC_SUBST(GNOMEDB_CFLAGS)
AC_SUBST(GNOMEDB_LIBS)

dnl ****************************
dnl GB Plugin
dnl ****************************
#try_gda=true
#gb_msg=yes
try_gb=no
gb_msg="disabled due to lack of gb maintenance"
AC_ARG_WITH(gb,
	[--{with,without}-gb       Compile the Gnome Basic plugin],
	if test "x$withval" = xyes; then
		#try_gb=true
		gb_msg="We are not joking.  There is no upstream maintainer for GB"
		#gb_msg="Disabled by request"
	fi
)
if test "$try_gb" = "true"; then
	PKG_CHECK_MODULES(GB, [libgb == 1.17 ],
		[gb_msg=yes],
		[gb_msg="NO.  gb problem"])
fi
AM_CONDITIONAL(WITH_GB, test x"$gb_msg" = "xyes")
AC_SUBST(GB_CFLAGS)
AC_SUBST(GB_LIBS)

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

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 src/functions/fn-math.c uses 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

AC_SUBST(GUILE_LIBS)
AC_SUBST(GUILE_INCS)

#check for guile 1.5
try_guile=false
guile_msg="disabled pending some guile developer interest"
enable_guile=false
AC_ARG_WITH(guile,
	[--{with,without}-guile    Compile with Guile support or without it],
	if test "x$withval" = xyes; then
		try_guile=true
	fi
)

dnl if $try_guile; then
dnl 	AC_MSG_CHECKING(for guile >= 1.5)
dnl 	if test "`guile -c '(display (string>=? (version) "1.4.1"))'`" != "#t"; then
dnl   		AC_MSG_RESULT([Your Guile is too old.  You need Guile 1.5 or later.])
dnl 	else
dnl 	        AC_MSG_RESULT(found)
dnl 		guile_msg="yes"
dnl 		enable_guile=true
dnl 	fi
dnl
dnl fi
dnl if $enable_guile; then
dnl 	GNOME_CHECK_GUILE
dnl fi
AM_CONDITIONAL(WITH_GUILE, $enable_guile)

dnl **************************************************
dnl * psiconv support
dnl **************************************************

AC_ARG_WITH(psiconv,
	[--{with,without}-psiconv  Compile with Psiconv support or without it])
AC_ARG_VAR(PSICONV_CONFIG, [The psiconv-config executable.])
if test "x$with_psiconv" != xno; then
    AC_CHECK_PROG(PSICONV_CONFIG,psiconv-config,psiconv-config)
    if test x"$PSICONV_CONFIG" = x; then
	psiconv_msg="missing dependencies"
    else
	AC_MSG_CHECKING(for psiconv >= 0.9.3)
	if $PSICONV_CONFIG --version | awk -F. \
		'{exit !($1 > 0 || $2 > 9 || ($2 == 9 && $3 >= 3))}'
	then
	    PSICONV_CFLAGS=`$PSICONV_CONFIG --cflags`
            PSICONV_LIBS=`$PSICONV_CONFIG --libs`
	    psiconv_msg=yes
        else
	    psiconv_msg=no
        fi
	AC_MSG_RESULT($psiconv_msg)
    fi
else
    psiconv_msg=no
fi
AM_CONDITIONAL(WITH_PSICONV, test "x$psiconv_msg" = "xyes")
AC_SUBST(PSICONV_LIBS)
AC_SUBST(PSICONV_CFLAGS)

try_paradox=true
enable_paradox=false
AC_ARG_WITH(paradox,
	[--{with,without}-paradox  Compile with Paradox support or without it],
	if test "x$withval" = xno; then
		try_paradox=false
	fi
)

if $try_paradox; then
	PKG_CHECK_MODULES(PARADOX, [
		pxlib >= 0.4.0
		],
		[paradox_msg="yes"],
		[paradox_msg="missing dependencies"])
fi

if test "x$paradox_msg" = "xyes"; then
	enable_paradox=true
fi
AM_CONDITIONAL(WITH_PARADOX, $enable_paradox)
AC_SUBST(PARADOX_LIBS)
AC_SUBST(PARADOX_CFLAGS)

AC_ARG_ENABLE(solver, [--disable-solver  Don't compile the solver])
if test "x$enable_solver" = xno; then
	enable_solver=false
else
	enable_solver=true
	AC_DEFINE(ENABLE_SOLVER, 1, [Define if SOLVER is compiled])
fi
AM_CONDITIONAL(ENABLE_SOLVER, $enable_solver)

plugin_list_given=false
PLUGIN_LIST=""
AC_ARG_ENABLE(plugins,
	[--enable-plugins="text html"  Compile only the listed plugins],
	plugin_list_given=true
	PLUGIN_LIST=$enableval
)
AM_CONDITIONAL(PLUGIN_LIST_GIVEN, $plugin_list_given)
AC_SUBST(PLUGIN_LIST)

dnl **************************************************
dnl stolen from gtk+
AC_MSG_CHECKING(whether make is GNU Make)
STRIP_BEGIN=
STRIP_END=
if $ac_make --version 2>/dev/null | grep '^GNU Make ' >/dev/null ; then
	STRIP_BEGIN='$(strip $(STRIP_DUMMY)'
	STRIP_END=')'
	AC_MSG_RESULT(yes)
else
	AC_MSG_RESULT(no)
fi
STRIP_DUMMY=
AC_SUBST(STRIP_DUMMY)
AC_SUBST(STRIP_BEGIN)
AC_SUBST(STRIP_END)

dnl **************************************************
dnl * internationalization support
dnl **************************************************
dnl
dnl Check doc/developer/translating.sgml for a description of how to translate
dnl and why we have so many translations.
dnl
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(ALL_LINGUAS)

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

#AC_PATH_PROG(TPUT_CMD,tput,no)
TPUT_CMD=no
marker_ok="<yes >"
marker_fail="<FAIL>"
marker_no="<NO  >"
if test "$TPUT_CMD" '!=' no && test -t 1 && test -t 2; then
  # We have tput and output goes to a terminal.
  marker_ok="<`$TPUT_CMD bold`yes`$TPUT_CMD sgr0` >"
  marker_fail="<`$TPUT_CMD bold`fail`$TPUT_CMD sgr0`>"
  marker_no="<`$TPUT_CMD bold`no`$TPUT_CMD sgr0`  >"
fi

dnl We do not want to force libgnomeprint 2.8 just yet.
SAVE_LIBS="$LIBS"
LIBS="$LIBS $GNUMERIC_LIBS"
AC_CHECK_FUNCS(gnome_print_pango_create_layout mkfifo)
LIBS="$SAVE_LIBS"
unset SAVE_LIBS

dnl We do not want to force pango 1.6 just yet.
SAVE_LIBS="$LIBS"
LIBS="$LIBS $GNUMERIC_LIBS"
AC_CHECK_FUNCS(pango_layout_set_ellipsize pango_context_set_matrix pango_context_get_font_map)
LIBS="$SAVE_LIBS"
unset SAVE_LIBS

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 We supply our own lgamma_r (using lgamma) when missing.
dnl We supply our own expm1 (using log1p) when missing.
AC_CHECK_FUNCS(lgamma)
if test $ac_cv_func_lgamma = no; then
  AC_CHECK_LIB(m, lgamma,
        [AC_DEFINE(HAVE_LGAMMA, 1,
		[Define if the lgamma function is available]
	 )
         LIBS="$LIBS -lm"])
fi
AC_CHECK_FUNCS(lgamma_r expm1 log1p asinh acosh atanh)

AC_C_LONG_DOUBLE
float_msg=double
have_ld=no
AC_ARG_WITH(long_double,
	[--{with,without}-long-double		Use long double for floating point],
	[if test "x$withval" = xyes; then
		if test "x$ac_cv_c_long_double" != "xyes"; then
			AC_MSG_ERROR([Long double type is not available.])
		fi

		need_sunmath=no
		for ldfunc in fabsl sqrtl expl expm1l logl log10l log1pl ceill floorl powl hypotl \
				sinl cosl tanl asinl acosl atanl atan2l fmodl lgammal lgammal_r \
				sinhl coshl tanhl asinhl acoshl atanhl \
				isnanl finitel; do
			AC_CHECK_FUNC($ldfunc,
			      ,
			      AC_CHECK_LIB(m,
				  $ldfunc,
				  ,
				  AC_CHECK_LIB(sunmath,
				      $ldfunc,
				      [ if test "x$need_sunmath" = "xno"; then
					    # FIXME: better idea?
					    LDFLAGS="$LDFLAGS -L/opt/SUNWspro/lib -R/opt/SUNWspro/lib -lsunmath"
					    sunmathinclude=`ls -d /opt/SUNWspro/*/include/cc | tail -1`
					    CPPFLAGS="$CPPFLAGS -I$sunmathinclude"
				        fi
					need_sunmath=yes ],
				      AC_MSG_ERROR([Long doubles require the $ldfunc function.]),
				      -L/opt/SUNWspro/lib $GNUMERIC_LIBS)))
		done
		if test "x$need_sunmath" = "xyes"; then
			AC_CHECK_HEADERS([sunmath.h floatingpoint.h],
					 ,
					 AC_MSG_ERROR([Long doubles require the sunmath.h header.]))
		fi

		AC_CHECK_FUNCS(modfl frexpl ldexpl strtold string_to_decimal decimal_to_quadruple)
		if test "x$ac_cv_func_strtold" = "xyes"; 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(MUST_PROTOTYPE_STRTOLD, 1,
					[Define if we need to provide a prototype for strtold()]
				 )],
				AC_MSG_RESULT(assuming not))
		elif 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

		if test "x$ac_cv_func_modfl" != "xyes"; then
			AC_MSG_WARN([You lack the modfl function -- precision will be impaired])
		fi

		AC_MSG_CHECKING([checking for working ynl])
		AC_TRY_RUN([
			#include <math.h>
			#ifdef HAVE_SUNMATH_H
			#include <sunmath.h>
			#endif
			int main ()
			{
				long double l = ynl (2, 4.0L);
				return !(l >= 0.21 && l <= 0.22);
			}],
			[AC_DEFINE(HAVE_YNL)
			AC_MSG_RESULT(yes)],
			AC_MSG_RESULT(no),
			[AC_CHECK_FUNCS(ynl)])

		AC_MSG_CHECKING([checking for working erfl and erfcl])
		AC_TRY_RUN([
			#include <math.h>
			#ifdef HAVE_SUNMATH_H
			#include <sunmath.h>
			#endif
			int main ()
			{
				long double l1 = erfl (1.2L);
				long double l2 = erfcl (1.2L);
				return !(l1 >= 0.91 && l1 <= 0.92 &&
					 l2 >= 0.08 && l2 <= 0.09);
			}],
			[AC_DEFINE(HAVE_ERFL)
			 AC_DEFINE(HAVE_ERFCL)
			AC_MSG_RESULT(yes)],
			AC_MSG_RESULT(no),
			[AC_CHECK_FUNCS(erfl erfcl)])

		float_msg="long double (EXPERIMENTAL)"
		AC_MSG_WARN([Long double support is experimental -- expect problems])
		AC_DEFINE(WITH_LONG_DOUBLE, 1,
			[Define if the long double type is to be used]
		)
	fi]
)

dnl **************************************************
dnl * Check for Perl
dnl **************************************************
dnl
perl_msg=no
AC_ARG_WITH(perl, [--{with,without}-perl   Compile the Perl plugin loader])
AC_ARG_VAR(PERL, [The Perl executable.])
if test "x$with_perl" != xno; then
    AC_CHECK_PROG(PERL, perl, perl)
    if test "x$PERL" != x; then
	AC_MSG_CHECKING([for perl ExtUtils::Embed module])
	if $PERL -e 'eval { require ExtUtils::Embed }; if ($@) { exit(1); } else { exit(0); }'
	then
	    AC_MSG_RESULT($marker_ok)
	    dnl AC_CHECK_LIB(perl, perl_construct, [], [perl_msg="missing/invalid libperl"])
	else
	    AC_MSG_RESULT($marker_no)
	    perl_msg="Missing ExtUtils::Embed"
	fi
    else
	perl_msg="could not find perl"
    fi
else
    perl_msg="Disabled by default"
fi

AM_CONDITIONAL(WITH_PERL, test "x$perl_msg" = xyes)
if test "x$perl_msg" = xyes ; then
    dnl Use ExtUtils::Embed to figure out the other options.
    PERL_CCCDLFLAGS=`$PERL -MConfig -e 'print $Config{cccdlflags},"\n";'`
    PERL_LDDLFLAGS=`$PERL -MConfig -e 'print $Config{lddlflags},"\n";'`
    PERL_CC=`$PERL -MConfig -e 'print $Config{cc},"\n";'`
    PERL_LD=`$PERL -MConfig -e 'print $Config{ld},"\n";'`
    PERL_CCOPTS=`$PERL -MExtUtils::Embed -e ccopts`
    PERL_LDOPTS=`$PERL -MExtUtils::Embed -e ldopts`

    perl_msg="yes (using $PERL)"
fi
AC_SUBST(PERL_CCCDLFLAGS)
AC_SUBST(PERL_LDDLFLAGS)
AC_SUBST(PERL_CC)
AC_SUBST(PERL_LD)
AC_SUBST(PERL_CCOPTS)
AC_SUBST(PERL_LDOPTS)

dnl **************************************************
dnl * Check for Python
dnl **************************************************
python_msg="yes"
AC_ARG_WITH(python, [--{with,without}-python   Compile the Python plugin loader])
AC_ARG_VAR(PYTHON, [The Python executable.])
if test "x$with_python" != xno; then
    AC_CHECK_PROG(PYTHON, python, python)
    if test "x$PYTHON" = x; then
	python_msg="could not find python"
    fi
else
    python_msg="Disabled by request"
fi

if test "x$python_msg" = xyes; then
    AC_MSG_CHECKING([for python >= 2.0])
    PY_PREFIX=`$PYTHON -c 'import sys ; print sys.prefix'`
    PY_EXEC_PREFIX=`$PYTHON -c 'import sys ; print sys.exec_prefix'`
    PY_MAKEFILE=`$PYTHON -c 'from distutils.sysconfig import get_makefile_filename; print get_makefile_filename()'`
    [PY_VERSION=`$PYTHON -c 'import sys ; print sys.version[0:3]'`
    broken_py_initialize=`$PYTHON -c \
'import sys
if int(sys.version[0]) < 2:
    print "yes"
else:
    print "no"'`]
    if test "x$broken_py_initialize" != "xyes"; then
	AC_MSG_RESULT($marker_ok)
    else
	AC_MSG_RESULT($marker_fail)
	python_msg="NO.  Python version is too old."
    fi
fi

if test "x$python_msg" = xyes; then
    AC_MSG_CHECKING(Look for $PY_PREFIX/include/python$PY_VERSION/Python.h)
    if test -f $PY_PREFIX/include/python$PY_VERSION/Python.h; then
	AC_MSG_RESULT($marker_ok)
	PY_LIBS="python$PY_VERSION"
    	PY_LIB_LOC=`$PYTHON -c 'from distutils.sysconfig import get_config_var; print get_config_var("LIBPL")'`
    	PY_LIB_LOC="-L$PY_LIB_LOC"
	PY_CFLAGS="-I$PY_PREFIX/include/python$PY_VERSION"
	[echo 'echo_python_libs:
	@echo $(MODLIBS) $(LIBS)' >confmake.tmp
	PY_EXTRA_LIBS=`make -s -f confmake.tmp -f "$PY_MAKEFILE"`
	rm -f confmake.tmp]
    else
	AC_MSG_RESULT($marker_fail)
	python_msg="unable to find Python.h"
    fi
fi

if test "x$python_msg" = xyes; then
    AC_MSG_CHECKING(if we can build a shared library depending on libpython)
    rm -rf testpython
    mkdir testpython
    cd testpython
    cat > testpython.c <<EOF
#include <Python.h>
int testpython (void)
{
    Py_Exit (0);
}
EOF
    if /bin/sh ../libtool --mode=compile ${CC} $PY_CFLAGS -c testpython.c >/dev/null 2>&1 && \
       /bin/sh ../libtool --mode=link ${CC} -o testpython.la -rpath `pwd` -module -avoid-version $PY_LIB_LOC testpython.lo -l$PY_LIBS $PY_EXTRA_LIBS >/dev/null 2>&1 && \
       grep 'dlname.*testpython' testpython.la >/dev/null 2>&1; then
	AC_MSG_RESULT($marker_ok)
    else
	AC_MSG_RESULT($marker_fail)
	python_msg="unable to link to python"
    fi
    cd ..
    rm -rf testpython
fi
if test "x$python_msg" = xyes; then
    PKG_CHECK_MODULES(PYGTK, pygtk-2.0 >= 1.99.10,
	    [],
	    [python_msg="NO.  pygtk problem"])
fi

AM_CONDITIONAL(WITH_PYTHON, test "x$python_msg" = xyes)
if test "x$python_msg" = xyes; then
    python_msg="yes (using $PYTHON)"
fi
AC_SUBST(PYGTK_CFLAGS)
AC_SUBST(PY_LIBS)
AC_SUBST(PY_LIB_LOC)
AC_SUBST(PY_CFLAGS)
AC_SUBST(PY_EXTRA_LIBS)

dnl **************************************************
dnl * Check for mono
dnl **************************************************
with_mono=no
mono_msg="disabled, still experimental"

dnl A tricky way to comment out in m4:
ifelse([
AC_ARG_WITH(mono, [--{with,without}-mono     Compile the mono scripting engine])
case x$with_mono in
xno)	mono_msg="Disabled by request";;
*)
    PKG_CHECK_MODULES(MONO, mono,
	[with_mono=yes
	 AC_DEFINE(WITH_MONO, 1, [Define if mono .NET engine is available])],
	[with_mono=no])
    mono_msg=$with_mono
    ;;
esac
])

AM_CONDITIONAL(WITH_MONO, test "x$with_mono" = "xyes")
AC_SUBST(MONO_CFLAGS)
AC_SUBST(MONO_LIBS)

dnl **************************************************
dnl * Config defaults (Must be after bonobo)
dnl **************************************************
gnumeric_version="${VERSION}"
gnumeric_prefix='${prefix}'
gnumeric_exec_prefix='${exec_prefix}'
gnumeric_sysconfdir='${sysconfdir}'
dnl These are changed in libgnumeric.c for WIN32 packages
gnumeric_datadir='${datadir}/gnumeric/${gnumeric_version}'
gnumeric_libdir='${libdir}/gnumeric/${gnumeric_version}'
gnumeric_plugindir='${gnumeric_libdir}/plugins'
gnumeric_gladedir='${gnumeric_datadir}/glade'
gnumeric_icondir='${datadir}/pixmaps/gnumeric'
gnumeric_localedir='${prefix}/${DATADIRNAME}/locale'

gnumeric_autoformatdir='${gnumeric_datadir}/autoformat-templates'

dnl Export to gnumeric-config.h
AC_DEFINE_UNQUOTED(GNUMERIC_VERSION, "gnumeric_full_version",
	[The version number of this release, possibly suffixed for bonobo]
)
AC_DEFINE_UNQUOTED(GNM_VERSION_EPOCH, gnumeric_version_epoch,
	[The Epoch of this release]
)
AC_DEFINE_UNQUOTED(GNM_VERSION_MAJOR, gnumeric_version_major,
	[The Major version number of this release]
)
AC_DEFINE_UNQUOTED(GNM_VERSION_MINOR, gnumeric_version_minor,
	[The Minor version number of this release]
)
AC_DEFINE_UNQUOTED(GNM_VERSION_EXTRA, "gnumeric_version_extra",
	[Extra, possibly empty tag for this release]
)
AC_SUBST(gnumeric_version)
AC_SUBST(gnumeric_prefix)
AC_SUBST(gnumeric_exec_prefix)
AC_SUBST(gnumeric_sysconfdir)
AC_SUBST(gnumeric_datadir)
AC_SUBST(gnumeric_libdir)
AC_SUBST(gnumeric_plugindir)
AC_SUBST(gnumeric_gladedir)
AC_SUBST(gnumeric_icondir)
AC_SUBST(gnumeric_localedir)
AC_SUBST(gnumeric_autoformatdir)
AC_SUBST(gnumeric_with_gnome)

AC_OUTPUT([
gnumeric.spec
Makefile
icons/Makefile
src/Makefile
src/dialogs/Makefile
src/gnumeric-paths.sh
src/pixmaps/Makefile
src/widgets/Makefile
src/tools/Makefile
src/tools/solver/Makefile
src/tools/solver/glpk/Makefile
src/tools/solver/glpk/source/Makefile
src/tools/solver/glpk/include/Makefile
src/tools/solver/lp_solve/Makefile
src/cut-n-paste-code/Makefile
src/cut-n-paste-code/foocanvas/Makefile
src/cut-n-paste-code/foocanvas/libfoocanvas/Makefile
src/cut-n-paste-code/goffice/Makefile
src/cut-n-paste-code/goffice/app/Makefile
src/cut-n-paste-code/goffice/pixmaps/Makefile
src/cut-n-paste-code/goffice/utils/Makefile
src/cut-n-paste-code/goffice/gui-utils/Makefile
src/cut-n-paste-code/goffice/graph/Makefile
src/cut-n-paste-code/goffice/graph/plugins/Makefile
src/cut-n-paste-code/goffice/graph/plugins/plot_barcol/Makefile
src/cut-n-paste-code/goffice/graph/plugins/plot_pie/Makefile
src/cut-n-paste-code/goffice/graph/plugins/plot_radar/Makefile
src/cut-n-paste-code/goffice/graph/plugins/plot_xy/Makefile
src/cut-n-paste-code/goffice/graph/plugins/plot_surface/Makefile
src/cut-n-paste-code/goffice/drawing/Makefile
src/cut-n-paste-code/goffice/ms-compat/Makefile
src/cut-n-paste-code/goffice/libpresent/Makefile
src/cut-n-paste-code/goffice/test/Makefile
src/cut-n-paste-code/goffice/cut-n-paste/Makefile
src/cut-n-paste-code/goffice/cut-n-paste/pcre/Makefile
src/cut-n-paste-code/goffice/cut-n-paste/egg-recent-files/Makefile
doc/Makefile
doc/C/Makefile
doc/C/figures/Makefile
doc/developer/Makefile
plugins/Makefile
plugins/numtheory/Makefile
plugins/fn-complex/Makefile
plugins/fn-database/Makefile
plugins/fn-date/Makefile
plugins/fn-eng/Makefile
plugins/fn-erlang/Makefile
plugins/fn-financial/Makefile
plugins/fn-info/Makefile
plugins/fn-logical/Makefile
plugins/fn-lookup/Makefile
plugins/fn-math/Makefile
plugins/fn-stat/Makefile
plugins/fn-string/Makefile
plugins/fn-random/Makefile
plugins/derivatives/Makefile
plugins/sc/Makefile
plugins/sylk/Makefile
plugins/excel/Makefile
plugins/gda/Makefile
plugins/gnome-db/Makefile
plugins/lotus-123/Makefile
plugins/oleo/Makefile
plugins/python-loader/Makefile
plugins/gnome-glossary/Makefile
plugins/py-func/Makefile
plugins/corba/Makefile
plugins/perl/Makefile
plugins/perl/ext/Makefile.PL
plugins/perl-func/Makefile
plugins/perl-loader/Makefile
plugins/guile/Makefile
plugins/xbase/Makefile
plugins/html/Makefile
plugins/dif/Makefile
plugins/qpro/Makefile
plugins/plan-perfect/Makefile
plugins/xml_sax/Makefile
plugins/applix/Makefile
plugins/openoffice/Makefile
plugins/psiconv/Makefile
plugins/mps/Makefile
plugins/sample_datasource/Makefile
plugins/paradox/Makefile
plugins/uihello/Makefile
po/Makefile.in
schemas/Makefile
templates/Makefile
templates/english/Makefile
templates/autoformat/Makefile
stamp.h
])

echo "

Configuration:

	Source code location:	${srcdir}
	Compiler:		${CC}
	Compiler flags:		${CFLAGS}
	Floating point type:	${float_msg}

	UI :			${ui_msg}

	Perl Support:		${perl_msg}
	Python Support:		${python_msg}
	Guile Support:		${guile_msg}

	GDA support:		${gda_msg}
	GNOME-DB support:	${gnomedb_msg}
	Psiconv support:        ${psiconv_msg}
"
#	Mono support:		${mono_msg}
