## -*-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=8
LIBOFX_MICRO_VERSION=0
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)

LIBOFX_SO_CURRENT=2
LIBOFX_SO_REVISION=0
LIBOFX_SO_AGE=0
LIBOFX_SO_EFFECTIVE="`echo \$(($LIBOFX_SO_CURRENT-$LIBOFX_SO_AGE))`"
AC_SUBST(LIBOFX_SO_CURRENT)
AC_SUBST(LIBOFX_SO_REVISION)
AC_SUBST(LIBOFX_SO_AGE)

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

#if 0
##This test works for me, but breaks for most distro because the config.h installed isn't really the one that was used tu compile OpenSP
## So --with-opensp-multibyte is now the default.
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
 ])
#endif

 AC_ARG_WITH(no-opensp-multibyte, 	
[  --with-no-opensp-multibyte     Force libofx to compile with the assumption that OpenSP was NOT compiled with SP_MULTI_BYTE defined],
		,
			AC_DEFINE(SP_MULTI_BYTE, 1, [SP_MULTI_BYTE value from when OpenSP was compiled])	 )
			
##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

# Check if getopt_long is available
# ----------------------------------------------------------------------------
dnl Available from the GNU Autoconf Macro Archive at:
dnl http://www.gnu.org/software/ac-archive/htmldoc/adl_func_getopt_long.html
dnl
AC_DEFUN([adl_FUNC_GETOPT_LONG],
 [AC_PREREQ(2.49)dnl
  # clean out junk possibly left behind by a previous configuration
  rm -f lib/getopt.h
  # Check for getopt_long support
  AC_CHECK_HEADERS([getopt.h])
  AC_CHECK_FUNCS([getopt_long],,
   [# FreeBSD has a gnugetopt library for this
    AC_CHECK_LIB([gnugetopt],[getopt_long],[AC_DEFINE([HAVE_GETOPT_LONG])],
     [# use the GNU replacement
      AC_LIBOBJ(getopt)
      AC_LIBOBJ(getopt1)
      AC_CONFIG_LINKS([lib/getopt.h:lib/gnugetopt.h])])])])

dnl check for getopt in standard library
adl_FUNC_GETOPT_LONG
AM_CONDITIONAL(NO_GETOPTLONG, test "$ac_cv_func_getopt_long" = no  )

# gengetopt command line parser generation
AC_ARG_ENABLE(gengetopt,
        AS_HELP_STRING(--disable-gengetopt,Disable rebuilding of command line parser with gengetopt),
[case "${enableval}" in
  yes) gengetopt=yes ;;
  no)  gengetopt=no ;;
  *) AC_MSG_ERROR([bad value ${enableval} for --disable-gengetopt]) ;;
esac],[gengetopt=yes])

if test x$gengetopt = xyes ; then
   AC_CHECK_PROG(have_gengetopt, gengetopt, yes, no)

   if test x$have_gengetopt = xno ; then
     AC_MSG_WARN([*** Not rebuilding command line parser as gengetopt is not found ***])
     gengetopt=no
   fi
fi

AM_CONDITIONAL(USE_GENGETOPT, test "x$gengetopt" = xyes)


dnl MY_CURL
dnl -------
dnl set my_cv_curl_vers to the version of libcurl or NONE
dnl if libcurl is not found or is too old
 
AC_DEFUN(MY_CURL,[
 AC_CACHE_VAL(my_cv_curl_vers,[
 my_cv_curl_vers=NONE
 dnl check is the plain-text version of the required version
 check="7.9.7"
 dnl check_hex must be UPPERCASE if any hex letters are present
 check_hex="070907"
 
 AC_MSG_CHECKING([for curl >= $check])
 
 if eval curl-config --version 2>/dev/null >/dev/null; then
   ver=`curl-config --version | sed -e "s/libcurl //g"`
#   hex_ver=`curl-config --vernum | tr 'a-f' 'A-F'`
#   ok=`echo "ibase=16; if($hex_ver>=$check_hex) $hex_ver else 0" | bc`
 
   if test x$ok != x0; then
     my_cv_curl_vers="$ver"
     AC_MSG_RESULT([$my_cv_curl_vers])
     CURL_INCS=`curl-config --cflags`
     CURL_LIBS=`curl-config --libs`
     AC_SUBST(CURL_INCS)
     AC_SUBST(CURL_LIBS)
   else
     AC_MSG_RESULT(FAILED)
     AC_MSG_WARN([$ver is too old. Need version $check or higher.])
   fi
 else
   AC_MSG_RESULT(FAILED)
   AC_MSG_ERROR([curl-config was not found])
 fi
 ])
])

# check for curl
# ----------------------------------------------------------------------------
MY_CURL

if test x$my_cv_curl_vers != xNONE; then
AC_DEFINE(HAVE_CURL,, [LIBCURL is available])
fi

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
			ofxconnect/Makefile
			)

