## -*-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

AC_INIT(inc/libofx.h)
AM_CONFIG_HEADER(config.h)
AC_CONFIG_AUX_DIR(config)
AC_PROG_CC
AC_PROG_CXX
#AC_PROG_RANLIB

LIBOFX_MAJOR_VERSION=0
LIBOFX_MINOR_VERSION=6
LIBOFX_MICRO_VERSION=2
LIBOFX_VERSION=$LIBOFX_MAJOR_VERSION.$LIBOFX_MINOR_VERSION.$LIBOFX_MICRO_VERSION

AC_SUBST(LIBOFX_MAJOR_VERSION)
AC_SUBST(LIBOFX_MINOR_VERSION)
AC_SUBST(LIBOFX_MICRO_VERSION)
AC_SUBST(LIBOFX_VERSION)
AM_INIT_AUTOMAKE(libofx,$LIBOFX_VERSION)




AM_MAINTAINER_MODE

AC_PROG_INSTALL

AC_LIBTOOL_DLOPEN
AM_PROG_LIBTOOL

AC_ISC_POSIX
AC_C_BIGENDIAN
AC_PROG_MAKE_SET
AC_HEADER_STDC

AC_ARG_WITH(opensp-includes, 
[  --with-opensp-includes=PATH     specify where to look for OpenSP includes  
                                  - default is /usr/include/OpenSP)],
			OPENSPINCLUDES="$with_opensp_includes",
			OPENSPINCLUDES="" )

AC_ARG_WITH(opensp-libs, 
[  --with-opensp-libs=PATH         specify where to look for libosp 
                                  - default is /usr/lib],
			OPENSPLIBPATH="$with_opensp_libs",
			OPENSPLIBPATH="/usr/lib")
			
			echo $OPENSPLIBPATH
for d in /usr/include/OpenSP /usr/local/include/OpenSP /usr/include/sp/generic /usr/local/include/sp/generic; do
    if test "x$OPENSPINCLUDES" = x; then
	save_CPPFLAGS="$CPPFLAGS"
	CPPFLAGS="$CPPFLAGS -I$d"
	AC_MSG_CHECKING(for ParserEventGenerator.h in $d)
	AC_TRY_CPP([#include <ParserEventGeneratorKit.h>],
	[ AC_MSG_RESULT(yes); OPENSPINCLUDES=$d ], [ AC_MSG_RESULT(no) ])
	CPPFLAGS="$save_CPPFLAGS"
    fi
done


##Detect if OpenSP was compiled with SP_MULTI_BYTE, and put the result in config.h
##
AC_DEFUN([CHECK_SP_MULTI_BYTE],
 [
AC_CACHE_VAL(ox_sp_multibyte,
   [
 for d in $OPENSPINCLUDES/config.h $OPENSPINCLUDES/../config.h $OPENSPINCLUDES/../include/config.h ; do
    if test "x$OPENSPCONFIG_H" = x; then
	save_CPPFLAGS="$CPPFLAGS"
	CPPFLAGS="$CPPFLAGS -DOPENSPCONFIG_H=\"$d\""
	AC_MSG_CHECKING(for OpenSP's config.h in $d)
	AC_TRY_CPP([#include OPENSPCONFIG_H],
	[ AC_MSG_RESULT(yes); OPENSPCONFIG_H=$d ], [ AC_MSG_RESULT(no) ])
	CPPFLAGS="$save_CPPFLAGS"
    fi
 done
 if test "x$OPENSPCONFIG_H" = x; then
	AC_MSG_ERROR([OpenSP's config.h not found])
 fi
AC_MSG_CHECKING(for if OpenSP's was compiled with SP_MULTI_BYTE)
    ox_sp_multibyte=`sed 's/^#.*SP_MULTI_BYTE[ \t]*\([01]\)/\1/;t;d' < \
            $OPENSPCONFIG_H`])
     if test x"$ox_sp_multibyte" != x ;then
         AC_DEFINE_UNQUOTED(SP_MULTI_BYTE, $ox_sp_multibyte, [SP_MULTI_BYTE value from when OpenSP was compiled])
		 AC_MSG_RESULT(yes)
     else
AC_MSG_RESULT(no)
    fi
 ])

 AC_ARG_WITH(opensp-multibyte, 
[  --with-opensp-multibyte     Force libofx to compile with the assumption that OpenSP was compiled with SP_MULTI_BYTE defined],
			AC_DEFINE(SP_MULTI_BYTE, 1, [SP_MULTI_BYTE value from when OpenSP was compiled]),
			CHECK_SP_MULTI_BYTE )
			
##if test x"$SP_MULTI_BYTE" == x ;then
##CHECK_SP_MULTI_BYTE
##fi

ac_save_CPPFLAGS="$CPPFLAGS"
if test "x$OPENSPINCLUDES" != x ; then
  CPPFLAGS="-I$OPENSPINCLUDES $CPPFLAGS"
fi
AC_LANG_CPLUSPLUS 
AC_CHECK_HEADERS([ParserEventGeneratorKit.h SGMLApplication.h EventGenerator.h], 
				[] , 
				[ AC_MSG_ERROR([OpenSP includes not found]) ], 
				[] )
				
OPENSPLIBS="-L$OPENSPLIBPATH -losp"
ac_save_LIBS="$LIBS"
LIBS="$OPENSPLIBS $LIBS"

AC_MSG_CHECKING([for libosp])
##dnl This is code from the opensp documentation, I modified it a little
##dnl It is really just a link test rather than a run test, it does nothing
AC_LANG_CPLUSPLUS 
	AC_TRY_RUN([
		#include "ParserEventGeneratorKit.h"
		using namespace std;
		class OutlineApplication : public SGMLApplication {
		public:
		  OutlineApplication() { }
		  void startElement(const StartElementEvent &event) {
		  }
		  void endElement(const EndElementEvent &) { }

		};
		int main(int argc, char **argv)
		{
		  ParserEventGeneratorKit parserKit;
		  EventGenerator *egp = parserKit.makeEventGenerator(argc - 1, argv + 1);
		  OutlineApplication app;
		}
	],
	[AC_MSG_RESULT([yes])],
	[AC_MSG_RESULT([no])
	AC_MSG_ERROR([unable to link a test program, is OpenSP installed?])],
	[AC_MSG_RESULT([unknown, assumed OK])])
				
CPPFLAGS="$ac_save_CPPFLAGS"
LIBS="$ac_save_LIBS"		

 
# 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

AC_SUBST(OPENSPINCLUDES)
AC_SUBST(OPENSPLIBS)
LIBOFX_DTD_DIR='${datadir}/libofx/dtd'
AC_SUBST(LIBOFX_DTD_DIR)

			
AC_OUTPUT( 		Makefile 
	   		libofx.spec
			lib/Makefile
			inc/Makefile
			dtd/Makefile
			doc/Makefile
			ofx2qif/Makefile
			ofxdump/Makefile
			)

