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

dnl Checks for programs.
dnl AC_PROG_CC
dnl AC_PROG_CC shoves in -g by default, i *really* hate this so here goes...
dnl ill shove in the autoconf def of AC_PROG_CC verbatim and remove the -g 
dnl stuff
dnl autoconf stuff begins here

AC_DEFUN(AC_PROG_CC_NO_G,
[AC_BEFORE([$0], [AC_PROG_CPP])dnl
AC_CHECK_PROG(CC, gcc, gcc)
if test -z "$CC"; then
  AC_CHECK_PROG(CC, cc, cc, , , /usr/ucb/cc)
  test -z "$CC" && AC_MSG_ERROR([no acceptable cc found in \$PATH])
fi

AC_PROG_CC_WORKS
AC_PROG_CC_GNU

dnl exec suffix for DOSish OSes, autoconf-2.13 required.

if test "$GCC" = yes; then
  GCC=yes
dnl Check whether -g works, even if CFLAGS is set, in case the package
dnl plays around with CFLAGS (such as to build both debugging and
dnl normal versions of a library), tasteless as that idea is.
  ac_test_CFLAGS="${CFLAGS+set}"
  ac_save_CFLAGS="$CFLAGS"
  CFLAGS=
dnl   C Begin
dnl AC_PROG_CC_G
dnl   C End
  if test "$ac_test_CFLAGS" = set; then
    CFLAGS="$ac_save_CFLAGS"
dnl   C Begin
dnl  elif test $ac_cv_prog_cc_g = yes; then
dnl    CFLAGS="-g -O2"
dnl   C End
  else
    CFLAGS="-O2"
  fi
else
  GCC=
  test "${CFLAGS+set}" = set 
dnl   C Begin
dnl || CFLAGS="-g"
dnl   C End
fi
])

dnl autoconf stuff ends here


AC_PROG_CC_NO_G
AC_PROG_MAKE_SET
AC_PROG_INSTALL

AC_PROG_RANLIB

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

# Configure libtool
#AM_ENABLE_SHARED(no)
#AM_ENABLE_STATIC(no)
AC_LIBTOOL_WIN32_DLL
AM_PROG_LIBTOOL
#end libtool stuff


AC_HEADER_STDC
dnl AC_HEADER_SYS_WAIT
dnl AC_TYPE_SIGNAL
dnl AC_FUNC_WAIT3

AC_CHECK_HEADERS(fcntl.h sys/file.h sys/ioctl.h unistd.h)
CFLAGS="$CFLAGS $X_CFLAGS"


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

dnl LDFLAGS="$LDFLAGS -lm"

dnl test for X, and Xpm and ttf to get the flags needed to compile 
dnl something to use libwmf

dnl finding X
AC_PATH_X
### Check for the pt library (for SCO, needed for X)
AC_CHECK_LIB(pt,ptsname,[X_EXTRA_LIBS="$X_EXTRA_LIBS -lpt"])

AC_PATH_XTRA

if test [ "$no_x" = yes ]; then
	X11=""
else
	X11="-lX11" 
fi

AC_SUBST(X11)
AC_SUBST(X_LIBS)
AC_SUBST(X_EXTRA_LIBS)

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

EXPORTER=
AC_ARG_WITH(exporter,
[  --with-exporter         Enable the exporter
  --without-exporter      Disable the exporter],
   if test x$withval = xno; then
	EXPORTER=
   else
	EXPORTER=EXPORTER
   fi
)

AC_SUBST(EXPORTER)

_cppflags=$CPPFLAGS
_ldflags=$LDFLAGS

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

AC_ARG_WITH(zlib,
[  --with-zlib=DIR         use libz in DIR],[
    if test "$withval" != "no"; then
        ZLIB_DIR=$withval
        CPPFLAGS="$CPPFLAGS -I$withval/include"
        LDFLAGS="$LDFLAGS -L$withval/lib"
    fi
])

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_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 -lz"
            else
                ZLIB_LIBS="-lz"
            fi])))

#AC_CHECK_HEADER(libxml/parser.h,
#       AC_CHECK_LIB(xml, xmlParseDocument,[
#               AC_DEFINE(HAVE_LIBXML2)
#               HAVE_LIBXML2=1
#               LIBXML2_LIBS="-lxml" ]))

AC_PATH_PROG(GLIB_CONFIG, glib-config, , [$PATH])
if test -n "$GLIB_CONFIG"; then
       AC_DEFINE(HAVE_GLIB)
       HAVE_GLIB=1
       GLIB_CFLAGS=`$GLIB_CONFIG --cflags`
       GLIB_LIBS=`$GLIB_CONFIG --libs`
fi

CPPFLAGS=$_cppflags
LDFLAGS=$_ldflags

AC_SUBST(LIBXML2_LIBS)
AC_SUBST(HAVE_LIBXML2)
AC_SUBST(GLIB_LIBS)
AC_SUBST(GLIB_CFLAGS)
AC_SUBST(HAVE_GLIB)
AC_SUBST(ZLIB_CFLAGS)
AC_SUBST(ZLIB_LIBS)

dnl system png support
dnl ========================================================
_cppflags=${CPPFLAGS}
_ldflags=${LDFLAGS}

AC_ARG_WITH(png,
[  --with-png=DIR          use png library in DIR],[
  if test "$withval" != "no"; then
    PNG_DIR=$withval
    CPPFLAGS="$CPPFLAGS -I$withval/include"
    if test "x$ZLIB_CFLAGS" != "x"; then
        CPPFLAGS="$CPPFLAGS $ZLIB_CFLAGS"
    fi
    LDFLAGS="$LDFLAGS -L$withval/lib"
  fi
])

AC_CHECK_HEADER(png.h,
    AC_CHECK_HEADER(pngconf.h,
        AC_CHECK_LIB(png, png_free,[
            AC_DEFINE(HasPNG)
            if test "x$PNG_DIR" != "x"; then
                PNG_CFLAGS="-I$PNG_DIR/include"
                PNG_LIBS="-L$PNG_DIR/lib -lpng"
            else
                PNG_LIBS="-lpng"
            fi],[
            AC_CHECK_LIB(png, png_free,[
                AC_DEFINE(HasPNG)
                if test "x$PNG_DIR" != "x"; then
                    PNG_CFLAGS="-I$PNG_DIR/include"
                    PNG_LIBS="-L$PNG_DIR/lib -lpng"
                else
                    PNG_LIBS="-lpng"
                fi], , $ZLIB_LIBS -lm)])))

CPPFLAGS=$_cppflags
LDFLAGS=$_ldflags
AC_SUBST(PNG_CFLAGS)
AC_SUBST(PNG_LIBS)

dnl system xpm support, done after X, as its usually in the same place
dnl ========================================================

_cppflags=$CPPFLAGS
_ldflags=$LDFLAGS

AC_ARG_WITH(xpm,
[  --with-xpm=DIR          use xpm in DIR],[
    if test "$withval" != "no"; then
        XPM_DIR=$withval
        CPPFLAGS="$CPPFLAGS -I$withval/include"
        LDFLAGS="$LDFLAGS -L$withval/lib"
    fi
])

AC_CHECK_HEADER(X11/xpm.h, [
	AC_DEFINE(HAVE_XPM)
	HAVE_XPM=1
        if test "x$XPM_DIR" != "x"; then
            XPM_CFLAGS="-I$XPM_DIR/include"
	fi
	XPMINCLUDE="<X11/xpm.h>"
	], no_sys_xpm=yes)

if test "x$no_sys_xpm" = "xyes"; then
	AC_CHECK_HEADER(xpm.h, [
			AC_DEFINE(HAVE_XPM)
			HAVE_XPM=1
		        if test "x$XPM_DIR" != "x"; then
		            XPM_CFLAGS="-I$XPM_DIR/include"
			fi
			XPMINCLUDE="<xpm.h>"
			], no_sys_xpm=no)
fi

AC_CHECK_HEADER(malloc.h, [
	AC_DEFINE(HAVE_MALLOC_H)
	HAVE_MALLOC_H=1
	], no_malloc_h=yes)

dnl I dumbly assume that if they have the header, they have the lib
if test "x$no_sys_xpm" != "xyes"; then
	if test "x$XPM_DIR" != "x"; then
            XPM_LIBS="-L$XPM_DIR/lib -lXpm"
        else
            XPM_LIBS="-lXpm"
        fi
	AC_DEFINE(USE_X)
fi

CPPFLAGS=$_cppflags
LDFLAGS=$_ldflags

AC_SUBST(XPM_CFLAGS)
AC_SUBST(XPM_LIBS)
AC_SUBST(XPMINCLUDE)

dnl system ttf support
dnl ========================================================
_cppflags=${CPPFLAGS}
_ldflags=${LDFLAGS}

AC_ARG_WITH(ttf,
[  --with-ttf=DIR          use ttf in DIR],[
    if test "$withval" != "no"; then
        TTF_DIR=$withval
        CPPFLAGS="${CPPFLAGS} -I$withval/include"
        LDFLAGS="${LDFLAGS} -L$withval/lib"
    fi
])

dnl Currently wv requires freetype only if it is being linked against libwmf
dnl so only need to check for library here

AC_CHECK_LIB(ttf,TT_Init_FreeType,[
    AC_DEFINE(HAVE_TTF)
    HAVE_TTF=1
    if test "x$TTF_DIR" != "x"; then
        TTF_CFLAGS="-I$TTF_DIR/include"
        TTF_LIBS="-L$TTF_DIR/lib -lttf"
    else
        TTF_LIBS="-lttf"
    fi],[
    AC_CHECK_LIB(ttf,TT_Done_FreeType,[
        AC_DEFINE(HAVE_TTF)
        HAVE_TTF=1
        if test "x$TTF_DIR" != "x"; then
            TTF_CFLAGS="-I$TTF_DIR/include"
            TTF_LIBS="-L$TTF_DIR/lib -lttf -lnsl"
        else
            TTF_LIBS="-lttf -lnsl"
        fi], ,-lnsl)
    ])

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

AC_SUBST(TTF_LIBS)
AC_SUBST(TTF_CFLAGS)

dnl system libwmf support
dnl ========================================================
_cppflags=${CPPFLAGS}
_ldflags=${LDFLAGS}

AC_ARG_WITH(wmf,
[  --with-wmf=DIR          use wmf in DIR],[
    if test "$withval" != "no"; then
        WMF_DIR=$withval
        CPPFLAGS="${CPPFLAGS} -I$withval/include"
        LDFLAGS="${LDFLAGS} -L$withval/lib"
    fi
])

AC_CHECK_HEADER(gdwmfapi.h,
    AC_CHECK_LIB(gdwmf, gd_draw_rectangle,[
        AC_DEFINE(HAVE_WMF)
        HAVE_WMF=1
        if test "x$WMF_DIR" != "x"; then
            WMF_CFLAGS="-I$WMF_DIR/include"
            WMF_LIBS="-L$WMF_DIR/lib -lgdwmf -lwmf -lxgd -ldib"
        else
            WMF_LIBS="-lgdwmf -lwmf -lxgd -ldib"
        fi], ,
        [-lwmf -lxgd -ldib $X_LIBS $XPM_LIBS $X_PRE_LIBS -lX11 $X_EXTRA_LIBS
         $TTF_LIBS $PNG_LIBS $ZLIB_LIBS $LIBXML2_LIBS -lm]))

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

AC_SUBST(WMF_LIBS)
AC_SUBST(WMF_CFLAGS)

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

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

AC_ARG_WITH(libiconv,
[  --with-libiconv[=DIR]   use libiconv [in DIR]],[
    if test "$withval" = "yes"; then
        check_for_libiconv=yes
    elif test "$withval" = "no"; then
        check_for_libiconv=no
    else
        check_for_libiconv=yes
        LIBICONV_DIR=$withval
        CPPFLAGS="${CPPFLAGS} -I$withval/include"
        LDFLAGS="${LDFLAGS} -L$withval/lib"
    fi
],check_for_libiconv="yes")

have_iconv=no

if test "$check_for_libiconv" = "yes"; then
    AC_CHECK_LIB(iconv,iconv_open,[
        if test "x$LIBICONV_DIR" != "x"; then
            ICONV_CFLAGS="-I$LIBICONV_DIR/include"
            ICONV_LIBS="-L$LIBICONV_DIR/lib -liconv"
        else
            ICONV_LIBS="-liconv"
        fi
        AC_DEFINE(USE_ICONV)
        have_iconv=yes])
fi

if test "$have_iconv" = "no"; then
    AC_CHECK_FUNC(iconv,[
        AC_DEFINE(USE_ICONV)
        have_iconv=yes],[
        ICONVOBJS="\$(RICONVOBJS)"
        ICONV="ICONV"
        AC_MSG_WARN(*** Get Iconv ***)])
fi

AC_DEFUN(AC_SYS_ICONV_WIN_CP_SUPPORT,
[
AC_MSG_CHECKING([for existance of $1 to $2 iconv support])
dnl AC_CACHE_VAL(have_codepages,
dnl [
_libs=${LIBS}
LIBS="${LIBS} ${ICONV_LIBS}"
AC_TRY_RUN([
	#include <iconv.h>
	#include <stdio.h>
	#include <errno.h>
	#include <string.h>
	#include <stdlib.h>

	int main(int argc, char *argv[])
	{
		char f_code[33];              /* From CCSID                           */
		char t_code[33];              /* To CCSID                             */
		iconv_t iconv_handle;         /* Conversion Descriptor returned       */
									/* from iconv_open() function           */
		const char *ibuf = "ABCxyz012#&";   /* Buffer of characters to be converted */
		char *obuf;                   /* Buffer for converted characters      */
		size_t ibuflen;               /* Length of input buffer               */
		size_t obuflen;               /* Length of output buffer              */
		const char *isav;             /* Saved pointer to input buffer        */
		char *osav;                   /* Saved pointer to output buffer       */

	/* All reserved positions of from code (last 12 characters) and to code   */
	/* (last 19 characters) must be set to hexadecimal zeros.                 */

		memset(f_code,'\0',33);
		memset(t_code,'\0',33);

		strcpy(f_code,"$1");
		strcpy(t_code,"$2");

		iconv_handle = iconv_open(t_code,f_code);
		if (iconv_handle == (iconv_t)-1)
		{
			perror("ERROR");
			fprintf(stderr,"iconv_open fail: cannot handle %s to %s\n","$1","$2");
			exit(-1);
		}
		else
			{
			fprintf(stderr,"success!, can handle %s to %s\n","$1","$2");
			iconv_close(iconv_handle);
			}
		return(0);
	}
], have_codepages=yes, have_codepages=no, have_codepages=no)
LIBS=$_libs
dnl])
AC_MSG_RESULT($have_codepages)
if test $have_codepages = no; then
AC_DEFINE(MUST_USE_INTERNAL_ICONV_TABLE)
ICONVOBJS="\$(RICONVOBJS)"
ICONV="ICONV"
fi
])

if [ test "$have_iconv" = yes ]; then
	AC_SYS_ICONV_WIN_CP_SUPPORT(CP874,UCS-2)
dnl	AC_SYS_ICONV_WIN_CP_SUPPORT(CP936,UCS-2)
dnl	AC_SYS_ICONV_WIN_CP_SUPPORT(CP932,UCS-2)
dnl	AC_SYS_ICONV_WIN_CP_SUPPORT(CP949,UCS-2)
dnl	AC_SYS_ICONV_WIN_CP_SUPPORT(CP950,UCS-2)
	AC_SYS_ICONV_WIN_CP_SUPPORT(CP1250,UCS-2)
	AC_SYS_ICONV_WIN_CP_SUPPORT(CP1251,UCS-2)
	AC_SYS_ICONV_WIN_CP_SUPPORT(CP1252,UCS-2)
	AC_SYS_ICONV_WIN_CP_SUPPORT(CP1253,UCS-2)
	AC_SYS_ICONV_WIN_CP_SUPPORT(CP1254,UCS-2)
	AC_SYS_ICONV_WIN_CP_SUPPORT(CP1255,UCS-2)
	AC_SYS_ICONV_WIN_CP_SUPPORT(CP1256,UCS-2)
	AC_SYS_ICONV_WIN_CP_SUPPORT(CP1257,UCS-2)

	AC_SYS_ICONV_WIN_CP_SUPPORT(UCS-2,UCS-2)
	AC_SYS_ICONV_WIN_CP_SUPPORT(UCS-2,UTF-8)
	AC_SYS_ICONV_WIN_CP_SUPPORT(UCS-2,koi8-r)
	AC_SYS_ICONV_WIN_CP_SUPPORT(UCS-2,tis-620)
	AC_SYS_ICONV_WIN_CP_SUPPORT(UCS-2,iso-8859-15)
fi

AC_SUBST(ICONV_CFLAGS)
AC_SUBST(ICONV_LIBS)

AC_SUBST(ICONV)
AC_SUBST(ICONVOBJS)

CPPFLAGS=$_cppflags
LDFLAGS=$_ldflags

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

dnl ========================================================
dnl test for imagemagick
dnl test for the existance of the MagickConfig program,
dnl if its not there then we dont have imagemagick, if it
dnl is there then we still have to do a big dance to get
dnl something to link against it as it does not provide all
dnl the information that we need to compile against it
dnl first try it raw, maybe the author will have fixed it
dnl by now, then try it with the prefix attached, and then
dnl try it with the X varient of that, and then try it with
dnl both.

_cppflags=$CPPFLAGS
_ldflags=$LDFLAGS

AC_ARG_WITH(Magick,
[  --with-Magick=DIR       use Magick in DIR],[
    if test "$withval" != "no"; then
        MAGICK_DIR=$withval
        CPPFLAGS="$CPPFLAGS -I$withval/include"
        LDFLAGS="$LDFLAGS -L$withval/lib"
    fi
])

if test -n "$MAGICK_DIR"; then
    AC_PATH_PROG(MAGICK_CONFIG, Magick-config, ,
    [$MAGICK_DIR:$MAGICK_DIR/bin:$PATH])
else
    AC_PATH_PROG(MAGICK_CONFIG, Magick-config)
fi

if test -n "$MAGICK_CONFIG"; then
    m_cppflags=`$MAGICK_CONFIG --cppflags`
    m_ldflags=`$MAGICK_CONFIG --ldflags`

    CPPFLAGS="$CPPFLAGS $m_cppflags"
    LDFLAGS="$LDFLAGS $m_ldflags"

    AC_CHECK_HEADER(magick/magick.h,
        AC_CHECK_LIB(Magick, GetImageInfo,[
            MAGICK_CFLAGS=$m_cppflags
            MAGICK_LIBS=$m_ldflags
            MAGICK="MAGICK"
            MAGICKOBJS="\$(RMAGICKOBJS)"]))
fi

if test -z "$MAGICK"; then
	MAGICK="MAGICK"
	MAGICK_CFLAGS="-I./magick"
	MAGICK_LIBS="$X_LIBS -lX11 $X_EXTRA_LIBS"
	MAGICKOBJS="\$(RMAGICKOBJS)"
fi

CPPFLAGS=$_cppflags
LDFLAGS=$_ldflags

AC_SUBST(MAGICK_CFLAGS)
AC_SUBST(MAGICK_LIBS)
AC_SUBST(MAGICK)
AC_SUBST(MAGICKOBJS)

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"])
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 
wvAbw
wvHtml
wvLatex
wvCleanLatex
wvText
wvWml
oledecod/Makefile 
libole2/Makefile 
glib-wv/Makefile 
expat/Makefile 
expat/xmltok/Makefile 
expat/xmlparse/Makefile
exporter/Makefile
iconv/Makefile
magick/Makefile
)

if test -z "$HAVE_WMF"; then
	AC_MSG_WARN(Could not find gdwmf library or some of its components: libwv *will* build but will not have wmf to png support: see README)
fi

if test -z "$HAVE_TTF"; then
	AC_MSG_WARN(Could not find ttf library or some of its components: libwv *will* build but will not have text in its wmf to png support: see README)
fi

if test -z "$HAVE_XPM"; then
	AC_MSG_WARN(Could not find xpm library or some of its components: libwv *will* build but will not have full 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
