dnl Process this file with autoconf to produce a configure script.
AC_INIT(pxlib, 0.6.1, http://sourceforge.net/pxlib)
AC_CONFIG_SRCDIR(src/paradox.c)
AM_INIT_AUTOMAKE(AC_PACKAGE_NAME, AC_PACKAGE_VERSION)

AM_MAINTAINER_MODE
AM_CONFIG_HEADER(config.h)
AC_PROG_INTLTOOL

dnl the following sets the soname version
PXLIB_MAJOR_VERSION=0
PXLIB_MINOR_VERSION=6
PXLIB_MICRO_VERSION=1
PXLIB_VERSION=$PXLIB_MAJOR_VERSION.$PXLIB_MINOR_VERSION.$PXLIB_MICRO_VERSION
PXLIB_VERSION_INFO=`expr $PXLIB_MAJOR_VERSION + $PXLIB_MINOR_VERSION`:$PXLIB_MICRO_VERSION:$PXLIB_MINOR_VERSION

AC_SUBST(PXLIB_MAJOR_VERSION)
AC_SUBST(PXLIB_MINOR_VERSION)
AC_SUBST(PXLIB_MICRO_VERSION)
AC_SUBST(PXLIB_VERSION)
AC_SUBST(PXLIB_VERSION_INFO)

dnl VERSION=${PXLIB_VERSION}

PXLIB_BUILD_DATE=`date '+%Y-%m-%d'`
AC_DEFINE_UNQUOTED(PXLIB_BUILD_DATE,"$PXLIB_BUILD_DATE",[pxlib build date])
AC_SUBST(PXLIB_BUILD_DATE)

dnl Checks for programs.
AC_PROG_CC
AC_PROG_INSTALL
AC_PROG_CPP

dnl Add the languages which your application supports here.
ALL_LINGUAS="de es fr"
AM_GLIB_GNU_GETTEXT
GETTEXT_PACKAGE=pxlib
AC_SUBST(GETTEXT_PACKAGE)
AC_DEFINE_UNQUOTED(GETTEXT_PACKAGE, "$GETTEXT_PACKAGE", GETTEXT_PACKAGE)

AM_PROG_LIBTOOL

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

dnl Checks for header files.
AC_HEADER_DIRENT
AC_HEADER_STDC
AC_CHECK_HEADERS(fcntl.h unistd.h ctype.h dirent.h errno.h malloc.h)
AC_CHECK_HEADERS(stdarg.h sys/stat.h sys/types.h time.h)
AC_CHECK_HEADERS(ieeefp.h nan.h math.h fp_class.h float.h)
AC_CHECK_HEADERS(stdlib.h sys/socket.h netinet/in.h arpa/inet.h)
AC_CHECK_HEADERS(netdb.h sys/time.h sys/select.h sys/mman.h)

dnl Checks for library functions.
AC_FUNC_STRFTIME
AC_CHECK_FUNCS(strdup strndup strerror snprintf)
AC_CHECK_FUNCS(finite isnand fp_class class fpclass)
AC_CHECK_FUNCS(strftime localtime)

dnl Checks for inet libraries:
AC_CHECK_FUNC(gethostent, , AC_CHECK_LIB(nsl, gethostent))
AC_CHECK_FUNC(setsockopt, , AC_CHECK_LIB(socket, setsockopt))
AC_CHECK_FUNC(connect, , AC_CHECK_LIB(inet, connect))

dnl Determine what socket length (socklen_t) data type is
AC_MSG_CHECKING([for type of socket length (socklen_t)])

dnl PX_LIBDIR='-L${libdir}'
dnl PX_INCLUDEDIR='-I${includedir}/pxlib -I${includedir}'
dnl PX_LIBS="-lpx $M_LIBS $LIBS"

dnl
dnl Extra flags
dnl
PX_CFLAGS=""

PX_HAVE_ICONV=0
PX_HAVE_RECODE=0

AM_ICONV_LINK

if test "$am_cv_func_iconv" != "yes" ; then
	AC_ARG_WITH(recode, [  --with-recode=DIR        Path to recode library (/usr)])
	if test -r ${withval}/include/recode.h ; then
		RECODE_LIBDIR=-L${withval}/lib
		RECODE_INCLUDEDIR=-I${withval}/include 
	else
		RECODE_LIBDIR=-L/usr
		RECODE_INCLUDEDIR=-I/usr/include 
	fi

	AC_CHECK_LIB(recode, recode_new_outer,
		RECODE_LIBDIR="$RECODE_LIBDIR -lrecode",
		AC_MSG_ERROR([librecode not found]),
		"$RECODE_LIBDIR")

	RECODE_LIBS=-lrecode
	AC_DEFINE(HAVE_RECODE, 1, [Define if you have the recode library.])
	PX_HAVE_RECODE=1
else
	PX_HAVE_ICONV=1
	AC_MSG_CHECKING([for recode])
	AC_MSG_RESULT([not needed, using iconv])
fi

PX_HAVE_GSF=0

AC_ARG_WITH(gsf, [  --with-gsf=DIR           Path to gsf library (/usr)],
if test "x$withval" = xno; then
	AC_MSG_RESULT("disable by user")
	PX_HAVE_GSF=0
else
	if test -r ${withval}/include/libgsf-1/gsf/gsf-input.h ; then
		GSF_LIBDIR=-L${withval}/lib
		GSF_INCLUDEDIR="-I${withval}/include/libgsf-1 -I${withval}/include/glib-2.0 -I${withval}/lib/glib-2.0/include"
		try_gsf=true
	else
		if test -r /usr/include/libgsf-1/gsf/gsf-input.h ; then
			GSF_LIBDIR=-L/usr
			GSF_INCLUDEDIR="-I/usr/include/libgsf-1 -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include"
			try_gsf=true
		else
			try_gsf=false
		fi
	fi

	if test "$try_gsf" = "true"; then
		AC_CHECK_LIB(gsf-1, gsf_input_read,
			GSF_LIBDIR="$GSF_LIBDIR -lgsf-1",
			AC_MSG_RESULT([libgsf-1 not found]),
			"$GSF_LIBDIR")

		GSF_LIBS=-lgsf-1
		AC_DEFINE(HAVE_GSF, 1, [Define if you have the gsf library.])
		PX_HAVE_GSF=1
	fi
fi
)

AC_CHECK_PROG(DOC_TO_MAN, docbook-to-man, docbook-to-man)
if test ! "$DOC_TO_MAN" ; then
	DOCDIR=
	AC_MSG_RESULT([docbook-to-man could not be found. I will not build man pages!])
else
	DOCDIR=doc
fi

AC_ARG_WITH(debug, [  --with-debug            Add the debugging module (on)])
if test "$with_debug" = "no" ; then
    echo Disabling DEBUG support
    WITH_DEBUG=0
    DEBUG_OBJ=
else    
    WITH_DEBUG=1
    DEBUG_OBJ=debugPX.o
fi
AC_SUBST(WITH_DEBUG)
AC_SUBST(DEBUG_OBJ)

AC_SUBST(CFLAGS)
AC_SUBST(PX_CFLAGS)

dnl AC_SUBST(PX_LIBDIR)
dnl AC_SUBST(PX_LIBS)
dnl AC_SUBST(PX_INCLUDEDIR)

AC_SUBST(RECODE_LIBDIR)
AC_SUBST(RECODE_LIBS)
AC_SUBST(RECODE_INCLUDEDIR)

AC_SUBST(PX_HAVE_RECODE)
AC_SUBST(PX_HAVE_ICONV)

AC_SUBST(GSF_LIBDIR)
AC_SUBST(GSF_LIBS)
AC_SUBST(GSF_INCLUDEDIR)

AC_SUBST(PX_HAVE_GSF)

AC_SUBST(M_LIBS)

AC_SUBST(DOCDIR)
AC_SUBST(DOC_TO_MAN)

dnl AC_DEFINE_UNQUOTED(PXLIB_MAJOR_VERSION, "$PXLIB_MAJOR_VERSION", PXLIB_MAJOR_VERSION)
dnl AC_DEFINE_UNQUOTED(PXLIB_MINOR_VERSION, "$PXLIB_MINOR_VERSION", PXLIB_MINOR_VERSION)
dnl AC_DEFINE_UNQUOTED(PXLIB_MICRO_VERSION, "$PXLIB_MICRO_VERSION", PXLIB_MICRO_VERSION)

AC_OUTPUT([
Makefile
pxlib.spec
pxlib.pc
include/Makefile
src/Makefile
po/Makefile.in
doc/Makefile
include/pxversion.h
include/paradox.h
include/paradox-gsf.h
])

echo
echo "Configuration:"
echo "  Prefix:     $prefix"
echo -n "  Recoding:   "
if test "$PX_HAVE_ICONV" = "1" ; then
	echo "Using iconv for recoding.";
else
	if test "$PX_HAVE_RECODE" = "1" ; then
		echo "Using recode for recoding.";
	else
		echo "No support for recoding.";
	fi
fi
echo
