dnl Process this file with autoconf to produce a configure script.
AC_INIT(src/global.h)

WV_MAJOR_VERSION=0
WV_MINOR_VERSION=2
WV_MICRO_VERSION=2
WV_VERSION=$WV_MAJOR_VERSION.$WV_MINOR_VERSION.$WV_MICRO_VERSION

AC_SUBST(WV_MAJOR_VERSION)
AC_SUBST(WV_MINOR_VERSION)
AC_SUBST(WV_MICRO_VERSION)
AC_SUBST(WV_VERSION)

# libtool versioning
LT_CURRENT=1
LT_REVISION=7
LT_AGE=0
AC_SUBST(LT_CURRENT)
AC_SUBST(LT_REVISION)
AC_SUBST(LT_AGE)

# For automake.
VERSION=$WV_VERSION
PACKAGE=wv2

AC_CANONICAL_SYSTEM
AC_CHECK_COMPILERS

dnl Initialize automake stuff
AM_INIT_AUTOMAKE($PACKAGE, $VERSION, no-define)
AM_CONFIG_HEADER(config.h)

dnl Initialize libtool
# Configure libtool
AC_ENABLE_SHARED(yes)
AC_ENABLE_STATIC(no)
AM_PROG_LIBTOOL

dnl Checks for programs.

AC_PROG_CXX
AC_PROG_AWK
AC_PROG_CC
AC_PROG_CPP
AC_PROG_INSTALL
AC_PROG_LN_S
AC_PROG_MAKE_SET

dnl Debug options

dnl Checks for libraries, etc.
AC_C_BIGENDIAN

dnl Checks for glib
AC_CHECK_SIZEOF(char)
AC_CHECK_SIZEOF(short)
AC_CHECK_SIZEOF(long)
AC_CHECK_SIZEOF(int)
AC_CHECK_SIZEOF(void *)

# Checks for library functions.
AC_FUNC_MEMCMP
AC_FUNC_STRTOD

dnl Checks for KJS::UString
AC_CHECK_HEADERS(strings.h string.h math.h float.h ieeefp.h errno.h)
AC_CHECK_LIB(m, isnan, [
  AC_DEFINE_UNQUOTED(HAVE_FUNC_ISNAN, 1, [Define if you have isnan])
])
AC_CHECK_LIB(m, finite, [
  AC_DEFINE_UNQUOTED(HAVE_FUNC_FINITE, 1, [Define if you have finite])
])
AC_CHECK_LIB(m, _finite, [
  AC_DEFINE_UNQUOTED(HAVE_FUNC__FINITE, 1, [Define if you have _finite])
])

AH_TOP([
#ifndef LIBWV2_CONFIG_H
#define LIBWV2_CONFIG_H
])

AH_BOTTOM([
#ifdef ICONV_REQUIRES_CONST
#define ICONV_CONST const
#else
#define ICONV_CONST
#endif

#endif /* ! LIBWV2_CONFIG_H */
])

dnl === Check for libgsf-1 ===
KDE_PKG_CHECK_MODULES(LIBGSF, libgsf-1 >= 1.7.2)
AC_SUBST(LIBGSF_CFLAGS)
AC_SUBST(LIBGSF_LIBS)

dnl ==== Check for iconv ====
wv_iconv=no

WV_ICONV_CFLAGS=""
WV_ICONV_LDFLAGS=""

AC_ARG_WITH(libiconv,[  --with-libiconv[=DIR]     use libiconv (in DIR)],[
	if [ test "x$withval" != "xno" ]; then
		if [ test "x$withval" != "xyes" ]; then
			ICONV_DIR=$withval
		fi
		default_to_libiconv=yes
		search_for_libiconv=yes
	else
		default_to_libiconv=no
		search_for_libiconv=no
	fi
],[	default_to_libiconv=no
	search_for_libiconv=yes
])

if [ test $default_to_libiconv = no ]; then
	dnl test for system iconv
	dnl Quesion: Do all systems with iconv also have iconv.h?
	dnl Note: FreeBSD 5.0 seems to have iconv.h in sys
	AC_CHECK_FUNC(iconv,[
		AC_CHECK_HEADERS(iconv.h sys/iconv.h)
		search_for_libiconv=no
		wv_iconv=iconv
	])
fi

if [ test $search_for_libiconv = yes ]; then
	_cppflags=$CPPFLAGS
	_ldflags=$LDFLAGS
	if [ test "x$ICONV_DIR" != "x" ]; then
		WV_ICONV_CFLAGS="-I$ICONV_DIR/include"
		WV_ICONV_LDFLAGS="-L$ICONV_DIR/lib -liconv"
	else
		WV_ICONV_LDFLAGS="-liconv"
	fi
	CPPFLAGS="$CPPFLAGS $WV_ICONV_CFLAGS"
	LDFLAGS="$LDFLAGS $WV_ICONV_LDFLAGS"
	AC_CHECK_LIB(iconv,libiconv_open,[
		AC_CHECK_HEADER(iconv.h,[
			AC_DEFINE(HAVE_ICONV_H, 1, [Define to 1 if you have the <iconv.h> header file.])
			wv_iconv=libiconv
		],[	AC_CHECK_HEADER(sys/iconv.h,[
				AC_DEFINE(HAVE_SYS_ICONV_H, 1, [Define to 1 if you have the <sys/iconv.h> header file.])
				wv_iconv=libiconv
                        ],[ AC_MSG_ERROR(* * * please check libiconv is installed correctly * * *)
                        ])
		])
	],[	AC_MSG_ERROR(* * * please ensure libiconv is installed * * *)
	])
	CPPFLAGS=$_cppflags
	LDFLAGS=$_ldflags
fi

if [ test $wv_iconv = no ]; then
	AC_MSG_ERROR(* * * No iconv support - unable to continue * * *)
fi

AC_LANG_CPLUSPLUS

CPPFLAGS="$CPPFLAGS $WV_ICONV_CFLAGS"
LDFLAGS="$LDFLAGS $WV_ICONV_LDFLAGS"
AC_MSG_CHECKING(need for const in iconv)
AC_TRY_COMPILE([
#ifdef HAVE_ICONV_H
#include <iconv.h>
#endif
#ifdef HAVE_SYS_ICONV_H
#include <sys/iconv.h>
#endif
],[
	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, 1, [Define to 1 if iconv requires const var in argument 2])
])

AC_LANG_C

dnl Check if the neede conversions are supported
AC_MSG_CHECKING(whether the iconv installation is complete enough)
AC_TRY_RUN([
#ifdef HAVE_ICONV_H
#include <iconv.h>
#endif
#ifdef HAVE_SYS_ICONV_H
#include <sys/iconv.h>
#endif
#include <stdlib.h>

int check( const char* from, const char* to )
{
    iconv_t myConverter = iconv_open( to, from );

    if ( myConverter != (iconv_t)-1 ) {
	iconv_close( myConverter );
	return 0;
    }
    else
	return 1;
}

int main(int argc, char** argv)
{
    const char* from[] = { "CP874", "CP932", "CP936", "CP949",
			   "CP950", "CP1250", "CP1251", "CP1252",
			   "CP1253", "CP1254", "CP1255", "CP1256",
			   "CP1257", "koi8-r", 0 };
    const char* to[] = { "UNICODELITTLE", "UNICODEBIG", 0 };
    int fromIndex = 0;
    int toIndex = 0;

    while ( to[ toIndex ] != 0 ) {
	while( from[ fromIndex ] != 0 ) {
	    if ( check( from[ fromIndex ], to[ toIndex ] ) != 0 )
		exit( 1 );
	    fromIndex = fromIndex + 1;
	}
	toIndex = toIndex + 1;
	fromIndex = 0;
    }
    exit( 0 );
}
], iconvFlag="yes", iconvFlag="no", iconvFlag="no")

if test "x$iconvFlag" = "xyes"; then
	AC_MSG_RESULT(yes)
else
	AC_MSG_RESULT(no)
	echo
	echo "wv2 depends on a modern iconv installation, supporting UNICODELITTLE and"
	echo "UNICODEBIG. The detected iconv version doesn't support these conversions."
	echo
	echo "Please get a new libiconv from http://www.gnu.org/software/libiconv/"
	echo "You might want to install the library to some alternative directory, in"
	echo "order not to overwrite your current installation. Please use the option"
	echo "--with-libiconv=DIR to specify the location."
	echo
	AC_MSG_ERROR(* * * No iconv support - unable to continue * * *)
fi

CPPFLAGS=$_cppflags
LDFLAGS=$_ldflags

AC_SUBST(WV_ICONV_CFLAGS)
AC_SUBST(WV_ICONV_LDFLAGS)

dnl ==== Final config stuff ====

WV_CFLAGS="$LIBGSF_CFLAGS $WV_ICONV_LDFLAGS"
AC_SUBST(WV_CFLAGS)
WV_LDFLAGS="$LIBGSF_LIBS $WV_ICONV_LDFLAGS"
AC_SUBST(WV_LDFLAGS)

AC_OUTPUT([
Makefile
src/Makefile
src/generator/Makefile
tests/Makefile
wv2-config
])
