AC_INIT(src/xml.c)

VERSION=1.0.2
PACKAGE=xmlstarlet

AM_INIT_AUTOMAKE($PACKAGE, $VERSION)
AM_MAINTAINER_MODE
AC_CANONICAL_HOST
AC_PROG_CC

EXTRA_LIBS=

case "${host}" in
  *-hpux* )
      if test "$GCC" != "yes"; then
        CFLAGS="-Ae"
      fi
      ;;
  * )
      ;;
esac

dnl
dnl The following parameters offer
dnl the ability to specify the location of the libxml
dnl library during linking and compilation.
dnl
AC_ARG_WITH(libxml-prefix,
        [  --with-libxml-prefix=[PFX]		Specify location of libxml],
        LIBXML_PREFIX=$withval
        LIBXML_CFLAGS="-I$withval/include/libxml2 -I$withval/include/libxml"
        LIBXML_LIBS="$withval/lib/libxml2.a -lz -lm -lpthread -ldl"
)
        
AC_ARG_WITH(libxml-include-prefix,
        [  --with-libxml-include-prefix=[PFX]	Specify location of libxml headers],
        LIBXML_CFLAGS="-I$withval/libxml2 -I$withval/libxml -I$withval"
)

AC_ARG_WITH(libxml-libs-prefix,
        [  --with-libxml-libs-prefix=[PFX]	Specify location of libxml libs],
        LIBXML_LIBS="$withval/libxml2.a -lz -lm -lpthread -ldl"
)

AC_ARG_WITH(libxml-src,
        [  --with-libxml-src=[DIR]               For libxml thats not installed yet (sets all three above)],
        LIBXML_SRC="$withval"
        LIBXML_LIBS="$withval/.libs/libxml2.a -lz -lm -lpthread"
        LIBXML_CFLAGS="-I$withval/include/libxml2 -I$withval/include/libxml -I$withval/include"
)

AC_DEFUN(VERSION_TO_NUMBER,
	 [`$1 | sed -e 's/libxml //' -e 's/libxslt //' | awk 'BEGIN { FS = "."; } { printf "%d", ([$]1 * 1000 + [$]2) * 1000 + [$]3;}'`])

LIBXML_REQUIRED_VERSION=2.6.12

if test "x$LIBXML_SRC" = "x"
then
	XMLVERS=`$LIBXML_PREFIX/bin/xml2-config --version`
	if test VERSION_TO_NUMBER(echo $XMLVERS) -lt VERSION_TO_NUMBER(echo $LIBXML_REQUIRED_VERSION); then
        	AC_MSG_ERROR(xmlstarlet needs at least libxml2 version $LIBXML_REQUIRED_VERSION (http://www.xmlsoft.org/))
	fi
fi

dnl
dnl The following parameters offer
dnl the ability to specify the location of the libxslt
dnl library during linking and compilation.
dnl
AC_ARG_WITH(libxslt-prefix,
        [  --with-libxslt-prefix=[PFX]		Specify location of libxslt],
	LIBXSLT_PREFIX=$withval
        LIBXSLT_CFLAGS="-I$withval/include/libxslt -I$withval/include/libexslt"
        LIBXSLT_LIBS="$withval/lib/libxslt.a $withval/lib/libexslt.a -lz -lm -lpthread"
)
        
AC_ARG_WITH(libxslt-include-prefix,
        [  --with-libxslt-include-prefix=[PFX]	Specify location of libxslt headers],
        LIBXSLT_CFLAGS="-I$withval/libxslt -I$withval/libexslt -I$withval"
)

AC_ARG_WITH(libxslt-libs-prefix,
        [  --with-libxslt-libs-prefix=[PFX]	Specify location of libxslt libs],
        LIBXSLT_LIBS="$withval/libxslt.a $withval/libexslt.a -lz -lm -lpthread"
)

AC_ARG_WITH(libxslt-src,
        [  --with-libxslt-src=[DIR]		For libxslt thats not installed yet (sets all three above)],
        LIBXSLT_SRC="$withval"
        LIBXSLT_LIBS="$withval/libxslt/.libs/libxslt.a $withval/libexslt/.libs/libexslt.a -lz -lm -lpthread"
        LIBXSLT_CFLAGS="-I$withval/libxslt -I$withval/libexslt -I$withval"
)

LIBXSLT_REQUIRED_VERSION=1.1.9

if test "x$LIBXSLT_SRC" = "x"
then
	XSLTVERS=`$LIBXSLT_PREFIX/bin/xslt-config --version`
	if test VERSION_TO_NUMBER(echo $XSLTVERS) -lt VERSION_TO_NUMBER(echo $LIBXSLT_REQUIRED_VERSION); then
        	AC_MSG_ERROR(xmlstarlet needs at least libxslt version $LIBXSLT_REQUIRED_VERSION (http://www.xmlsoft.org/))
	fi
fi

LIBICONV_PREFIX="/usr"
AC_ARG_WITH(libiconv-prefix,
        [  --with-libiconv-prefix=[PFX]		Specify location of lib iconv],
        LIBICONV_PREFIX=$withval
        LIBICONV_CFLAGS="-I$withval/include"
        LIBICONV_LIBS="-L$withval/lib"
)
         
AC_ARG_WITH(libiconv-include-prefix,
        [  --with-libiconv-include-prefix=[PFX]	Specify location of libiconv headers],
        LIBICONV_CFLAGS="-I$withval"
)
 
AC_ARG_WITH(libiconv-libs-prefix,
        [  --with-libiconv-libs-prefix=[PFX]	Specify location of libiconv libs],
        LIBICONV_LIBS="-L$withval/lib"
)

dnl
dnl Check the environment
dnl

AC_ISC_POSIX
AC_PROG_CC
AC_STDC_HEADERS
AC_ARG_PROGRAM
dnl AM_PROG_LIBTOOL

dnl
dnl Math detection
dnl

AC_CHECK_HEADERS(sys/types.h sys/time.h stdlib.h unistd.h string.h)
AC_CHECK_HEADERS(ieeefp.h nan.h math.h fp_class.h float.h ansidecl.h)
AC_CHECK_HEADERS(sys/timeb.h time.h sys/stat.h stdarg.h)
AC_CHECK_FUNCS(stat _stat)
dnl AC_CHECK_FUNC(isnan, , AC_CHECK_LIB(m, isnan,
dnl   [M_LIBS="-lm"; AC_DEFINE(HAVE_ISNAN)]))

dnl AC_CHECK_FUNC(isinf, , AC_CHECK_LIB(m, isinf,
dnl   [M_LIBS="-lm"; AC_DEFINE(HAVE_ISINF)]))

dnl AC_CHECK_FUNC(pow, , AC_CHECK_LIB(m, pow,
dnl   [M_LIBS="-lm"; AC_DEFINE(HAVE_POW)]))

dnl AC_CHECK_FUNC(floor, , AC_CHECK_LIB(m, floor,
dnl   [M_LIBS="-lm"; AC_DEFINE(HAVE_FLOOR)]))

dnl AC_CHECK_FUNC(fabs, , AC_CHECK_LIB(m, fabs,
dnl   [M_LIBS="-lm"; AC_DEFINE(HAVE_FABS)]))
AC_CHECK_FUNCS(gettimeofday)
AC_CHECK_FUNCS(mktime localtime asctime time gmtime ftime)
AC_CHECK_FUNCS(strdup)

dnl Checking the standard string functions availability
AC_CHECK_FUNCS(printf sprintf fprintf snprintf vfprintf vsprintf vsnprintf sscanf,,
               NEED_TRIO=1)
dnl
dnl Check for trio string functions
dnl

if test "${NEED_TRIO}" = "1" ; then
    echo Reusing trio library for string functions
    WITH_TRIO=1
else
    WITH_TRIO=0
fi
AC_SUBST(WITH_TRIO)

WIN32_EXTRA_LIBADD=
WIN32_EXTRA_LDFLAGS=

case "${host}" in
  *sun* )
      LIBXML_LIBS="${LIBXML_LIBS} -lsocket -lnsl"
      LIBICONV_LIBS=
      ;;
  *cygwin* )
      if test "x$LIBICONV_LIBS" = "x"
      then
          LIBXML_LIBS="${LIBXML_LIBS} ${LIBICONV_LIBS} -liconv"
      else
          LIBXML_LIBS="${LIBXML_LIBS} -liconv"
      fi
      ;;
  *mac* )
      if test "x$LIBICONV_LIBS" = "x"
      then
          LIBXML_LIBS="${LIBXML_LIBS} ${LIBICONV_LIBS} -liconv"
      else
          LIBXML_LIBS="${LIBXML_LIBS} -liconv"
      fi
      ;;
  *mingw* )
      LIBICONV_LIBS=
      WIN32_EXTRA_LIBADD="-lwsock32"
      WIN32_EXTRA_LDFLAGS="-no-undefined"
      AC_DEFINE([_WINSOCKAPI_],1,[Using the Win32 Socket implementation])
      AC_DEFINE([snprintf],[_snprintf],[Win32 Std C name mangling work-around])
      AC_DEFINE([vsnprintf],[_vsnprintf],[Win32 Std C name mangling work-around])
      LIBXML_LIBS=`echo "$LIBXML_LIBS" | sed -e 's/ -lz//g' -e 's/ -lpthread//g'`
      LIBXSLT_LIBS=`echo "$LIBXSLT_LIBS" | sed -e 's/ -lz//g' -e 's/ -lpthread//g'`
      ;;
  *-hpux* )
      LIBXML_LIBS=`echo "$LIBXML_LIBS" | sed -e 's/ -lz//g'`
      LIBXSLT_LIBS=`echo "$LIBXSLT_LIBS" | sed -e 's/ -lz//g'`
      ;;
  * )
      LIBICONV_LIBS=
      ;;
esac

AC_SUBST(WIN32_EXTRA_LIBADD)
AC_SUBST(WIN32_EXTRA_LDFLAGS)

AC_SUBST(CFLAGS)
AC_SUBST(CPPFLAGS)
AC_SUBST(LDFLAGS)

dnl
dnl Set LIBXML include path
dnl
if test "x$LIBXML_CFLAGS" = "x"
then
	LIBXML_CFLAGS="-I${LIBXML_PREFIX}/include/libxml2"
fi

dnl
dnl Set LIBXML libraries location
dnl
if test "x$LIBXML_LIBS" = "x"
then
	LIBXML_LIBS="${LIBXML_PREFIX}/lib/libxml2.a -lz -lm -lpthread -ldl"
fi

dnl
dnl Set LIBXSLT include path
dnl
if test "x$LIBXSLT_CFLAGS" = "x"
then
	LIBXSLT_CFLAGS="-I${LIBXSLT_PREFIX}/include/libxslt -I${LIBXSLT_PREFIX}/include/libexslt"
fi

dnl
dnl Set LIBXSLT libraries location
dnl
if test "x$LIBXSLT_LIBS" = "x"
then
	LIBXSLT_LIBS="${LIBXSLT_PREFIX}/lib/libxslt.a ${LIBXSLT_PREFIX}/lib/libexslt.a -lz -lm -lpthread"
fi

dnl
dnl Set LIBICONV include path
dnl
if test "x$LIBICONV_CFLAGS" = "x"
then
	LIBICONV_CFLAGS="-I${LIBICONV_PREFIX}/include"
fi

dnl
dnl Set LIBICONV libraries location
dnl
if test "x$LIBICONV_LIBS" = "x"
then
	LIBICONV_LIBS="-L${LIBXSLT_PREFIX}/lib"
fi

AC_SUBST(VERSION)
AC_SUBST(PACKAGE)
AC_SUBST(EXTRA_LIBS)
AC_SUBST(LIBXML_LIBS)
AC_SUBST(LIBXML_CFLAGS)
AC_SUBST(LIBXSLT_LIBS)
AC_SUBST(LIBXSLT_CFLAGS)
AC_SUBST(LIBICONV_LIBS)
AC_SUBST(LIBICONV_CFLAGS)

dnl for the spec file
RELDATE=`date +'%a %b %e %Y'`
AC_SUBST(RELDATE)

AM_CONFIG_HEADER(config.h)

rm -f COPYING
ln -s Copyright COPYING

AC_OUTPUT([
xmlstarlet.spec
Makefile
src/Makefile
doc/Makefile
solaris/package/sol8-sparc/pkginfo
solaris/package/sol9-sparc/pkginfo
])

