dnl Process this file with autoconf to produce a configure script.
AC_INIT(README)

dnl Settings
PACKAGE=dolfin
VERSION=0.3.4
DOLFIN_MAIN="main"

dnl Note: need to be in the correct order, and some are needed twice.
dnl Not very pretty... Maybe someone knows how to fix this?
DOLFIN_KERNEL="io grid fem elements quadrature io la math common settings log settings common"

dnl Find modules
cd src/modules
./scanmodules.sh
cd ../..
DOLFIN_MODULES=""
for l in `cat src/modules/modules.list | grep -v "#"`; do
	DOLFIN_MODULES="$DOLFIN_MODULES$l "
done

dnl Initialize automake
AM_INIT_AUTOMAKE($PACKAGE, $VERSION, no-define)

dnl Tell automake not to generate Makefile.in unless asked to.
dnl This way the code can be installed on systems without automake installed.
AM_MAINTAINER_MODE

dnl Checks for programs
AC_PROG_CC
AC_PROG_CXX
AC_PROG_RANLIB
AC_PROG_AWK
AC_PROG_INSTALL
AC_PROG_LN_S

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

dnl Check for libxml2
AC_CHECK_PROG(libxml2_found, xml2-config, yes, no)
if test $libxml2_found = yes; then
	XML2_INCLUDES=`xml2-config --cflags`
	XML2_LFLAGS=`xml2-config --libs`
 	AC_SUBST(XML2_INCLUDES)
	AC_SUBST(XML2_LFLAGS)
else
 	echo "*** Unable to find libxml2 development files on your system."
        echo "*** Perhaps you need to install the package libxml2-dev?"
	exit 1
fi

dnl Check for curses
AC_SEARCH_LIBS(wbkgdset, ncurses,,echo "*** Unable to find ncurses on your system."; exit 1)

dnl Generate include path for modules (including kernel code)
MODULE_INCLUDES=""
DIRS="$DOLFIN_MAIN $DOLFIN_KERNEL"
for f in $DIRS; do
	MODULE_INCLUDES="$MODULE_INCLUDES-I\$(top_builddir)/src/kernel/$f "
done
AC_SUBST(MODULE_INCLUDES)

dnl Generate include path for main (including also module code)
MAIN_INCLUDES=""
DIRS="$DOLFIN_MODULES"
for f in $DIRS; do
	MAIN_INCLUDES="$MAIN_INCLUDES-I\$(top_builddir)/src/modules/$f "
done
MAIN_INCLUDES="$MODULE_INCLUDES $MAIN_INCLUDES"
AC_SUBST(MAIN_INCLUDES)

dnl Generate library list for linking in the correct order.
DOLFIN_LFLAGS=""
DIRS="$DOLFIN_MAIN $DOLFIN_MODULES $DOLFIN_KERNEL"
for f in $DIRS; do
	DOLFIN_LFLAGS="$DOLFIN_LFLAGS-ldolfin-$f "
done
AC_SUBST(DOLFIN_LFLAGS)

dnl Create Makefiles
dnl Seems like automake cannot handle a variable containing the
dnl list of files, so this cannot be done automatically. We have
dnl to list all the files.
AC_OUTPUT( Makefile \
           src/Makefile \
           src/pre/Makefile \
           src/kernel/Makefile \
           src/kernel/common/Makefile \
           src/kernel/common/dolfin/Makefile \
           src/kernel/elements/Makefile \
           src/kernel/elements/dolfin/Makefile \
           src/kernel/fem/Makefile \
           src/kernel/fem/dolfin/Makefile \
           src/kernel/grid/Makefile \
           src/kernel/grid/dolfin/Makefile \
           src/kernel/io/Makefile \
           src/kernel/io/dolfin/Makefile \
           src/kernel/la/Makefile \
           src/kernel/la/dolfin/Makefile \
           src/kernel/main/Makefile \
           src/kernel/main/dolfin/Makefile \
           src/kernel/math/Makefile \
           src/kernel/math/dolfin/Makefile \
           src/kernel/quadrature/Makefile \
           src/kernel/quadrature/dolfin/Makefile \
           src/kernel/log/Makefile \
           src/kernel/log/dolfin/Makefile \
           src/kernel/settings/Makefile \
           src/kernel/settings/dolfin/Makefile \
           src/modules/Makefile \
           src/modules/poisson/Makefile \
           src/modules/convdiff/Makefile \
           src/modules/navierstokes/Makefile \
           src/modules/template/Makefile \
           src/config/Makefile \
           src/post/Makefile \
           src/demo/Makefile \
           src/demo/solvers/Makefile \
           src/utils/Makefile \
           src/utils/inp2dx/Makefile )
