## -*-m4-*-

dnl Process this file with autoconf to produce a configure script.

# FILE:
# configure.in
#
# FUNCTION:
# implements checks for a variety of system-specific functions
#
# Contents:
#   Headers   - Autoconf header stuff
#   Variables - Hardcoded variables
#   Programs  - Check for programs binaries
#   Functions - Check for functions
#   With      - Check for --with/without options
#   Enable    - Check for --enable/disable options
#   Libraries - Check for libraries
#   Footer    - Autoconf footer stuff

# Autoconf initialization
AC_INIT
AC_CONFIG_HEADERS(config.h)
AC_CONFIG_SRCDIR(src/engine/Transaction.h)
AC_PREREQ(2.53)

# Define the version number
GNUCASH_MAJOR_VERSION=1
GNUCASH_MINOR_VERSION=9
GNUCASH_MICRO_VERSION=0
GNUCASH_VERSION_STRING="$GNUCASH_MAJOR_VERSION.$GNUCASH_MINOR_VERSION.$GNUCASH_MICRO_VERSION"

AM_INIT_AUTOMAKE(gnucash, $GNUCASH_VERSION_STRING)


##  Do this first, because the other tests depend on it:

# Check for gcc and intltool (somehow breaks the suggested content
# order, doesn't it? Whatever.)
AC_PROG_CC
AC_PROG_INTLTOOL

AM_GCONF_SOURCE_2

# Override gconf default install locations.  The default is to always
# put them into /etc.  This change puts them into $(prefix)/etc.
if test "x$GCONF_SCHEMA_INSTALL_SOURCE" = "x"; then
  GCONF_SCHEMA_CONFIG_SOURCE="xml::${sysconfdir}/gconf/gconf.xml.defaults"
fi
GCONF_SCHEMA_CONFIG_SOURCE_DIRONLY=`echo $GCONF_SCHEMA_CONFIG_SOURCE | cut -d ':' -f 3`
AC_SUBST(GCONF_SCHEMA_CONFIG_SOURCE_DIRONLY)


AC_SUBST(GNUCASH_MAJOR_VERSION)
AC_SUBST(GNUCASH_MINOR_VERSION)
AC_SUBST(GNUCASH_MICRO_VERSION)

AC_DEFINE_UNQUOTED(GNUCASH_MAJOR_VERSION, $GNUCASH_MAJOR_VERSION,
	[GnuCash Major version number])
AC_DEFINE_UNQUOTED(GNUCASH_MINOR_VERSION, $GNUCASH_MINOR_VERSION,
	[GnuCash Minor version number])
AC_DEFINE_UNQUOTED(GNUCASH_MICRO_VERSION, $GNUCASH_MICRO_VERSION,
	[GnuCash Micro version number])

dnl Set of available languages.
ALL_LINGUAS="da de el en_GB es es_NI fr hu it ja nb nl pl pt pt_BR ru rw sk sv ta uk zh_CN zh_TW"

GETTEXT_PACKAGE=gnucash
AC_SUBST(GETTEXT_PACKAGE)
AC_DEFINE_UNQUOTED(GETTEXT_PACKAGE, "$GETTEXT_PACKAGE",
	[GetText version number])
               
AM_GLIB_GNU_GETTEXT


AM_MAINTAINER_MODE
AC_CONFIG_MACRO_DIR(macros)


AC_PROG_INSTALL

AC_LIBTOOL_DLOPEN
AM_DISABLE_STATIC
AM_PROG_LIBTOOL

AC_SUBST(LIBTOOL_DEPS)

AC_ISC_POSIX
AC_C_BIGENDIAN
AC_PROG_MAKE_SET
AC_HEADER_STDC

AC_CHECK_HEADERS(limits.h)
AC_CHECK_FUNCS(stpcpy memcpy timegm towupper)
AC_CHECK_FUNCS(setenv,,[
  AC_CHECK_FUNCS(putenv,,[
    AC_MSG_ERROR([Must have one of the setenv or putenv functions.])
  ])
])

## +jsled
# copied from gnumeric
AC_CHECK_HEADERS(ieeefp.h ieee754.h)
dnl Check for some functions
AC_CHECK_FUNCS(random drand48 finite memmove mkdtemp uname times sysconf fmin)
## --jsled

STRUCT_TM_GMTOFF_CHECK
SCANF_LLD_CHECK
if test $am_cv_scanf_lld = "no"; then
  SCANF_QD_CHECK

  if test $am_cv_scanf_qs = "no"; then
    AC_MSG_ERROR([cannot continue, no long long conversion support in scanf])
  fi
fi

AC_CHECK_HEADERS(ltdl.h)
if test "x$ac_cv_header_ltdl_h" = xno; then
  AC_MSG_ERROR([Cannot find ltdl.h -- libtool-devel not installed?])
fi

AM_PATH_GLIB_2_0(2.4.0,,AC_MSG_ERROR([
*** GLIB >= 2.4 is required to build Gnucash; please make sure you have the
*** development headers installed. The latest version of GLIB is
*** always available at ftp://ftp.gnome.org/pub/gnome/sources/glib/.]),
	gthread gobject)

if test ${no_glib}x = yesx
then
  AC_MSG_ERROR([Cannot find glib. Check config.log])
fi

AC_MSG_CHECKING(for GLIB - version >= 2.6.0)
if $PKG_CONFIG 'glib-2.0 >= 2.6.0'
then
  AC_MSG_RESULT(yes)
  AC_DEFINE(HAVE_GLIB26,1,[System has glib 2.6.0 or better])
  HAVE_GLIB26=yes
else
  AC_MSG_RESULT(no)
fi
AM_CONDITIONAL(HAVE_GLIB26, test "x$HAVE_GLIB26" = "xyes" )

AC_CHECK_HEADERS(dlfcn.h dl.h utmp.h locale.h mcheck.h unistd.h wctype.h)

DL_LIB=
AC_CHECK_FUNCS(dlopen,,[
  AC_CHECK_LIB(dl, dlopen, DL_LIB="-ldl",[
    AC_CHECK_LIB(dld, shl_load, DL_LIB="-ldld",[
      AC_CHECK_FUNCS(dlopen, DL_LIB="",
      AC_MSG_ERROR(Dynamic linking is not available on this platform.  Some apps,
like panel, will not run properly.))
    ])
  ])
])

oLIBS="$LIBS"
LIBS="$LIBS $DL_LIB"
AC_CHECK_FUNCS(dlerror,,)
LIBS="$oLIBS"
AC_SUBST(DL_LIB)

### --------------------------------------------------------------------------
### MacOS 10.x requires some special handling...

# Some systems (MacOS) require -lintl
# not true for darwin 10.3 - halts the build.
#AC_SEARCH_LIBS(gettext, intl, ,[
#	AC_MSG_ERROR([Cannot find gettext -- do you need to build -lintl?])], )

AC_MSG_CHECKING(for darwin)
case $host_os in
	rhapsody* | darwin1*)
		AC_MSG_RESULT([yes, but too old])
		AC_MSG_ERROR([This platform is not supported, please
update to latest  darwin])
		;;
	darwin*)
     dnl Use fink under MacOS X to find popt
     AC_MSG_CHECKING(for fink support)
     if test -d "/sw/lib" -a -d "/sw/include"; then
        AM_CFLAGS="$AM_CFLAGS -I/sw/include"
        LDFLAGS="$LDFLAGS -L/sw/lib"
        AC_MSG_RESULT(yes)
    else
        AC_MSG_RESULT(no)
    fi
    AC_CHECK_HEADERS(popt.h)
		AC_MSG_RESULT([yes, patching libtool to always build dylibs])
		mv libtool libtool.old
		sed -e 's/^deplibs_check_method.*/deplibs_check_method=pass_all/g' \
			-e 's|^archive_cmds.*|archive_cmds="$CC -dynamiclib \\$allow_undefined_flag -o \\$lib \\$libobjs \\$deplibs\\$linker_flags -install_name \\$rpath/\\$soname \\$verstring"|g' \
			-e 's|^library_names_spec.*|library_names_spec="\\$libname\\$release\\$versuffix.dylib \\$libname\\$release\\${major}.dylib \\$libname.dylib"|g' \
			-e 's|^soname_spec.*|soname_spec="\\$libname\\$release\\$major.dylib"|g' \
			< libtool.old > libtool
		rm libtool.old
		;;
	*)
		AC_MSG_RESULT(no)
		;;	
esac


### --------------------------------------------------------------------------
### Guile and g-wrap version checks (should this be something other than
### the Gnome check?)

GUILE_LIBS=""
GNOME_CHECK_GUILE
AS_SCRUB_INCLUDE(GUILE_INCS)
AC_SUBST(GUILE_LIBS)

### --------------------------------------------------------------------------
### G-wrap (libraries and executable)

G_WRAP_COMPILE_ARGS=""
G_WRAP_LINK_ARGS=""

AM_GUILE_VERSION_CHECK(1.6, , , [AC_MSG_ERROR([

  guile  does not appear to be installed correctly, or is not in the
  correct version range.  Perhaps you have not installed the guile
  development packages?  Gnucash requires at least version 1.6 to build.
])])

AC_DEFINE_UNQUOTED(GNC_GUILE_MAJOR_VERSION, ${guile_major_version},
	[Guile Major version number])
AC_DEFINE_UNQUOTED(GNC_GUILE_MINOR_VERSION, ${guile_minor_version},
	[Guile Minor version number])
AC_DEFINE_UNQUOTED(GNC_GUILE_MICRO_VERSION, ${guile_micro_version},
	[Guile Micro version number])

AM_PATH_GWRAP(1.3.3, , , [AC_MSG_ERROR([

  g-wrap does not appear to be installed correctly, or is not new
  enough.  Right now gnucash requires at least version 1.3.3 to build.
  If you need to install g-wrap, you can find it at
  http://www.gnucash.org/pub/g-wrap.
])])

# Find out what the g-wrap compile and link flags are.
AC_MSG_CHECKING(for g-wrap compile args)
G_WRAP_COMPILE_ARGS=`${G_WRAP_CONFIG} --c-compile-args guile`
AC_MSG_RESULT($G_WRAP_COMPILE_ARGS)

AC_MSG_CHECKING(for g-wrap link args)
G_WRAP_LINK_ARGS=`${G_WRAP_CONFIG} --c-link-args guile`
AC_MSG_RESULT($G_WRAP_LINK_ARGS)

AC_MSG_CHECKING(for g-wrap module directory)
G_WRAP_MODULE_DIR=`${G_WRAP_CONFIG} --guile-module-directory`
G_WRAP_LIB_DIR=`echo $G_WRAP_MODULE_DIR | sed -e 's|share/guile|lib|'`
AC_MSG_RESULT($G_WRAP_MODULE_DIR)

AC_GWRAP_CHECK_GUILE($G_WRAP_MODULE_DIR)

AC_SUBST(G_WRAP_CONFIG)
AC_SUBST(G_WRAP_COMPILE_ARGS)
AC_SUBST(G_WRAP_LINK_ARGS)
AC_SUBST(G_WRAP_MODULE_DIR)
AC_SUBST(G_WRAP_LIB_DIR)

AS_SCRUB_INCLUDE(CFLAGS)

### Check size of long_long - some guile's are broken.
AC_MSG_CHECKING(if guile long_long is at least as big as gint64)
GNC_OLDCFLAGS="$CFLAGS"
CFLAGS="${GNOME_CFLAGS} ${GUILE_INCS} ${CFLAGS} ${GLIB_CFLAGS}"
AC_TRY_RUN([
  #include <glib.h>
  #include <libguile/__scm.h>
  int main(int argc, char *argv[]) {
    return(!(sizeof(long_long) >= sizeof(gint64)));
  }
],[
  AC_MSG_RESULT(yes)
  AC_CHECK_LIB(guile, scm_long_long2num,
               AC_DEFINE(GUILE_LONG_LONG_OK,1,is sizeof(long_long) >=
	       sizeof(gint64)))
],[
  AC_MSG_RESULT(no)
])
CFLAGS="$GNC_OLDCFLAGS"

# One of the places this is critical is in gnc_scm_to_gint64 and inverse.
# However, I'm sure we require this elsewhere, so don't remove this test
# unless you've done sufficient code review/testing.
AC_MSG_CHECKING(if unsigned long is at least as big as guint32)
GNC_OLDCFLAGS="$CFLAGS"
CFLAGS="${GNOME_CFLAGS} ${GUILE_INCS} ${CFLAGS} ${GLIB_CFLAGS}"
AC_TRY_RUN([
  #include <glib.h>
  int main(int argc, char *argv[]) {
    return(!(sizeof(unsigned long) >= sizeof(guint32)));
  }
],[
  AC_MSG_RESULT(yes)
],[
  AC_MSG_RESULT(no)
  AC_MSG_ERROR(cannot continue, size of unsigned long too small.)
])
CFLAGS="$GNC_OLDCFLAGS"


### --------------------------------------------------------------------------
### Check which SRFIs we need.

GNC_ADD_ON_SRFIS=""

for f in ${srcdir}/lib/srfi/srfi-*.scm
do
  srfi=`echo $f | sed 's%.*/%%' | sed 's/.scm//'`
  AC_MSG_CHECKING(if guile needs our copy of ${srfi})
  if ${GUILE} -c "(use-modules (srfi ${srfi}))" > /dev/null 2>&1
  then
    AC_MSG_RESULT(no)
  else
    GNC_ADD_ON_SRFIS="${GNC_ADD_ON_SRFIS} ${srfi}.scm"
    AC_MSG_RESULT(yes)
  fi
done

AC_SUBST(GNC_ADD_ON_SRFIS)
if test "x${GNC_ADD_ON_SRFIS}" != "x"
then
 GNC_SRFI_LOAD_PATH="\${top_srcdir}/lib/"
 GNC_TEST_SRFI_LOAD_CMD="--guile-load-dir \${top_srcdir}/lib/"
else
 GNC_SRFI_LOAD_PATH=""
 GNC_TEST_SRFI_LOAD_CMD=""
fi
AC_SUBST(GNC_SRFI_LOAD_PATH)
AC_SUBST(GNC_TEST_SRFI_LOAD_CMD)

### --------------------------------------------------------------------------
### See if we need guile-www

gnc_have_guile_www=no

AC_MSG_CHECKING([if guile needs our copy of (guile www)])
if ${GUILE} -c "(use-modules (www main))" > /dev/null 2>&1
then
  gnc_have_guile_www=yes
  AC_MSG_RESULT(no)
else
  gnc_have_guile_www=no
  AC_MSG_RESULT(yes)
fi

### --------------------------------------------------------------------------
### See if we have pkg-config

AC_PATH_PROG(PKG_CONFIG,pkg-config)
if test "x$PKG_CONFIG" = x; then
    # @*%&$ Ximian programmers renamed this application
    AC_PATH_PROG(PKG_CONFIG,pkgconfig)
    if test "x$PKG_CONFIG" = x; then
    	AC_MSG_ERROR([Could not find the pkg-config (or pkgconfig) program.],)
    fi
fi

PKG_CHECK_MODULES(GCONF, gconf-2.0 >= "2.0")
AC_SUBST(GCONF_CFLAGS)
AC_SUBST(GCONF_LIBS)

AM_CONDITIONAL(GNC_HAVE_GUILE_WWW, test "${gnc_have_guile_www}" = yes)

### --------------------------------------------------------------------------
### Gnome XML -- GNOME_XML_LIB is defined by GNOME_XML_CHECK

PKG_CHECK_MODULES(XML, libxml-2.0)
AS_SCRUB_INCLUDE(XML_CFLAGS)
AC_SUBST(XML_CFLAGS)
AC_SUBST(XML_LIBS)

LIBXML2_REQUIRED=2.5.10
PKG_CHECK_MODULES(LIBXML2, libxml-2.0 >= $LIBXML2_REQUIRED)
LIBXML_VERSION=`$PKG_CONFIG --version xml`
AC_SUBST(LIBXML2_CFLAGS)
AC_SUBST(LIBXML2_LIBS)
AC_SUBST(LIBXML_VERSION)

oLIBS="$LIBS"
LIBS="$LIBS $XML_LIBS"

# Compile a test program to verify that the xml2 library works.
AC_CHECK_LIB(xml2, xmlElemDump, [:], [
  AC_MSG_ERROR([libxml2 is required for GnuCash])
  ])
LIBS="$oLIBS"

dnl *************************************
dnl QOF
dnl *************************************

AC_ARG_WITH(qof, [  --with-qof=path         prefix for Query Object Framework - QOF (auto)])
QOF_REQUIRED=0.6.0
AC_PATH_PROG(PKG_CONFIG, pkg-config, no)
if test pkg-config = no; then
        AC_MSG_ERROR([Please install pkgconfig])
        exit 1
fi
AC_MSG_CHECKING([for QOF, version >= $QOF_REQUIRED])
if test "$withval" != "yes"; then
        QOF=`$PKG_CONFIG --exists '$withval/lib/pkgconfig/qof-1.pc >= $QOF_REQUIRED'`
        QOF_LIBS=`$PKG_CONFIG --libs $withval/lib/pkgconfig/qof-1.pc`
        QOF_CFLAGS=`$PKG_CONFIG --cflags $withval/lib/pkgconfig/qof-1.pc`
        QOF_VERSION=`$PKG_CONFIG --modversion $withval/lib/pkgconfig/qof-1.pc`
        QOF_PREFIX=`$PKG_CONFIG --variable=prefix $withval/lib/pkgconfig/qof-1.pc`
        QOF_LIB_DIR=`$PKG_CONFIG --variable=libdir $withval/lib/pkgconfig/qof-1.pc`
        QOF_XML_DIR=`$PKG_CONFIG --variable=xmldir $withval/lib/pkgconfig/qof-1.pc`
else
        QOF=`$PKG_CONFIG --exists 'qof-1 >= $QOF_REQUIRED'`
        QOF_LIBS=`$PKG_CONFIG --libs qof-1`
        QOF_CFLAGS=`$PKG_CONFIG --cflags qof-1`
        QOF_VERSION=`$PKG_CONFIG --modversion qof-1`
        QOF_PREFIX=`$PKG_CONFIG --variable=prefix qof-1`
        QOF_LIB_DIR=`$PKG_CONFIG --variable=libdir qof-1`
        QOF_XML_DIR=`$PKG_CONFIG --variable=xmldir qof-1`
fi
if test x$QOF_XML_DIR = x; then
        AC_MSG_RESULT([no, will use internal QOF code])
	QOF_CFLAGS="-I\${top_srcdir}/src/engine"
	QOF_LIBS="\${top_srcdir}/src/engine/libqof.la"
	QOF_LIB_DIR="${libdir}"
	QOF_PREFIX="internal"
	QOF_XML_DIR="$QSF_SCHEMA_DIR"
	LIBQOF_LIBRARY_VERSION=1:1:0
	LIBQOF_BACKEND_QSF_LIBRARY_VERSION=0:0:0
	AC_SUBST(LIBQOF_LIBRARY_VERSION)
	AC_SUBST(LIBQOF_BACKEND_QSF_LIBRARY_VERSION)
  	AC_DEFINE(HAVE_LIBQOF,,[We will use the internal QOF code])
	HAVE_LIBQOF="use_internal"
else
        AC_MSG_RESULT([yes])
fi
AM_CONDITIONAL(USE_LIBQOF, [test x$HAVE_LIBQOF != x])
AM_CONDITIONAL(USE_LIBQOF, [test $HAVE_LIBQOF = "use_internal"])
AC_SUBST(USE_LIBQOF)
AC_SUBST(QOF_CFLAGS)
AC_SUBST(QOF_LIBS)
AS_SCRUB_INCLUDE(QOF_PREFIX)
AC_SUBST(QOF_PREFIX)
AC_SUBST(QOF_LIB_DIR)

### --------------------------------------------------------------------------
### for GOG/goffice...

dnl If libgoffice-1 >= 0.0.3-2 and libgsf >= 1.12.0-1 are found, 
dnl we need to omit lib/goffice and use the external library
dnl because lib/goffice will not build with newer versions of libgsf.

GOFFICE_REQUIRED=0.0.3
GSF_REQUIRED=1.12.0
AC_MSG_CHECKING([for GOffice and GSF])
LIBGOFFICE_CFLAGS=`$PKG_CONFIG --silence-errors --cflags " libgoffice-1 >= $GOFFICE_REQUIRED"`
LIBGOFFICE_LIBS=`$PKG_CONFIG --silence-errors --libs " libgoffice-1 >= $GOFFICE_REQUIRED"`
LIBGSF_CFLAGS=`$PKG_CONFIG --silence-errors --cflags " libgsf-1 >= $GSF_REQUIRED"`
LIBGSF_LIBS=`$PKG_CONFIG --silence-errors --libs " libgsf-1 >= $GSF_REQUIRED"`
AC_SUBST(LIBGOFFICE_CFLAGS)
AC_SUBST(LIBGOFFICE_LIBS)

if test x"$LIBGOFFICE_LIBS" = x; then
  AC_MSG_RESULT([No, GOffice not found, will build using internal goffice library.])
else
  AC_MSG_RESULT([Yes, using GOffice])
  AC_DEFINE(HAVE_GOFFICE,,[We have found an external GOffice])
  HAVE_GOFFICE="use_external"
fi
AM_CONDITIONAL(USE_LIBGOFFICE, [test x$HAVE_GOFFICE != x])
AM_CONDITIONAL(USE_LIBGOFFICE, [test $HAVE_GOFFICE = "use_external"])
AC_SUBST(USE_LIBGOFFICE)

###
### GSF
PKG_CHECK_MODULES(GSF, libgsf-1 >= 1.8.0
                       libgsf-gnome-1 >= 1.8.0)
AC_SUBST(GSF_LIBS)
AC_SUBST(GSF_CFLAGS)

### libart
PKG_CHECK_MODULES(ART, libart-2.0 >= 2.3.11)
AC_SUBST(ART_LIBS)
AC_SUBST(ART_CFLAGS)

### silence convention-difference from cut-n-paste'd gnumeric Makefile.am's:
AM_CONDITIONAL(WITH_GNOME, true)
# for lib/goffice/graph/plugins/plot_barcol/gog-1.5d.c's use of
# goffice/graph/gog-error-bar.h:gog_error_bar_prefs, which is bounded by
# WITH_GTK for some reason... :/
AM_CONDITIONAL(WITH_GTK, true)
AC_DEFINE(WITH_GTK, 1, [Define if UI is built])

### gnome-print [= gnome-font]
PKG_CHECK_MODULES(PRINT, libgnomeprint-2.2 >= 2.5.2)
AC_SUBST(PRINT_LIBS)
AC_SUBST(PRINT_CFLAGS)

AC_ARG_VAR(GLIB_GENMARSHAL, [Absolute path of the glib-genmarshal executable.])
AC_PATH_PROG(GLIB_GENMARSHAL, glib-genmarshal)

gog_plugindir='${GNC_LIBDIR}/plugins'
# do this to minimize changes in the plugin makefiles...
gnumeric_plugindir='${gog_plugindir}'
AC_SUBST(gog_plugindir)
AC_SUBST(gnumeric_plugindir)
#AC_SUBST(pkglibdir) -- this breaks builds into /opt.
gnumeric_icondir='${GNC_SHAREDIR}/pixmaps'
AC_SUBST(gnumeric_icondir)


# /GOG

### --------------------------------------------------------------------------
### Variables
### Set up all the initial variable values...

AC_ARG_ENABLE(opt-style-install,
  [  --enable-opt-style-install   install everything in subdirs of --prefix],
  [case "${enableval}" in
     yes) gnc_opt_style_install=true ;;
     no)  gnc_opt_style_install=false ;;
     *) AC_MSG_ERROR(bad value ${enableval} for --enable-opt-style-install) ;;
   esac],
   [gnc_opt_style_install=false])

if test x${gnc_opt_style_install} = xtrue
then
  GNC_CONFIGDIR='${sysconfdir}'
  GNC_DOC_INSTALL_DIR='${datadir}/doc'
  GNC_INCLUDE_DIR='${includedir}'
  GNC_LIBDIR='${libdir}'
  GNC_SCM_INSTALL_DIR='${datadir}/scm'
  GNC_SHAREDIR='${datadir}'
  GNC_LIBEXECDIR='${libexecdir}'
else
  GNC_CONFIGDIR='${sysconfdir}/gnucash'
  GNC_DOC_INSTALL_DIR='${pkgdatadir}/doc'
  GNC_INCLUDE_DIR='${includedir}/gnucash'
  GNC_LIBDIR='${pkglibdir}'
  GNC_SCM_INSTALL_DIR='${pkgdatadir}/scm'
  GNC_SHAREDIR='${pkgdatadir}'
  GNC_LIBEXECDIR='${libexecdir}/gnucash'
fi

GNC_ACCOUNTS_DIR='${GNC_SHAREDIR}/accounts'
GNC_GLADE_DIR='${GNC_SHAREDIR}/glade'
QSF_SCHEMA_DIR='${GNC_SHAREDIR}/xml/qsf'
GNC_UI_DIR='${GNC_SHAREDIR}/ui'
GNC_GWRAP_LIBDIR='${GNC_SHAREDIR}/guile-modules/g-wrapped'
GNC_MODULE_DIR='${pkglibdir}'
GNC_PIXMAP_DIR='${GNC_SHAREDIR}/pixmaps'

AC_SUBST(GNC_ACCOUNTS_DIR)
AC_SUBST(GNC_CONFIGDIR)
AC_SUBST(GNC_DOC_INSTALL_DIR)
AC_SUBST(GNC_GLADE_DIR)
AC_SUBST(QSF_SCHEMA_DIR)
AC_SUBST(GNC_UI_DIR)
AC_SUBST(GNC_GWRAP_LIBDIR)
AC_SUBST(GNC_INCLUDE_DIR)
AC_SUBST(GNC_LIBDIR)
AC_SUBST(GNC_MODULE_DIR)
AC_SUBST(GNC_PIXMAP_DIR)
AC_SUBST(GNC_SCM_INSTALL_DIR)
AC_SUBST(GNC_SHAREDIR)
AC_SUBST(GNC_LIBEXECDIR)

AC_ARG_ENABLE( debug,
  [  --enable-debug               compile with debugging flags set],
       CFLAGS="${CFLAGS} -g"
       LDFLAGS="${LDFLAGS} -g"
       AC_DEFINE(DEBUG_MEMORY,1,Enable debug memory),
       AC_DEFINE(DEBUG_MEMORY,0,Enable debug memory) )

AC_ARG_ENABLE( profile,
  [  --enable-profile             compile with profiling set],
       CFLAGS="${CFLAGS} -pg"
       LDFLAGS="${LDFLAGS} -pg")

AC_ARG_ENABLE( ref-counts-dumps,
  [  --enable-ref-counts-dumps     compile with ref count dumps],
       AC_DEFINE(DEBUG_REFERENCE_COUNTING,1,Enable reference count dumps),
       AC_DEFINE(DEBUG_REFERENCE_COUNTING,0,Enable reference count dumps) )


### --------------------------------------------------------------------------
### SQL
# Check to see if the user wants to have Postgres support
#
# hack alert ... we should use 'pg_config --includedir' and
# 'pg_config --libdir' to find paths; unfortunately pg_config itself
# is hard to find :-(

AC_ARG_ENABLE( sql,
  [  --enable-sql                 compile with sql support],
  [
    PG_CONFIG=`which pg_config`

    if test "x$PG_CONFIG" = "x" ; then
        PG_CONFIG="/usr/lib/postgresql/bin/pg_config"
    fi

    if test "x$enableval" != "xno" ; then

      PGSQL_CFLAGS=`${PG_CONFIG} --includedir`
      if test "x$PGSQL_CFLAGS" != x; then
	PGSQL_CFLAGS="-I${PGSQL_CFLAGS}"
      fi

      AS_SCRUB_INCLUDE(PGSQL_CFLAGS)

      saved_CPPFLAGS="${CPPFLAGS}"
      CPPFLAGS="${CPPFLAGS} ${PGSQL_CFLAGS}"
      AC_CHECK_HEADERS(pgsql/libpq-fe.h postgresql/libpq-fe.h libpq-fe.h)
      if test "x$ac_cv_header_pgsql_libpq_fe_h$ac_cv_header_postgresql_libpq_fe_h$ac_cv_header_libpq_fe_h" = xnonono; then
         AC_MSG_ERROR([Cannot find PostgreSQL headers; won't build sql backend]) 
      else 
        master_dirs="/usr/include /usr/local/include"
        if test "x$ac_cv_header_pgsql_libpq_fe_h" != xno; then
          for dir in $master_dirs; do
              if test -f "$dir/pgsql/libpq-fe.h"; then
                  CFLAGS="${CFLAGS} -I$dir/pgsql"
                  break
	      fi
          done
        elif test "x$ac_cv_header_postgresql_libpq_fe_h" != xno; then
          for dir in $master_dirs; do
              if test -f "$dir/postgresql/libpq-fe.h"; then
                  CFLAGS="${CFLAGS} -I$dir/postgresql"
                  break
              fi
          done
	fi

	AS_SCRUB_INCLUDE(CFLAGS)
        
        PGSQL_LIBS=`${PG_CONFIG} --libdir`
        if test "x$PGSQL_LIBS" != x; then
	  PGSQL_LIBS="-L${PGSQL_LIBS}"
        fi

        saved_LIBS="$LIBS"
 	LIBS="${PGSQL_LIBS} -lpq $LIBS"
        AC_MSG_CHECKING(for libpq)
        AC_TRY_LINK(
          [
	    #include <libpq-fe.h>
	  ],
          [
             PQconnectdb("asdf");
          ],
          [
            AC_MSG_RESULT(yes)
            SQL_DIR=postgres
          ],
          [
            AC_MSG_ERROR([Cannot find PostgreSQL libraries; will not build sql backend]) 
          ]
        )
        LIBS="$saved_LIBS"
      fi
      CPPFLAGS="$saved_CPPFLAGS"
    fi
  ]
)

AC_SUBST(PGSQL_CFLAGS)
AC_SUBST(PGSQL_LIBS)
AC_SUBST(SQL_DIR)


### --------------------------------------------------------------------------
### RPC
AC_ARG_ENABLE( rpc,
  [  --enable-rpc                 compile with rpc support],
  [
       AC_MSG_WARN([

       The RPC Backend is depricated.  You should not use it.
       It may go away in the future.  If you need it, please
       contact gnucash-devel@gnucash.org and let the developers
       know.
       ])
       RPC_DIR=rpc
  ])

AC_SUBST(RPC_DIR)

### --------------------------------------------------------------------------
### OFX
AC_ARG_ENABLE( ofx,
  [  --enable-ofx                 compile with ofx support (needs LibOFX)],
  if test "x$enableval" != "xno" ; then
    OFX_DIR=ofx
  fi)
if test x${OFX_DIR} = xofx ;
then
    AC_ARG_WITH( ofx-prefix, 
	[  --with-ofx-prefix=DIR     specify where to look for libOFX],
       OFXPREFIX="$with_ofx_prefix" )

    if test x${OFXPREFIX} != x ; then
    	LIBOFX_LIBS="-L${OFXPREFIX}/lib"
	LIBOFX_CFLAGS="-I${OFXPREFIX}/include"
    fi

    ### Check libofx version
    # Obtain version string
    AC_MSG_CHECKING(for libofx version >= 0.7.0)
    if test x${OFXPREFIX} = x ; then
	ofx_version_output=`ofxdump --version`
    else
	ofx_version_output=`${OFXPREFIX}/bin/ofxdump --version`
    fi
    # Extract version number; output format changed from 0.6.x to 0.7.x
    LIBOFX_VERSION=`echo ${ofx_version_output} | sed 's/\([[^0-9]]*\)\([[0-9]]*\.\)/\2/' `
    LIBOFX_VERSION_MAJOR=`echo ${LIBOFX_VERSION} | cut -d. -f1`
    LIBOFX_VERSION_MINOR=`echo ${LIBOFX_VERSION} | cut -d. -f2`
    # Make sure the numbers are not empty
    if test x${LIBOFX_VERSION_MAJOR} = x ; then
	LIBOFX_VERSION_MAJOR=0
    fi
    if test x${LIBOFX_VERSION_MINOR} = x ; then
	LIBOFX_VERSION_MINOR=0
    fi
    # Now check for >= 0.7.x or >= 1.x.x
    if test "${LIBOFX_VERSION_MAJOR}" -ge 1 -o \
	     "${LIBOFX_VERSION_MINOR}" -ge 7; then
	# This is libofx >= 0.7.x
	AC_MSG_RESULT([found ${LIBOFX_VERSION}])
    else
	AC_MSG_ERROR([found ${LIBOFX_VERSION}; Libofx 0.7.0 or newer needed for ofx support])
    fi

    # Version number verified. Now check header files.
    AC_MSG_CHECKING(for libofx/libofx.h)
    AS_SCRUB_INCLUDE(LIBOFX_CFLAGS)
    save_CPPFLAGS="${CPPFLAGS}"
    CPPFLAGS="${CPPFLAGS} ${LIBOFX_CFLAGS}"
    AC_TRY_CPP( [#include <libofx/libofx.h>], AC_MSG_RESULT(yes), OFXfound=no )

    if test x${OFXPREFIX} = x -a x${OFXfound} = no ; then
    	LIBOFX_LIBS="-L$/usr/local/lib"
	LIBOFX_CFLAGS="-I$/usr/local/include"
        AS_SCRUB_INCLUDE(LIBOFX_CFLAGS)
        CPPFLAGS="${CPPFLAGS} ${LIBOFX_CFLAGS}"

        AC_TRY_CPP( [#include <libofx/libofx.h>], AC_MSG_RESULT([yes (in /usr/local)]),
	  [ AC_MSG_ERROR([cannot find libofx header, needed for OFX support.]) ] )
    fi

    LIBOFX_LIBS="${LIBOFX_LIBS} -lofx"
    AC_MSG_CHECKING(for libofx)
    save_LIBS="${LIBS}"
    LIBS="${LIBS} ${LIBOFX_LIBS}"
    AC_TRY_LINK( [
#include <libofx/libofx.h>
	], [
	LibofxContextPtr libofx_context = libofx_get_new_context();
	libofx_free_context(libofx_context);
	], AC_MSG_RESULT(yes),
	[ AC_MSG_ERROR([*** Cannot compile test program for libofx library. Please check config.log for the exact error.]) ] )

    LIBS="${save_LIBS}"
    CPPFLAGS="${save_CPPFLAGS}"
    AC_SUBST(LIBOFX_CFLAGS)
    AC_SUBST(LIBOFX_LIBS)
fi
AC_SUBST(OFX_DIR)

### --------------------------------------------------------------------------
### MT940
AC_ARG_ENABLE( mt940,
  [  --enable-mt940               compile with MT940 support (needs --enable-hbci)],
  if test "x$enableval" != "xno" ; then
    MT940_DIR=mt940
  fi)
if test x${MT940_DIR} = xmt940 ;
then
    AC_MSG_ERROR([Sorry, MT940 support is currently broken since it depended on the hbci module with the old openhbci 0.9.x library. But the hbci module has now been ported to a new hbci library called aqbanking. That library contains a MT940 parser, too, so porting shouldnt be too difficult. Nevertheless someone needs to do the porting work before this can be enabled again.])
    AC_AQBANKING(1,0,0)
    # Note: HBCI_LIBS is changed again below in the --enable-hbci
    # section. So check for mt940 first and *not* the other way round!
    HBCI_LIBS="${OPENHBCI_LIBS}"
    HBCI_CFLAGS="${OPENHBCI_CFLAGS}"

    AS_SCRUB_INCLUDE(HBCI_CFLAGS)
    AC_SUBST(HBCI_LIBS)
    AC_SUBST(HBCI_CFLAGS)
fi
AC_SUBST(MT940_DIR)

### --------------------------------------------------------------------------
### HBCI
AC_ARG_ENABLE( hbci,
  [  --enable-hbci                compile with HBCI support (needs AqBanking)],
  if test "x$enableval" != "xno" ; then
    HBCI_DIR=hbci
  fi)
if test x${HBCI_DIR} = xhbci ;
then
    # Check for Aqbanking library
    AC_AQBANKING(1,0,0)
    if test x${have_aqbanking} != xyes; 
    then
	AC_MSG_ERROR([Could not find aqbanking. If you use --enable-hbci, you *have* to enable aqbanking.])
    fi
    HBCI_LIBS="${aqbanking_libs}"
    HBCI_CFLAGS="${aqbanking_includes}"

    # also check for ktoblzcheck
    AC_CHECK_HEADERS(ktoblzcheck.h)
    if test "x$ac_cv_header_ktoblzcheck_h" != xno; then
	HBCI_LIBS="${HBCI_LIBS} -lktoblzcheck"  
    fi

    AS_SCRUB_INCLUDE(HBCI_CFLAGS)
    AC_SUBST(HBCI_LIBS)
    AC_SUBST(HBCI_CFLAGS)
fi
AC_SUBST(HBCI_DIR)

### --------------------------------------------------------------------------
### i18n

AC_ARG_WITH( locale-dir, 
  [  --with-locale-dir=PATH       specify where to look for locale-specific information],
  LOCALE_DIR="$with_locale_dir",
  LOCALE_DIR="\${prefix}/share/locale")

AC_SUBST(LOCALE_DIR)

dnl check for nl_langinfo(D_FMT) which is missing on FreeBSD
LANGINFO_D_FMT_CHECK

dnl Enable locale-specific tax-related information in the accounts
AC_ARG_ENABLE( locale-specific-tax,
  [  --enable-locale-specific-tax enable localized tax categories (experimental)],
  AC_DEFINE(LOCALE_SPECIFIC_TAX,1,Enable the experimental locale-specific tax categories) )

### --------------------------------------------------------------------------
### help files

# Used to initialize doc-path.
AC_ARG_WITH( help-prefix,
  [  --with-help-prefix=PATH      specify where to store the help files],
  GNC_HELPDIR="$with_help_prefix",
  GNC_HELPDIR="\${datadir}")

AC_SUBST(GNC_HELPDIR)


### --------------------------------------------------------------------------
### Check for etags

AC_ARG_ENABLE( etags,
  [  --enable-etags               enable automatic create of TAGS file],
  if test $enableval = yes; then
     USE_ETAGS=1
  fi,
  USE_ETAGS=0)

if test ${USE_ETAGS} = 1; then
  AC_CHECK_PROG(GNC_ETAGS_FILE, etags, TAGS)
fi

AM_CONDITIONAL(GNC_ETAGS_FILE, test x${GNC_ETAGS_FILE} = xTAGS)

### --------------------------------------------------------------------------
### Check for ctags

AC_ARG_ENABLE( ctags,
  [  --enable-ctags               enable automatic create of tags file],
  if test $enableval = yes; then
     USE_CTAGS=1
  fi,
  USE_CTAGS=0)

if test ${USE_CTAGS} = 1; then
  AC_CHECK_PROG(GNC_CTAGS_FILE, ctags, tags)
fi

AM_CONDITIONAL(GNC_CTAGS_FILE, test x${GNC_CTAGS_FILE} = xtags)

### --------------------------------------------------------------------------
### Check for perl

# Check for perl, force version 5
AC_ARG_WITH(perl,
  [  --with-perl=FILE             which perl executable to use ],
  PERL="${with_perl}")

# If the user didn't specify a perl, then go fetch.
if test x"$PERL" = x;
then
  AC_PATH_PROG(PERL, perl)
fi

# Make sure Perl was found
if test x"$PERL" = x; then
    AC_MSG_ERROR([Cannot find Perl. Try using the --with-perl flag.])
fi

# Make sure it's version 5 or later
if "$PERL" -e 'exit 1 if $] < 5.0'; then
	:
else
    AC_MSG_ERROR([Found ${PERL} reports version ]
                 [`${PERL} -e 'print $]'`, need version 5.*])
fi
AC_SUBST(PERL)

# Now check for perl headers
# This appears to be what Perl's ExtUtils::MakeMaker module does, so
# I'm reasonably sure it's correct.
# PERLINCL="/usr/lib/perl5/i386-linux/5.00404"
#
PERLINCL=`$PERL -MConfig -e 'print $Config{"archlibexp"}'`
AC_ARG_WITH( perl-includes, 
  [  --with-perl-includes=DIR     specify where to look for perl includes],
       PERLINCL="$with_perl_includes" )
AC_SUBST(PERLINCL)

# check for doxygen, mostly stolen from http://log4cpp.sourceforge.net/
# ----------------------------------------------------------------------------
AC_DEFUN([BB_ENABLE_DOXYGEN],
[
AC_ARG_ENABLE(doxygen, [  --enable-doxygen        enable documentation generation with doxygen (auto)])
AC_ARG_ENABLE(dot, [  --enable-dot            use 'dot' to generate graphs in doxygen (auto)])              
AC_ARG_ENABLE(html-docs, [  --enable-html-docs      enable HTML generation with doxygen (yes)], [], [ enable_html_docs=yes])              
AC_ARG_ENABLE(latex-docs, [  --enable-latex-docs     enable LaTeX documentation generation with doxygen (no)], [], [ enable_latex_docs=no])              
if test "x$enable_doxygen" = xno; then
        enable_doc=no
else 
        AC_PATH_PROG(DOXYGEN, doxygen, , $PATH)
        if test x$DOXYGEN = x; then
                if test "x$enable_doxygen" = xyes; then
                        AC_MSG_ERROR([could not find doxygen])
                fi
                enable_doc=no
        else
                enable_doc=yes
                AC_PATH_PROG(DOT, dot, , $PATH)
        fi
fi
AM_CONDITIONAL(DOC, test x$enable_doc = xyes)

if test x$DOT = x; then
        if test "x$enable_dot" = xyes; then
                AC_MSG_ERROR([could not find dot])
        fi
        enable_dot=no
else
        enable_dot=yes
fi
AM_CONDITIONAL(ENABLE_DOXYGEN, test x$enable_doc = xtrue)
AC_SUBST(enable_dot)
AC_SUBST(enable_html_docs)
AC_SUBST(enable_latex_docs)
])

# check for doxygen
# ----------------------------------------------------------------------------
BB_ENABLE_DOXYGEN


### --------------------------------------------------------------------------
### Libraries
LIBS="$LIBS -lm"


### --------------------------------------------------------------------------
### popt
AC_CHECK_HEADERS(popt.h)
AC_CHECK_LIB(popt, poptStrippedArgv,, [AC_MSG_ERROR([

  popt 1.5 or newer is required to build gnucash. You can download
  the latest version from ftp://people.redhat.com/sopwith/popt/, or if
  you're running Debian, install the libpopt-dev package.
])])


### --------------------------------------------------------------------------
### GNOME gui components -- built by default, but not if --enable-gui=no
### or --disable-gui is passed to autogen 
### --------------------------------------------------------------------------

AC_ARG_ENABLE(gui, 
  [  --disable-gui                  build without the GNOME GUI components of Gnucash],
  [case "${enableval}" in
     yes) gnc_build_gui=true ;;
     no)  gnc_build_gui=false ;;
     *) gnc_build_gui=true ;;
   esac],
   [gnc_build_gui=true])

AM_CONDITIONAL(GNUCASH_ENABLE_GUI, test x${gnc_build_gui} = xtrue)

if test x${gnc_build_gui} = xtrue ;
then
    GNOME_COMPILE_WARNINGS

    # Look for libgnomeui by pkg-config
    PKG_CHECK_MODULES(GTK, gtk+-2.0 >= 2.4)
    AS_SCRUB_INCLUDE(GTK_CFLAGS)
    AC_SUBST(GTK_CFLAGS)
    AC_SUBST(GTK_LIBS)

    PKG_CHECK_MODULES(GNOME, libgnomeui-2.0 >= 2.4)
    AS_SCRUB_INCLUDE(GNOME_CFLAGS)

    AC_SUBST(GNOME_CFLAGS)
    AC_SUBST(GNOME_LIBS)

    # look for gdk-pixbuf
    PKG_CHECK_MODULES(GDK_PIXBUF, gdk-pixbuf-2.0)
    AS_SCRUB_INCLUDE(GDK_PIXBUF_CFLAGS)
    AC_SUBST(GDK_PIXBUF_CFLAGS)
    AC_SUBST(GDK_PIXBUF_LIBS)

    # Gnome-Print -- some people already have/need libgnomeprint-2.2
    # Yep, so lets test to see if it is 2.0 or 2.2, 
    # Anyone know why gnome has versions in the package config? 
    # Why cant I just ask for the version of 'libgnomeprint'

    PKG_CHECK_MODULES(GNOME_PRINT, libgnomeprint-2.2 libgnomeprintui-2.2)
    AS_SCRUB_INCLUDE(GNOME_PRINT_CFLAGS)
    AC_SUBST(GNOME_PRINT_CFLAGS)
    AC_SUBST(GNOME_PRINT_LIBS)

    # There is no libguppi.  Just substitute empty stuff
    LIBGUPPI_CFLAGS=
    LIBGUPPI_LIBS=
    AC_SUBST(LIBGUPPI_CFLAGS)
    AC_SUBST(LIBGUPPI_LIBS)
    
    ### ----------------------------------------------------------------------
    ### Check for glade, gtkhtml
    
    PKG_CHECK_MODULES(GLADE, libglade-2.0 >= 2.4)
    AS_SCRUB_INCLUDE(GLADE_CFLAGS)
    AC_SUBST(GLADE_CFLAGS)
    AC_SUBST(GLADE_LIBS)

    # check for gtkhtml 3.x
    gtkhtml=0
    PKG_CHECK_MODULES(GTKHTML, libgtkhtml-3.8 , [gtkhtml=1] , [gtkhtml=0])
    if test $gtkhtml = 0
      then
    	PKG_CHECK_MODULES(GTKHTML, libgtkhtml-3.6 , [gtkhtml=1] , [gtkhtml=0])
    	if test $gtkhtml = 0
    	  then
    	     PKG_CHECK_MODULES(GTKHTML, libgtkhtml-3.2 , [gtkhtml=1] , [gtkhtml=0])
    	     if test $gtkhtml = 0
    	       then 
    	          PKG_CHECK_MODULES(GTKHTML, libgtkhtml-3.1 , [gtkhtml=1] , [gtkhtml=0])
    	          if test $gtkhtml = 0
    	            then 
    	              PKG_CHECK_MODULES(GTKHTML, libgtkhtml-3.0 ,,
    	    		AC_MSG_ERROR([Could not find a working gtkhtml version]))
    	          fi
    	    fi
    	fi
    fi
    AS_SCRUB_INCLUDE(GTKHTML_CFLAGS)
dnl if Mac OSX, also scrub /sw/include
dnl GIVEN_CFLAGS=$(echo $GIVEN_CFLAGS | sed -e "s;-I/sw/include ;;" | sed -e "s;-I/sw/include$;;")
case $host_os in
	darwin*)
		GTKHTML_CFLAGS=$(echo $GTKHTML_CFLAGS | sed -e "s;-I/sw/include ;;" | sed -e "s;-I/sw/include$;;")
		GTKHTML_CFLAGS=$(echo $GTKHTML_CFLAGS | sed -e "s;-I/sw/include/gtkhtml ;;" | sed -e "s;-I/sw/includ/gtkhtmle$;;")
		;;
esac
    AC_SUBST(GTKHTML_CFLAGS)
    AC_SUBST(GTKHTML_LIBS)

   
    ### ----------------------------------------------------------------------
    
    AC_ARG_ENABLE( efence,
      [  --enable-efence               link using efence],
      if test $enableval = yes; then
         EFENCE_LIBS="-lefence"
         USE_EFENCE=1
	 AC_DEFINE(USE_EFENCE,,We are using EFence)
      fi,
      USE_EFENCE=0
      EFENCE_LIBS="")
    AC_SUBST(EFENCE_LIBS)
    
    ### ----------------------------------------------------------------------
    ### XIM
    AC_ARG_ENABLE(xim,
      [  --enable-xim                 support XIM [default=yes]],
      , enable_xim="yes")
    
    if test "x$enable_xim" = "xyes"; then
      GTK_XIM_FLAGS="-DUSE_XIM"
    fi
    
    AC_SUBST(GTK_XIM_FLAGS)
    
    
    ### ----------------------------------------------------------------------
    ## For now, we just presume you're using the GNOME version.  The other
    ## UI's haven't been carried over during the automake transition.  At
    ## some point, if it's deemed worthwhile, they can be resurrected...
    
    GNOME=1
    AC_DEFINE(GNOME,,using GNOME)
else
  # GNOME_COMPILE_WARNINGS will add -Wall; no need to set it again.
  # also, only add it for GCC.
  if test ${GCC}x = yesx
  then
     # We should always see these errors...
     CFLAGS="${CFLAGS} -Wall"
  fi
fi

###-------------------------------------------------------------------------
### Stuff from Mac OS X Port
###-------------------------------------------------------------------------

AC_CHECK_FUNCS(pthread_mutex_init)
AC_REPLACE_FUNCS(strptime localtime_r)
#AC_REPLACE_FUNCS(scm_strptime)

if test $am_cv_val_LC_MESSAGES = "no"; then
  LC_MESSAGES_ENUM="LC_ALL"
else
  LC_MESSAGES_ENUM="LC_MESSAGES"
fi
AC_SUBST(LC_MESSAGES_ENUM)

### --------------------------------------------------------------------------
### GnuCash flags and libs configuration

GNUCASH_ENGINE_BASE_LIBS="${GLIB_LIBS}"
GNUCASH_ENGINE_LIBS="-L${GNC_MODULE_DIR} -L${GNC_GWRAP_LIBDIR} ${GNUCASH_ENGINE_BASE_LIBS} ${GUILE_LIBS} -lgncmod-engine -lgw-engine -lgw-kvp -lgncmodule"

GNUCASH_ENGINE_BASE_CFLAGS="-DGNUCASH ${GLIB_CFLAGS} ${G_WRAP_COMPILE_ARGS}"
GNUCASH_ENGINE_CFLAGS="${GNUCASH_ENGINE_BASE_CFLAGS} ${GUILE_INCS}"

AC_SUBST(GNUCASH_ENGINE_BASE_LIBS)
AC_SUBST(GNUCASH_ENGINE_LIBS)
AC_SUBST(GNUCASH_ENGINE_BASE_CFLAGS)
AC_SUBST(GNUCASH_ENGINE_CFLAGS)

GNUCASH_APP_UTILS_CFLAGS="${GNUCASH_ENGINE_CFLAGS}"
GNUCASH_APP_UTILS_LIBS="${GNUCASH_ENGINE_LIBS} -lgncmod-calculation -lgncmod-app-utils"

AC_SUBST(GNUCASH_APP_UTILS_CFLAGS)
AC_SUBST(GNUCASH_APP_UTILS_LIBS)

GNUCASH_NETWORK_UTILS_CFLAGS="${GLIB_CFLAGS} ${GHTTP_CFLAGS} ${GNOME_INCLUDEDIR}"
GNUCASH_NETWORK_UTILS_LIBS="${GHTTP_LIBS} ${GNOME_LIBDIR} ${GNOME_LIBS} ${GNOMEUI_LIBS} -lgncmod-network-utils"

AC_SUBST(GNUCASH_NETWORK_UTILS_CFLAGS)
AC_SUBST(GNUCASH_NETWORK_UTILS_LIBS)

GNUCASH_GNOME_UTILS_CFLAGS="${GNUCASH_APP_UTILS_CFLAGS} ${GNUCASH_NETWORK_UTILS_CFLAGS} ${GNOME_PRINT_CFLAGS} ${GNOME_INCLUDEDIR} ${GUPPI_CFLAGS} ${GDK_PIXBUF_CFLAGS}"
GNUCASH_GNOME_UTILS_LIBS="${GNUCASH_APP_UTILS_LIBS} ${GNUCASH_NETWORK_UTILS_LIBS} ${GNOME_LIBDIR} ${GNOMEUI_LIBS} ${GNOME_PRINT_LIBS} ${GTKHTML_LIBS} ${GLADE_LIBS} ${GUPPI_LIBS} ${GDK_PIXBUF_LIBS} -lgncmod-gnome-utils"

AC_SUBST(GNUCASH_GNOME_UTILS_CFLAGS)
AC_SUBST(GNUCASH_GNOME_UTILS_LIBS)

GNUCASH_REGISTER_CORE_CFLAGS="${GNUCASH_GNOME_UTILS_CFLAGS}"
GNUCASH_REGISTER_CORE_LIBS="${GNUCASH_GNOME_UTILS_LIBS} -lgncmod-register-core"

AC_SUBST(GNUCASH_REGISTER_CORE_CFLAGS)
AC_SUBST(GNUCASH_REGISTER_CORE_LIBS)

AM_CONDITIONAL(GNUCASH_SEPARATE_BUILDDIR, test "x${srcdir}" != "x.")
AM_CONDITIONAL(GNC_FALSE, false)

###-------------------------------------------------------------------------
### Additional compiler warnings (or not) if we're running GCC
###-------------------------------------------------------------------------

# This has to come after AC_PROG_CC _AND_ GNOME_COMPILE_WARNINGS
AC_MSG_CHECKING(what extra warning flags to pass to the C compiler)
if test ${GCC}x = yesx
then
  warnFLAGS=
  # These two are because of g-wrap -- it can't avoid unused and uninitialized.
  #warnFLAGS="${warnFLAGS} -Wno-uninitialized"
  #warnFLAGS="${warnFLAGS} -Wno-unused"
  # other flags...
  # These next two are included in the GNOME_COMPILE_WARNINGS
  #warnFLAGS="${warnFLAGS} -Wmissing-prototypes"
  #warnFLAGS="${warnFLAGS} -Wmissing-declarations"
  #warnFLAGS="${warnFLAGS} -Werror-implicit-function-declaration" # In -Wall


  ## Disable error-on-warning by default again because this seriously
  ## breaks quite a number of configure tests (due to, like, 'warning:
  ## unused variable some_variable') -- cstim, 02/02/2003
  #
  # Re-enabled and moved to later in the build process.  This should allow
  # us to keep it turned on, but also not break configure proceses.
  # -- warlord, 02/02/2003


  # Enable error-on-warning by default -- I'm tired of fixing other
  # people's missing #includes, etc.
  AC_ARG_ENABLE(error-on-warning,
    [  --disable-error-on-warning    disable treating compile warnings as errors],
    [case "${enableval}" in
       yes) warnFLAGS="${warnFLAGS} -Werror" ;;
       no)  ;;
       *) AC_MSG_ERROR(bad value ${enableval} for --enable-error-on-warning) ;;
     esac],
       [  warnFLAGS="${warnFLAGS} -Werror" ])

  # For gcc >= 3.4.x, specifically enable the new warning switch
  # -Wdeclaration-after-statement in order to preserve source code
  # compatibility to gcc 2.95 and other compilers.
  GCC_VERSION=`${CC} -dumpversion`
  if test `echo ${GCC_VERSION} | cut -d. -f1` -ge 3; then
     # This is gcc >= 3.x.x
     if test `echo ${GCC_VERSION} | cut -d. -f2` -ge 4; then
	# This is gcc >= 3.4.x
	warnFLAGS="${warnFLAGS} -Wdeclaration-after-statement"
     else if test `echo ${GCC_VERSION} | cut -d. -f1` -ge 4; then
	# This is gcc == 4.x.x
	warnFLAGS="${warnFLAGS} -Wdeclaration-after-statement -Wno-pointer-sign"
	fi
     fi
  fi

  CFLAGS="${CFLAGS} ${warnFLAGS}"

else
  warnFLAGS=none
fi
AC_MSG_RESULT($warnFLAGS)

### --------------------------------------------------------------------------
### Adjustments -- especially executables that aren't generated via
### makefiles, so that we don't have an opportunity to adjust them
### there.

chmod u+x ${srcdir}/src/gnc-test-env
chmod u+x ${srcdir}/src/bin/generate-gnc-script
chmod u+x ${srcdir}/src/bin/overrides/gnucash
chmod u+x ${srcdir}/src/bin/overrides/gnucash-run-script
chmod u+x ${srcdir}/src/bin/overrides/gnucash-make-guids

### --------------------------------------------------------------------------
### Makefile creation

# This is necessary so that .o files in LIBOBJS are also built via
# the ANSI2KNR-filtering rules.
LIBOBJS_SEDSCRIPT="s,\.[[^.]]* ,$U&,g;s,\.[[^.]]*\$\$,$U&,"
AC_SUBST(LIBOBJS_SEDSCRIPT)

AC_CONFIG_FILES(po/Makefile.in
          dnl # Makefiles
          Makefile
          accounts/Makefile
          accounts/C/Makefile
          accounts/da/Makefile
          accounts/de_CH/Makefile
          accounts/de_DE/Makefile
          accounts/el_GR/Makefile
          accounts/es_ES/Makefile
          accounts/fr_FR/Makefile
          accounts/hu_HU/Makefile
          accounts/it/Makefile
          accounts/pt_BR/Makefile
          accounts/pt_PT/Makefile
          accounts/sk/Makefile
          accounts/tr_TR/Makefile
          doc/Makefile
          doc/examples/Makefile
          intl-scm/Makefile
          lib/Makefile
          lib/glib26/Makefile
          lib/guile-www/Makefile
          lib/srfi/Makefile
          lib/libc/Makefile
          lib/goffice/Makefile
          lib/goffice/split/Makefile
          lib/goffice/split/widgets/Makefile
          lib/goffice/app/Makefile
          lib/goffice/graph/Makefile
          lib/goffice/graph/plugins/Makefile
          lib/goffice/graph/plugins/plot_barcol/Makefile
          lib/goffice/graph/plugins/plot_pie/Makefile
          lib/goffice/graph/plugins/plot_radar/Makefile
          lib/goffice/graph/plugins/plot_surface/Makefile
          lib/goffice/graph/plugins/plot_xy/Makefile
          lib/goffice/utils/Makefile
          lib/goffice/gui-utils/Makefile
          lib/goffice/drawing/Makefile
          lib/goffice/pixmaps/Makefile
          lib/goffice/cut-n-paste/Makefile
          lib/goffice/cut-n-paste/pcre/Makefile
          rpm/Makefile
          src/Makefile
          src/app-utils/Makefile
          src/app-utils/test/Makefile
          src/backend/Makefile
          src/backend/net/Makefile
          src/backend/file/Makefile
          src/backend/file/test/Makefile
          src/backend/file/test/test-files/Makefile
          src/backend/file/test/test-files/xml2/Makefile
	  src/backend/qsf/Makefile
          src/backend/postgres/Makefile
          src/backend/postgres/test/Makefile
          src/backend/rpc/Makefile
          src/bin/Makefile
          src/bin/overrides/Makefile
          src/bin/test/Makefile
          src/core-utils/Makefile
          src/calculation/Makefile 
          src/calculation/test/Makefile 
          src/doc/Makefile
          src/doc/design/Makefile
          src/doc/xml/Makefile
          src/engine/Makefile
          src/engine/test/Makefile
          src/engine/test-core/Makefile
          src/gnc-module/Makefile
          src/gnc-module/test/Makefile
          src/gnc-module/test/mod-foo/Makefile
          src/gnc-module/test/mod-bar/Makefile
          src/gnc-module/test/mod-baz/Makefile
          src/gnc-module/test/misc-mods/Makefile
          src/gnome/Makefile
	  src/gnome/glade/Makefile
	  src/gnome/schemas/Makefile
	  src/gnome/ui/Makefile
          src/gnome-utils/Makefile
	  src/gnome-utils/schemas/Makefile
	  src/gnome-utils/test/Makefile
	  src/gnome-utils/ui/Makefile
          src/gnome-search/Makefile
          src/import-export/Makefile
          src/import-export/test/Makefile
          src/import-export/binary-import/Makefile
          src/import-export/binary-import/test/Makefile
          src/import-export/qif-import/Makefile
          src/import-export/qif/Makefile
          src/import-export/qif/test/Makefile
          src/import-export/qif-import/test/Makefile
          src/import-export/qif-io-core/Makefile
          src/import-export/qif-io-core/test/Makefile
          src/import-export/schemas/Makefile
          src/import-export/ofx/Makefile
          src/import-export/ofx/test/Makefile
          src/import-export/mt940/Makefile
          src/import-export/log-replay/Makefile
          src/import-export/hbci/Makefile
          src/import-export/hbci/glade/Makefile
          src/import-export/hbci/schemas/Makefile
          src/import-export/hbci/test/Makefile
          src/network-utils/Makefile
          src/network-utils/test/Makefile
          src/optional/Makefile
          src/optional/swig/Makefile
          src/optional/swig/examples/Makefile
          src/pixmaps/Makefile
          src/quotes/Makefile
          src/register/Makefile
          src/register/ledger-core/Makefile
          src/register/ledger-core/test/Makefile
          src/register/register-core/Makefile
          src/register/register-core/test/Makefile
          src/register/register-gnome/Makefile
          src/register/register-gnome/test/Makefile
          src/report/Makefile
          src/report/report-gnome/Makefile
          dnl # src/report/report-gnome/test/Makefile
          src/report/report-system/Makefile
          src/report/report-system/test/Makefile
          src/report/standard-reports/Makefile
          src/report/standard-reports/test/Makefile
          src/report/locale-specific/Makefile
          src/report/locale-specific/us/Makefile
          src/report/locale-specific/us/test/Makefile
          src/report/stylesheets/Makefile
          src/report/stylesheets/test/Makefile
          src/report/utility-reports/Makefile
          src/report/utility-reports/test/Makefile
          src/scm/Makefile
          src/scm/gnumeric/Makefile
          src/scm/printing/Makefile
          src/tax/Makefile
          src/tax/us/Makefile
          src/tax/us/test/Makefile
          src/test-core/Makefile
          src/business/Makefile
          src/business/business-core/Makefile
          src/business/business-core/test/Makefile
          src/business/business-core/file/Makefile
          src/business/business-utils/Makefile
          src/business/dialog-tax-table/Makefile
          src/business/business-gnome/Makefile
          src/business/business-gnome/glade/Makefile
          src/business/business-gnome/schemas/Makefile
          src/business/business-gnome/ui/Makefile
          src/business/business-ledger/Makefile
          src/business/business-reports/Makefile

          dnl # non-makefiles
          dnl # Please read doc/build-system before adding *anything* here

          dnl # we configure gnucash-config here because we *don't*
          dnl # want variables to be fully expanded
          gnucash-config
          ,
          dnl # commands go here, but we don't have any right now
)
AC_OUTPUT

if test x$RPC_DIR = xrpc ; then
       AC_MSG_WARN([

       The RPC Backend is deprecated.  You should not use it.
       It may go away in the future.  If you need it, please
       contact gnucash-devel@gnucash.org and let the developers
       know.
       ])
fi

AC_MSG_RESULT([
  Options detected/selected
  -------------------------
  gnucash version ...... : $VERSION
  Build for host ....... : $host
  Extra Warnings ....... : $warnFLAGS
  CPPFLAGS ............. : $CPPFLAGS
  CFLAGS ............... : $CFLAGS
  LDFLAGS .............. : $LDFLAGS
  QOF support ...........: $QOF_VERSION
  QOF location ..........: $QOF_PREFIX
  QOF library dir .......: $QOF_LIB_DIR
  QOF backend config ....: $QOF_XML_DIR
])
