-*- mode: m4 -*-
dnl We require Automake 1.7.1, which requires Autoconf 2.54.
AC_PREREQ(2.54)

m4_define([libgsf_version_major], [1])
m4_define([libgsf_version_minor], [12])
m4_define([libgsf_version_micro], [3])
m4_define([libgsf_version_extra], [])
m4_define([libgsf_full_version],
    [libgsf_version_major.libgsf_version_minor.libgsf_version_micro[]libgsf_version_extra])

AC_INIT(libgsf, [libgsf_full_version],
	[http://bugzilla.gnome.org/enter_bug.cgi?product=libgsf])
AC_CONFIG_SRCDIR(gsf/gsf.h)

AC_SUBST(LIBGSF_MAJOR_VERSION, libgsf_version_major)
AC_SUBST(LIBGSF_MINOR_VERSION, libgsf_version_minor)
AC_SUBST(LIBGSF_MICRO_VERSION, libgsf_version_micro)

dnl Version info for libraries = CURRENT:REVISION:AGE
AC_SUBST(VERSION_INFO,
m4_eval(libgsf_version_major + libgsf_version_minor)[]dnl
:libgsf_version_micro:libgsf_version_minor)

dnl FIXME: the above formula and the following explanation are wrong:  --kasal
dnl
dnl Set release number
dnl This is derived from "Versioning" chapter of info libtool documentation.
dnl     4a) Increment when removing or changing interfaces.
dnl LIBGSF_MAJOR_VERSION=1
dnl     4a) 5) Increment when adding interfaces.
dnl     6) Set to zero when removing or changing interfaces.
dnl LIBGSF_MINOR_VERSION=12
dnl     3) Increment when interfaces not changed at all,
dnl               only bug fixes or internal changes made.
dnl     4b) Set to zero when adding, removing or changing interfaces.
dnl LIBGSF_MICRO_VERSION=0

AM_INIT_AUTOMAKE(dist-bzip2)

AC_CONFIG_HEADERS(gsf-config.h)
AM_MAINTAINER_MODE

dnl Propagate Gnome-specific variable ACLOCAL_FLAGS to Makefile.
AC_SUBST(ACLOCAL_AMFLAGS, $ACLOCAL_FLAGS)

dnl Checks for programs.
AC_PROG_CC
AC_PROG_MAKE_SET
AC_LIBTOOL_WIN32_DLL
AM_PROG_LIBTOOL

AC_FUNC_FSEEKO
AC_SYS_LARGEFILE
AC_PATH_PROG(PKG_CONFIG, pkg-config, no)

dnl Gettext/i18n stuff

AC_PROG_INTLTOOL

ALL_LINGUAS="es"

AM_GLIB_GNU_GETTEXT

GETTEXT_PACKAGE=AC_PACKAGE_NAME
AC_SUBST(GETTEXT_PACKAGE)
AC_DEFINE_UNQUOTED(GETTEXT_PACKAGE,"$GETTEXT_PACKAGE", [Gettext package.])

dnl Checks for libraries.
ifelse([
  If we defined the module lists on the M4 level, we could have problems
  with PKG_CHECK_MODULES from pkgconfig 0.16.0, which double quotes its
  second argument (the module list).
  As a handy workaround, we use shell variables.
])
dnl Modules common to libgsf and libgsf-gnome
libgsf_reqs="
    gobject-2.0 >= 2.4.0
    glib-2.0 >= 2.4.0
    libxml-2.0 >= 2.4.16
"
dnl Modules required for libgsf-gnome
libgsf_gnome_reqs="
    $libgsf_reqs
    libbonobo-2.0 >= 2.0.0
    gnome-vfs-2.0 >= 2.2.0
    gnome-vfs-module-2.0 >= 2.2.0
"
PKG_CHECK_MODULES(LIBGSF, $libgsf_reqs)

## this should come after `AC_PROG_CC'
ifdef([GNOME_COMPILE_WARNINGS],[GNOME_COMPILE_WARNINGS],)
CFLAGS="$CFLAGS $WARN_CFLAGS -DG_DISABLE_DEPRECATED"
if test "$GCC" = "yes"; then
	for option in -Wno-system-headers -Wfloat-equal -Wpointer-arith -Wbad-function-cast -Wwrite-strings -Wsign-compare -Waggregate-return -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wformat -Wnested-externs -Winline -Wdeclaration-after-statement -Wundef -W -Wmissing-noreturn -Wmissing-format-attribute; do
		SAVE_CFLAGS="$CFLAGS"
		CFLAGS="$CFLAGS $option"
		AC_MSG_CHECKING([whether gcc understands $option])
		AC_COMPILE_IFELSE([AC_LANG_PROGRAM([], [])],
			has_option=yes,
			has_option=no)
		if test $has_option = no; then
		  CFLAGS="$SAVE_CFLAGS"
		fi
		AC_MSG_RESULT($has_option)
		unset has_option
		unset SAVE_CFLAGS
	done
	unset option
fi

AC_CHECK_DECL(fdopen, fdopen_works=yes, fdopen_works=no)
if test $fdopen_works = no ; then
	unset ac_cv_have_decl_fdopen
	CFLAGS="$CFLAGS -D_POSIX_SOURCE"
	AC_MSG_NOTICE([adding -D_POSIX_SOURCE to CFLAGS])
	AC_CHECK_DECL(fdopen, fdopen_works=yes, fdopen_works=no)
	if test $fdopen_works = no ; then
		AC_MSG_ERROR([fdopen is not available])
	fi
fi

# Unfortunately, -D_POSIX_SOURCE turns off struct timeval on Solaris
AC_MSG_CHECKING([whether struct timeval is available])
for try in 1 2; do
	AC_COMPILE_IFELSE(
		[AC_LANG_PROGRAM(
			[[#include <sys/time.h>]],
			[[struct timeval tv;]])],
		struct_timeval_works=yes,
		struct_timeval_works=no)
	test $struct_timeval_works = yes && break
	# Try this for the second attempt:
	test $try = 1 && CFLAGS="$CFLAGS -D__EXTENSIONS__"
done
AC_MSG_RESULT($struct_timeval_works)
if test $struct_timeval_works = no ; then
	AC_MSG_ERROR([struct timeval is not available])
fi

AC_MSG_CHECKING([whether -D_BSD_SOURCE is needed for caddr_t])
AC_COMPILE_IFELSE(
	[AC_LANG_PROGRAM([[#include <sys/types.h>]], [[caddr_t ca]])],
	need_bsd1=no,
	need_bsd1=yes)
AC_MSG_RESULT($need_bsd1)

AC_MSG_CHECKING([whether -D_BSD_SOURCE is needed for lstat])
AC_LINK_IFELSE([AC_LANG_PROGRAM(
[[#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>]],
[[void *ptr = &lstat]])],
need_bsd2=no, need_bsd2=yes)
AC_MSG_RESULT($need_bsd2)
if test $need_bsd1 = yes -o $need_bsd2 = yes; then
	CFLAGS="$CFLAGS -D_BSD_SOURCE"
fi

AC_TYPE_MODE_T
AC_CHECK_HEADERS(fcntl.h malloc.h unistd.h io.h)
AC_FUNC_MMAP

AC_CHECK_FUNCS(chown)

SAVE_CFLAGS=$CFLAGS
SAVE_LIBS=$LIBS
CFLAGS="$CFLAGS $LIBGSF_CFLAGS"
LIBS="$LIBGSF_LIBS $LIBS"
AC_MSG_CHECKING([for g_chmod])
AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include <glib/gstdio.h>]], [[(void)g_chmod("/xxx",0777);]])],
               [AC_DEFINE(HAVE_G_CHMOD, 1, [Define if g_chmod is available as macro or function])
		AC_MSG_RESULT(yes)],
	       [AC_MSG_RESULT(no)])
AC_MSG_CHECKING([for g_access])
AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include <glib/gstdio.h>]], [[(void)g_access("/xxx",0777);]])],
               [AC_DEFINE(HAVE_G_ACCESS, 1, [Define if g_access is available as macro or function])
		AC_MSG_RESULT(yes)],
	       [AC_MSG_RESULT(no)])
CFLAGS=$SAVE_CFLAGS
LIBS=$SAVE_LIBS

AC_MSG_CHECKING([whether macro S_ISREG is available])
AC_COMPILE_IFELSE([AC_LANG_PROGRAM(
[[#include <sys/types.h>
#include <sys/stat.h>]],
[[S_ISREG(S_IFMT);]])],
	[macro_s_isreg=yes],
	[AH_TEMPLATE([S_ISREG], [Define S_ISREG if stat.h doesn't])
	 AC_DEFINE([S_ISREG(m)], [(((m) & S_IFMT) == S_IFREG)])
	 macro_s_isreg=no])
AC_MSG_RESULT($macro_s_isreg)

AC_MSG_CHECKING([for native Win32])
case $host in
  *-*-mingw*)
    native_win32=yes
    ;;
  *)
    native_win32=no
    ;;
esac
AC_MSG_RESULT([$native_win32])
AM_CONDITIONAL(OS_WIN32, test "$native_win32" = yes)

AC_MSG_CHECKING([for Win32 platform in general])
case $host in
  *-*-mingw*|*-*-cygwin*)
    platform_win32=yes
    ;;
  *)
    platform_win32=no
    ;;
esac
AC_MSG_RESULT($platform_win32)
AM_CONDITIONAL(PLATFORM_WIN32, test "$platform_win32" = yes)

##################################################
# Checks for gtk-doc and docbook-tools
##################################################
gtk_doc_installed=true
#ifdef([GTK_DOC_CHECK],[GTK_DOC_CHECK([1.0])],[gtk_doc_installed=false])
# I would have liked to conditionalize this, but 
# that appears to break other things http://bugzilla.gnome.org/show_bug.cgi?id=156399
GTK_DOC_CHECK([1.0])
AM_CONDITIONAL(GTK_DOC_INSTALLED, $gtk_doc_installed)

dnl ***********************************************************************************
# Check for zlib.
_cppflags=$CPPFLAGS
_ldflags=$LDFLAGS

Z_DIR= Z_LIBS= Z_CPPFLAGS=

AC_ARG_WITH(zlib,
        [[  --with-zlib=DIR       use libz in DIR]],
        [case $withval in
         yes|no) ;;
         *)     Z_DIR=$withval
                CPPFLAGS="${CPPFLAGS} -I$withval/include"
                LDFLAGS="${LDFLAGS} -L$withval/lib"
                ;;
         esac])

if test "x$with_zlib" != xno; then
        with_zlib=no
        AC_CHECK_HEADER(zlib.h, [AC_CHECK_LIB(z, gzread, [with_zlib=yes])])
fi
if test "$with_zlib" = no; then
        AC_MSG_ERROR([*** zlib is required])
fi
if test "x$Z_DIR" != "x"; then
        Z_CPPFLAGS="-I$Z_DIR/include"
        case $host in
        *-*-solaris*)   Z_LIBS="-L$Z_DIR/lib -R$Z_DIR/lib -lz" ;;
        *)              Z_LIBS="-L$Z_DIR/lib -lz" ;;
        esac
else
        Z_LIBS="-lz"
fi
AC_SUBST(Z_CPPFLAGS)
AC_SUBST(Z_LIBS)

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

dnl ***********************************************************************************
pygtk_msg=no

dnl Uncomment to enable Python bindings.
dnl
dnl AM_PATH_PYTHON
dnl AM_CHECK_PYTHON_HEADERS(,[AC_MSG_ERROR(could not find Python headers)])
dnl # check for pygtk
dnl PKG_CHECK_MODULES(PYGTK, pygtk-2.0 >= 1.99.10,
dnl 		  [pygtk_msg="yes"],
dnl 		  [pygtk_msg="NO.  pygtk problem"])
dnl AC_SUBST(PYGTK_CFLAGS)
dnl AC_PATH_PROG(PYGTK_CODEGEN, pygtk-codegen-2.0, no)
dnl if test "x$PYGTK_CODEGEN" = xno; then
dnl    AC_MSG_ERROR(could not find pygtk-codegen-2.0 script)
dnl fi

AM_CONDITIONAL(WITH_PYTHON, [test "$pygtk_msg" = yes])

dnl ***********************************************************************************

dnl bz2
dnl
BZ2_LIBS=
test_bz2=true
AC_ARG_WITH(bz2,
	[--{with,without}-bz2   Build additional wrappers for BZ2],
	if test "x$withval" = xno; then
		test_bz2=false
	fi
)

with_bz2=false
if test "x$test_bz2" = xtrue ; then
   AC_CHECK_LIB(bz2, BZ2_bzDecompressInit, 
		bz2_ok=yes,
		bz2_ok=no
		AC_MSG_WARN(*** BZ2 support disabled (BZ2 library not found) ***))

   if test "$bz2_ok" = yes; then
      AC_MSG_CHECKING([for bzlib.h])
      AC_PREPROC_IFELSE(
	[AC_LANG_SOURCE(
	[[#include <stdio.h>
	#undef PACKAGE
	#undef VERSION
	#undef HAVE_STDLIB_H
	#include <bzlib.h>]])],
        bz2_ok=yes,
        bz2_ok=no)
      AC_MSG_RESULT($bz2_ok)
   fi

   if test "$bz2_ok" = yes; then
	AC_DEFINE(HAVE_BZ2, 1, [Is bzip2 available and enabled])
	BZ2_LIBS="-lbz2"
   else
	AC_MSG_WARN(*** BZ2 support disabled (BZ2 header not found) ***)
   fi
else
   AC_MSG_WARN([BZ2 support disabled, as requested (Use --with-bz2 to enable)])
fi

AC_SUBST(BZ2_LIBS)

dnl ***********************************************************************************

dnl gnome
dnl
want_gnome=auto
AC_ARG_WITH(gnome,
	[--{with,without}-gnome   Build additional wrappers for GNOME-VFS and BONOBO],
	[case $withval in
	yes) want_gnome=yes;;
	no) want_gnome=no;;
	esac[]dnl
])
have_gnome=no
if  test $want_gnome = no ; then 
    AC_MSG_WARN([GNOME support disabled, as requested (Use --with-gnome to enable)])
else
    PKG_CHECK_MODULES(LIBGSF_GNOME, $libgsf_gnome_reqs,
	[have_gnome=yes],
	[if test $want_gnome = yes; then
	    AC_MSG_ERROR([GNOME support requested, but not available.])
	 else
	    AC_MSG_WARN([GNOME support disabled, unable to find required version of VFS and/or Bonobo])
	 fi])
fi
AM_CONDITIONAL(WITH_LIBGSF_GNOME, test $have_gnome = yes)

# GConf configuration
dnl Don't publish the GCONFTOOL variable, AM_GCONF_SOURCE_2 has ``gconftool-2'' hardwired.
GCONFTOOL=
AC_CHECK_PROG([GCONFTOOL], [gconftool-2], [gconftool-2], [no])
if test "x$GCONFTOOL" = "xno"; then
    AC_MSG_WARN([thumbnailer will not be built, unable to find gconftool-2])
else
    AM_GCONF_SOURCE_2
fi
AM_CONDITIONAL(WITH_GCONF, test "x$GCONFTOOL" != "xno")

LDFLAGS="-no-undefined $LDFLAGS"

AC_CONFIG_FILES([
Makefile
doc/Makefile
gsf/Makefile
gsf-gnome/Makefile
gsf-win32/Makefile
tests/Makefile
python/Makefile
thumbnailer/Makefile
po/Makefile.in
gsf/version.c
doc/version.xml
libgsf-1.pc
libgsf-1.spec
libgsf-gnome-1.pc
libgsf-gnome-1.spec
libgsf-win32-1.pc
libgsf-zip
])
AC_OUTPUT

AC_MSG_RESULT([Configured $PACKAGE $VERSION])
