dnl Process this file with autoconf to produce a configure script.
AC_INIT(fib.c)
AC_CONFIG_HEADER(config.h)

dnl I did this wrong, I think. I should have used AC_DEFINE(VERSION,"$VERSION") or similar
dnl but what's done is done... (fjf)
VERSION=1.0.3
AC_SUBST(VERSION)

# Tests for Windows
AC_CYGWIN
AC_EXEEXT
AC_OBJEXT
AC_MINGW32
# End Tests for Windows

dnl Checks for programs.

dnl I'm changing back to the default since I think avoiding the -g is rather too elaborate
dnl but tell me if I'm wrong to do this. The definition has been moved to acinclude.m4, so
dnl just uncomment AC_PROG_CC_NO_G and comment AC_PROG_CC to return to the original: (fjf)
dnl AC_PROG_CC_NO_G
AC_PROG_CC

xuname="x`uname`"

if test "x$GCC" = "xyes" -a "x$MINGW32" != "xyes" -a "$xuname" != "xQNX"; then
  ANSI_PEDANTIC="-ansi -pedantic"
else
  ANSI_PEDANTIC=""
fi
AC_SUBST(ANSI_PEDANTIC)

if test "$xuname" = "xDarwin"; then
  CFLAGS="$CFLAGS -Wno-long-long -no-cpp-precomp"
  LDFLAGS="$LDFLAGS -Wl,-multiply_defined -Wl,suppress"
fi

AC_PROG_MAKE_SET
AC_PROG_INSTALL
AC_PROG_RANLIB

dnl Checks for headers.

AC_HEADER_STDC
dnl AC_HEADER_SYS_WAIT
dnl AC_TYPE_SIGNAL
dnl AC_FUNC_WAIT3

AC_CHECK_HEADERS(fcntl.h io.h malloc.h sys/file.h sys/ioctl.h unistd.h)

dnl Checks for typedefs, structures, and compiler characteristics.

AC_C_CONST

dnl exporter support
dnl =======================================================

AC_ARG_WITH(exporter,[  --with-exporter         build the exporter (default is no)],[
	if test "x$withval" = "xno"; then
		EXPORTER=""
	else
		dnl Need to watch out for case-insensitive file systems (e.g. HFS+)
		EXPORTER="BEXPORTER"
	fi
],[	EXPORTER=""
])

AC_SUBST(EXPORTER)

dnl zlib support
dnl =======================================================

_cppflags=$CPPFLAGS
_ldflags=$LDFLAGS

AC_ARG_WITH(zlib,[  --with-zlib=DIR         use libz in DIR],[
	if test "x$withval" = "xyes"; then
		ZLIB_DIR=""
	elif test "x$withval" != "xno"; then
		ZLIB_DIR="$withval"
		CPPFLAGS="$CPPFLAGS -I$withval/include"
		LDFLAGS="$LDFLAGS -L$withval/lib"
	else
		AC_MSG_WARN([No zlib? Are you sure? I think I'll look for it anyway...])
		ZLIB_DIR=""
	fi
],[	ZLIB_DIR=""
])

AC_CHECK_HEADER(zlib.h,[
	AC_CHECK_LIB(z,gzread,[
		AC_DEFINE(HAVE_ZLIB)
		HAVE_ZLIB=1
		if test "x$ZLIB_DIR" != "x"; then
			ZLIB_CFLAGS="-I$ZLIB_DIR/include"
			ZLIB_LIBS="-L$ZLIB_DIR/lib -lz"
		else
			ZLIB_CFLAGS=""
			ZLIB_LIBS="-lz"
		fi
	],[	AC_CHECK_LIB(gz,gzread,[
			AC_DEFINE(HAVE_ZLIB)
			HAVE_ZLIB=1
			if test "x$ZLIB_DIR" != "x"; then
				ZLIB_CFLAGS="-I$ZLIB_DIR/include"
				ZLIB_LIBS="-L$ZLIB_DIR/lib -lgz"
			else
				ZLIB_CFLAGS=""
				ZLIB_LIBS="-lgz"
			fi
		],[	dnl Shouldn't this be an error?
			AC_MSG_WARN([* * * Unable to find zlib, which is kinda necessary. * * *])
			HAVE_ZLIB=0
			ZLIB_CFLAGS=""
			ZLIB_LIBS=""
		])
	])
])

AC_SUBST(ZLIB_CFLAGS)
AC_SUBST(ZLIB_LIBS)

CPPFLAGS=$_cppflags
LDFLAGS=$_ldflags

dnl system png support
dnl ========================================================

_cppflags=$CPPFLAGS
_ldflags=$LDFLAGS

dnl libpng requires zlib
CPPFLAGS="$CPPFLAGS $ZLIB_CFLAGS"

AC_ARG_WITH(png,[  --with-png=DIR          use png library in DIR],[
	if test "x$withval" = "xyes"; then
		PNG_DIR=""
	elif test "x$withval" != "xno"; then
		PNG_DIR=$withval
		CPPFLAGS="$CPPFLAGS -I$withval/include"
		LDFLAGS="$LDFLAGS -L$withval/lib"
	else
		AC_MSG_WARN([No libpng? Are you sure? I think I'll look for it anyway...])
		PNG_DIR=""
	fi
],[	PNG_DIR=""
])

AC_CHECK_HEADER(png.h,[
	AC_CHECK_HEADER(pngconf.h,[
		AC_CHECK_LIB(png, png_free,[
			AC_DEFINE(HasPNG)
			HAVE_PNG=1
			if test "x$PNG_DIR" != "x"; then
				PNG_CFLAGS="-I$PNG_DIR/include"
				PNG_LIBS="-L$PNG_DIR/lib -lpng"
			else
				PNG_CFLAGS=""
				PNG_LIBS="-lpng"
			fi
		],[	dnl Shouldn't this be an error?
			AC_MSG_WARN([* * * Unable to find libpng, which is kinda necessary. * * *])
			HAVE_PNG=0
			PNG_CFLAGS=""
			PNG_LIBS=""
		],$ZLIB_LIBS -lm)
	],[	dnl Shouldn't this be an error?
		AC_MSG_WARN([* * * Missing "pngconf.h" header file * * *])
		HAVE_PNG=0
		PNG_CFLAGS=""
		PNG_LIBS=""
	])
],[	dnl Shouldn't this be an error?
	AC_MSG_WARN([* * * Missing "png.h" header file * * *])
	HAVE_PNG=0
	PNG_CFLAGS=""
	PNG_LIBS=""
])

AC_SUBST(PNG_CFLAGS)
AC_SUBST(PNG_LIBS)

CPPFLAGS=$_cppflags
LDFLAGS=$_ldflags

dnl system XML support
dnl ========================================================

_cppflags=$CPPFLAGS
_ldflags=$LDFLAGS

dnl There are various options:
dnl (1) wv's 'included' expat (default)
dnl (2) system 'expat' library
dnl (3) system 'libxml2' library
dnl (4) 'peer' expat (i.e., AbiWord build) ?

wv_xml="included"

AC_ARG_WITH(expat,[  --with-expat[=DIR]      use system expat (default is no)],[
	if test "x$withval" = "xyes"; then
		wv_xml="expat"
		XML_DIR=""
	elif test "x$withval" = "xpeer"; then
		wv_xml="peer"
		XML_DIR=""
	elif test "x$withval" != "xno"; then
		wv_xml="expat"
		XML_DIR=$withval
		CPPFLAGS="$CPPFLAGS -I$withval/include"
		LDFLAGS="$LDFLAGS -L$withval/lib"
	fi
])

AC_ARG_WITH(libxml2,[  --with-libxml2[=DIR]    use system libxml2 (default is no)],[
	if test "x$wv_xml" = "xexpat"; then
		AC_MSG_WARN([* * * Cannot specify expat AND libxml2. I'm going to use expat * * *])
	else
		if test "x$withval" = "xyes"; then
			wv_xml="libxml2"
			XML_DIR=""
		elif test "x$withval" != "xno"; then
			wv_xml="libxml2"
			XML_DIR=$withval
		fi
	fi
])

if test "x$wv_xml" = "xincluded"; then
	INCLUDED_EXPAT=1
	HAVE_LIBXML2=0
	XML_CFLAGS=""
	XML_LIBS=""
elif test "x$wv_xml" = "xexpat"; then
	AC_DEFINE(HAVE_EXPAT)
	INCLUDED_EXPAT=0
	HAVE_LIBXML2=0
	AC_CHECK_HEADER(expat.h,[
		AC_CHECK_LIB(expat,XML_ParserCreate,[
			if test "x$XML_DIR" != "x"; then
				XML_CFLAGS="-I$XML_DIR/include"
				XML_LIBS="-L$XML_DIR/lib -lexpat"
			else
				XML_CFLAGS=""
				XML_LIBS="-lexpat"
			fi
		],[	AC_MSG_ERROR([* * * Unable to find expat library * * *])
		])
	],[	AC_MSG_ERROR([* * * Unable to find header file "expat.h" * * *])
	])
elif test "x$wv_xml" = "xlibxml2"; then
	AC_DEFINE(HAVE_LIBXML2)
	INCLUDED_EXPAT=0
	HAVE_LIBXML2=1

	if [ test -n "$XML_DIR" ]; then
		AC_PATH_PROG(XML2_CONFIG,xml2-config, ,[$XML_DIR/bin:$PATH])
	else
		AC_PATH_PROG(XML2_CONFIG,xml2-config)
	fi

	if [ test -n "$XML2_CONFIG" ]; then
		xml2_cflags=`$XML2_CONFIG --cflags`
		xml2_libs=`$XML2_CONFIG --libs`
	else
		AC_MSG_ERROR([* * * unable to find xml2-config; unable to continue * * *])
	fi

	CPPFLAGS="$CPPFLAGS $xml2_cflags"
	LDFLAGS="$LDFLAGS $xml2_libs"

	AC_CHECK_HEADER(libxml/parser.h,[
		AC_CHECK_LIB(xml2,xmlParseDocument,[
			XML_CFLAGS="$xml2_cflags"
			XML_LIBS="$xml2_libs"
		],[	AC_MSG_ERROR([* * * Unable to find libxml2 library * * *])
		],$xml2_libs)
	],[	AC_MSG_ERROR([* * * Unable to find header file "libxml/parser.h" * * *])
	])
elif test "x$wv_xml" = "xpeer"; then
	AC_DEFINE(HAVE_EXPAT)
	INCLUDED_EXPAT=0
	HAVE_LIBXML2=0
	XML_CFLAGS="-I\$(top_builddir)/../expat/lib"
	XML_LIBS=""
else
	AC_MSG_ERROR([* * * Glitch! Unexpected value. * * *])
fi

AC_SUBST(INCLUDED_EXPAT)
AC_SUBST(HAVE_LIBXML2)

AC_SUBST(XML_CFLAGS)
AC_SUBST(XML_LIBS)

CPPFLAGS=$_cppflags
LDFLAGS=$_ldflags

dnl system glib support
dnl ========================================================

AC_ARG_WITH(glib,[  --with-glib[=DIR]       use system glib [in DIR]],[
	if [ test "x$withval" = "xno" ]; then
		wv_sys_glib=no
	elif [ test "x$withval" = "xyes" ]; then
		wv_sys_glib=required
		GLIB_DIR=""
	elif [ test "x$withval" = "xglib2" ]; then
		wv_sys_glib=glib2
		GLIB_DIR=""
	else
		wv_sys_glib=required
		GLIB_DIR="$withval"
	fi
],[	wv_sys_glib=check
	GLIB_DIR=""
])

if test $wv_sys_glib = glib2; then
	PKG_CHECK_MODULES(GLIB,glib-2.0 >= 2.0,[
		dnl do we need to set these?
		wv_glib2=yes
	],[	wv_glib2=no
	])
	if test $wv_glib2 = no; then
		AC_MSG_ERROR([$GLIB_PKG_ERRORS])
	fi

elif test $wv_sys_glib != no; then
	wv_glib2=no
	if test "x$GLIB_DIR" = "x"; then
		AC_PATH_PROG(PKG_CONFIG, pkg-config, no)
		if test "x$PKG_CONFIG" != "xno" ; then
			PKG_CHECK_MODULES(GLIB,glib-2.0 >= 2.0,[
				wv_sys_glib=glib2
				wv_glib2=yes
			],[	AC_MSG_WARN([*** falling back to glib-1.2])
			])
		fi
	fi
	if test $wv_glib2 = no; then
		if test "x$GLIB_DIR" = "x"; then
			AC_PATH_PROG(GLIB_CONFIG, glib12-config, , [$PATH])
		else
			AC_PATH_PROG(GLIB_CONFIG, glib12-config, , [$GLIB_DIR/bin:$PATH])
		fi
		if test "x$GLIB_CONFIG" = "x"; then
			if test "x$GLIB_DIR" = "x"; then
				AC_PATH_PROG(GLIB_CONFIG, glib-config, , [$PATH])
			else
				AC_PATH_PROG(GLIB_CONFIG, glib-config, , [$GLIB_DIR/bin:$PATH])
			fi
		fi
		if test "x$GLIB_CONFIG" = "x"; then
			if test $wv_sys_glib = required; then
				AC_MSG_ERROR([* * * Unable to find glib12-config or glib-config in path! * * *])
			fi
			wv_sys_glib=no
		fi
	fi
fi

if test $wv_sys_glib = no; then
	HAVE_GLIB=0
	GLIB_CFLAGS=""
	GLIB_LIBS=""
elif test $wv_sys_glib != glib2; then
	AC_DEFINE(HAVE_GLIB)
	HAVE_GLIB=1
	GLIB_CFLAGS=`$GLIB_CONFIG --cflags`
	GLIB_LIBS=`$GLIB_CONFIG --libs`
else
	AC_DEFINE(HAVE_GLIB)
	HAVE_GLIB=1
fi

AC_SUBST(HAVE_GLIB)

AC_SUBST(GLIB_CFLAGS)
AC_SUBST(GLIB_LIBS)

dnl system libwmf support
dnl ========================================================

dnl Check for libwmf-0.2.1 or higher

_cppflags=$CPPFLAGS
_ldflags=$LDFLAGS

have_libwmf=no

AC_ARG_WITH(libwmf,[  --with-libwmf=DIR       use libwmf(2) in DIR],[
	if [ test "x$withval" != "xno" ]; then
		if [ test "x$withval" != "xyes" ]; then
			LIBWMF_DIR=$withval
		fi
		search_for_libwmf=yes
	else
		search_for_libwmf=no
	fi
],[	search_for_libwmf=no
])

if [ test $search_for_libwmf != no ]; then
	if [ test -n "$LIBWMF_DIR" ]; then
		AC_PATH_PROG(LIBWMF_CONFIG,libwmf-config, ,[$LIBWMF_DIR/bin:$PATH])
	else
		AC_PATH_PROG(LIBWMF_CONFIG,libwmf-config)
	fi

	if [ test -n "$LIBWMF_CONFIG" ]; then
		libwmf_cflags=`$LIBWMF_CONFIG --cflags`
		libwmf_libs=`$LIBWMF_CONFIG --libs`
	else
		AC_MSG_ERROR(* * * unable to find libwmf-config; unable to continue * * *)
	fi

	CPPFLAGS="$CPPFLAGS $libwmf_cflags"
	LDFLAGS="$LDFLAGS $libwmf_libs"

	AC_CHECK_HEADER(libwmf/api.h,[
		AC_CHECK_LIB(wmf,wmf_stream_create,have_libwmf=yes,[
			AC_MSG_ERROR(* * * libwmf >= 0.2.1 required * * *)
		],$libwmf_libs)
		AC_CHECK_HEADER(libwmf/foreign.h,[
			AC_DEFINE(HAVE_LIBWMF_FOREIGN_H)
		],[	AC_MSG_WARN(* * * libwmf >= 0.2.2 recommended * * *)
		])
	])

	if [ test $have_libwmf != no]; then
		AC_DEFINE(HAVE_LIBWMF)

		WMF_CFLAGS="$libwmf_cflags"
		WMF_LIBS="$libwmf_libs"
	fi
fi

CPPFLAGS=$_cppflags
LDFLAGS=$_ldflags

AC_SUBST(WMF_LIBS)
AC_SUBST(WMF_CFLAGS)

dnl test for iconv
dnl ========================================================

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

have_iconv=no
use_peer_libiconv=no
check_for_libiconv=yes
default_to_libiconv=yes

AC_ARG_WITH(libiconv,[  --with-libiconv[=DIR]   use libiconv [in DIR]],[
	if [ test "$withval" = "no" ]; then
		check_for_libiconv=no
		default_to_libiconv=no
	elif [ test "$withval" != "yes" ]; then
		check_for_libiconv=yes
		LIBICONV_DIR=$withval
	fi
],default_to_libiconv=no)

dnl use --with-peer-iconv to get libiconv from the peer directory
dnl 
AC_ARG_WITH(peer-iconv,[  --with-peer-iconv       use libiconv from peer directory ],[
	if test "$withval" != "no"; then
		use_peer_libiconv=$withval
		if [ test -d $srcdir/../libiconv ]; then
			check_for_libiconv=yes
			default_to_libiconv=yes
			LIBICONV_DIR="$srcdir/../libiconv"
		fi
	fi
])

dnl first search for system iconv
dnl 
if [ test "$default_to_libiconv" = "no" ]; then
	AC_CHECK_FUNC(iconv,[
		AC_CHECK_HEADER(iconv.h,[
			have_iconv=sys
			check_for_libiconv=no
		],[	AC_MSG_ERROR(* * * I found system iconv but can't find iconv.h header! * * *)
		])
	])
fi

dnl then search for libiconv
dnl 
if [ test "$check_for_libiconv" = "yes" ]; then
	if [ test "x$LIBICONV_DIR" != "x" ]; then
		if [ test "$use_peer_libiconv" != "no" ]; then
			ICONV_CFLAGS="-I\$(top_builddir)/../libiconv/include"
			ICONV_LIBS="-L\$(top_builddir)/../libiconv/lib/.libs -liconv"
			CPPFLAGS="$CPPFLAGS -I../libiconv/include"
			LDFLAGS="$LDFLAGS -L../libiconv/lib/.libs -liconv"
		else
			ICONV_CFLAGS="-I$LIBICONV_DIR/include"
			ICONV_LIBS="-L$LIBICONV_DIR/lib -liconv"
			CPPFLAGS="$CPPFLAGS $ICONV_CFLAGS"
			LDFLAGS="$LDFLAGS $ICONV_LIBS"
		fi
	else
		ICONV_CFLAGS=""
		ICONV_LIBS="-liconv"
		LDFLAGS="$LDFLAGS $ICONV_LIBS"
	fi

	if [ test "$use_peer_libiconv" = "abi" ]; then
		dnl if configure is called with --with-peer-iconv=abi
		dnl then assume that iconv exists and will be linked eventually
		dnl 
		AC_CHECK_HEADER(iconv.h,have_iconv=abi)
	else
		AC_CHECK_HEADER(iconv.h,[
			AC_CHECK_LIB(iconv,iconv_open,[
				have_iconv=iconv
			],[	AC_CHECK_LIB(iconv,libiconv_open,[
					have_iconv=libiconv
				],[	AC_MSG_ERROR(* * * I found iconv.h header but no working library! * * *)
				])
			])
		])
	fi	
fi

if [ test "$have_iconv" = "no" ]; then
	AC_MSG_ERROR(* * * iconv is required: ftp://ftp.gnu.org/pub/gnu/libiconv * * *)
fi

dnl Apparently this trick fails on RedHat 6.2 (and presumably earlier RedHats & other
dnl concurrent Linux distributions) because g++ issues a warning rather than an error.
dnl Perhaps we should have something like (I'm not sure about the flag):        (fjf)
dnl if test "x$GCC" = "xyes"; then CFLAGS="$CFLAGS --fstrict-ansi"; fi

AC_LANG_CPLUSPLUS

AC_MSG_CHECKING(need for const in iconv)
AC_TRY_COMPILE([
#include <iconv.h>
],[
	iconv_t handle;
	size_t ib;
	size_t ob;
	const char* in = 0;
	char* out = 0;
	iconv (handle,const_cast<char **>(&in),&ib,&out,&ob);
],[	AC_MSG_RESULT(no)
],[	AC_MSG_RESULT(yes)
	AC_DEFINE(ICONV_REQUIRES_CONST)
])

AC_LANG_C

AC_SUBST(ICONV_CFLAGS)
AC_SUBST(ICONV_LIBS)

CPPFLAGS=$_cppflags
LDFLAGS=$_ldflags

dnl end of iconv
dnl ========================================================

dnl ========================================================
dnl begin of possible optimization tests
dnl read notes/optimization

AC_CHECK_SIZEOF(char,1)
AC_CHECK_SIZEOF(short,2)
AC_CHECK_SIZEOF(int,4)
AC_CHECK_SIZEOF(long,4)

if [test "$SIZEOF_CHAR" != "1" ]; then
    if [test "$SIZEOF_SHORT" != "2" ]; then
        if [test "$SIZEOF_INT" != "4" ]; then
            AC_DEFINE(MATCHED_TYPE)
        fi
    fi
fi

if [ test "$cross_compiling" != "yes" ]; then
    AC_C_BIGENDIAN
dnl	here we will know the endianness
	if [ test "$ac_cv_c_bigendian" = "no" ]; then
	    AC_DEFINE(XML_BYTE_ORDER,12)
	else 
		if [ test "$ac_cv_c_bigendian" = "yes" ]; then
			AC_DEFINE(XML_BYTE_ORDER,21)
		fi
	fi
else
dnl	here we will not know the endianness, so we will
dnl go with the mechanism that works everywhere
    AC_DEFINE(WORDS_BIGENDIAN)
fi

dnl given matched types and little endian we can read types larger
dnl than a byte with fread and avoid byteswapping

dnl at some later stage we will have a test to see if the compiler can
dnl get rid of holes, for the moment we will assume only that we can
dnl do it if we have __GNUC__ defined

dnl end of possible optimization tests


dnl check for getopt
AC_CHECK_FUNC(getopt_long, , [LIBOBJS="$LIBOBJS getopt1.o getopt.o"])
dnl check for strerror
AC_CHECK_FUNC(strerror, , [LIBOBJS="$LIBOBJS strerror.o"])
dnl check for strcasecmp
AC_CHECK_FUNC(strcasecmp, , [LIBOBJS="$LIBOBJS strcasecmp.o"])
AC_EGREP_HEADER(strcasecmp,string.h,,AC_DEFINE(DEFINE_STRCASECMP))
dnl check for memcpy, if not there we'll use bcopy
AC_CHECK_FUNCS(memcpy)
dnl check for mmap
AC_FUNC_MMAP

AC_SUBST(LIBOBJS)

AC_OUTPUT(
Makefile 
version.c
wvAbw
wvDVI
wvPS
wvPDF
wvHtml
wvDocBook
wvLatex
wvCleanLatex
wvText
wvWml
libole2/Makefile 
glib-wv/Makefile 
expat/Makefile 
expat/xmltok/Makefile 
expat/xmlparse/Makefile
exporter/Makefile
iconv/Makefile
magick/Makefile
)

if [ test $have_libwmf != yes ]; then
	AC_MSG_WARN(libwv will not have wmf to png support: see README)
fi

if test -z "$HAVE_ZLIB"; then
	AC_MSG_WARN(Could not find zlib library or some of its components: libwv *will* build but will not be able to decompress most wmf files: see README)
fi
