dnl -*- mode: Autoconf; -*-
dnl Invoke autogen.sh to produce a configure script.
dnl configure-pre.in and src/backends/*/configure-sub.in and configure-post.in come before this part

AC_SUBST(BACKEND_CPPFLAGS)

enable_any="no"
backend_is_enabled () {
    eval echo \${enable_${1}}
}
for backend in $BACKENDS; do
    if test `backend_is_enabled $backend` = "yes"; then
       enable_any="yes"
       SYNCEVOLUTION_MODULES="$SYNCEVOLUTION_MODULES src/backends/sync${backend}.la"
    fi
done

if test "$enable_any" = "no"; then
        AC_MSG_ERROR([no backend enabled - refusing to continue: $anymissing])
fi

dnl glib initialization is done only if requested by some configure-sub.in,
dnl for not needed otherwise even if found
if test "$need_glib" = "yes"; then
        # HAVE_GLIB (aka GLIBFOUND) are a catch-all for these
        # three GNOME libs. Assume we have all three unless one of
        # the checks fails.
        GLIBFOUND=yes

        dnl check for glib - calling g_type_init() is expected on Maemo
        PKG_CHECK_MODULES(GLIB, "glib-2.0", , GLIBFOUND=no)
        # This check here is broken on Ubuntu 8.04: it calls glib-config,
        # which isn't found, but the error is not detected by configure.
        #if test "x${GLIBFOUND}" = "xno"; then
        #	PKG_CHECK_MODULES(GLIB, "glib", GLIBFOUND=yes, GLIBFOUND=no)
        #fi

        PKG_CHECK_MODULES(GTHREAD, "gthread-2.0", , GLIBFOUND=no)
        PKG_CHECK_MODULES(GOBJECT, "gobject-2.0", , GLIBFOUND=no)
        PKG_CHECK_MODULES(GIO, "gio-2.0", , GLIBFOUND=no)

        if  test "x${GLIBFOUND}" = "xyes"; then
            AC_DEFINE(HAVE_GLIB, 1, [glib found])
        else
            AC_ERROR([not all GNOME libraries found])
        fi
        BACKEND_CPPFLAGS="$BACKEND_CPPFLAGS $GLIB_CFLAGS $GTHREAD_CFLAGS $GOBJECT_CFLAGS"
fi

# Check for Qt if some backend needs it.
if test "$need_qt_modules"; then
   AT_WITH_QT([-gui $need_qt_modules],
              [$qt_config],
              [$qt_misc])
fi

# determine from where we can get a SHA-256 implementation
have_sha="no"
if test "$GLIBFOUND" = "yes"; then
   # only use glib if we need it anyway, also has to be at lease 2.16
   PKG_CHECK_MODULES(GLIB216, [glib-2.0 >= 2.16],
                     [AC_DEFINE(USE_SHA256, 1, [choose implementation of SHA-256])
                      have_sha="glib"],
                     [true])
fi
if test "$have_sha" = "no"; then
   # Fallback is Mozilla NSS. In contrast to libgcrypt it has a .pc
   # file and a simple API.
   PKG_CHECK_MODULES(NSS, "nss",
                     [AC_DEFINE(USE_SHA256, 2, [choose implementation of SHA-256])
                      have_sha="nss"],
                     [true])
fi

dnl figure out whether we link all code statically or as modules
AM_CONDITIONAL([ENABLE_MODULES], [test "$enable_shared" = "yes"])
if test "$enable_shared" = "yes"; then
        AC_DEFINE(ENABLE_MODULES, 1, [enable dynamically opening sync source backends])
fi
AC_SUBST(SYNCEVOLUTION_LDADD)

dnl CXXFLAGS gets applied to SyncEvolution and the client library.
dnl For e.g. "-Wall -Werror" this might not be such a good idea;
dnl SYNCEVOLUTION_CXXFLAGS can be used instead. It applies only
dnl to the sources in the SyncEvolution repository.
AC_SUBST(SYNCEVOLUTION_CXXFLAGS)

dnl a quick-and-dirty way to detect compilation for the iPhone
if test "$host" = "arm-apple-darwin"; then
   AC_DEFINE(IPHONE, 1, [compiling for iPhone])
   DEVICE_TYPE=iPhone
fi

dnl --enable-evolution-compatibility
if test "$enable_evolution_compatibility" = "yes"; then
   AC_DEFINE(EVOLUTION_COMPATIBILITY, 1, [avoid hard dependency on Evolution shared objects])
   # don't link against libs wrapped by eds_abi_wrapper (no longer limited to EDS alone...)
   ECAL_LIBS=
   EBOOK_LIBS=
   EPACKAGE_LIBS=
   BLUEZ_LIBS=
fi
AM_CONDITIONAL([ENABLE_EVOLUTION_COMPATIBILITY], [test "$enable_evolution_compatibility" = "yes"])
PKG_CHECK_MODULES(LIBICAL_AVAILABLE,
                  libical >= 0.43,
                  AC_DEFINE(HAVE_LIBICAL_R, 1, [have recent enough libical with _r variants]),
                  pass)

dnl --enable-developer-mode
if test "$enable_developer_mode" = "yes"; then
backendsearchdir="`pwd`/src/backends/"
else
backendsearchdir='$(libdir)'/syncevolution/backends/
fi

backenddir='$(libdir)'/syncevolution/backends
AC_SUBST(backenddir)
AC_SUBST(backendsearchdir)

# for ActiveSync backend
syncevo_backenddir='$(libdir)'/syncevolution/backends
AC_SUBST(syncevo_backenddir)

dnl This string is sent as part of the SyncML DevInf (device
dnl information) structure to the server. All SyncEvolution platforms
dnl use "SyncEvolution" as HTTP user agent and "Mod" (model), so the
dnl device type is the only way how different kinds of clients can be
dnl distinguished.
AC_DEFINE_UNQUOTED(DEVICE_TYPE, "$DEVICE_TYPE", "SyncML DevInf DevType")

AC_CHECK_HEADERS(stdarg.h valgrind/valgrind.h execinfo.h)

AC_DEFINE(SYNTHESIS, 1, "using Synthesis engine")

# fallback for lack of --with-docdir support in older automake
if test ! "$docdir"; then
   docdir = ${datadir}/doc/syncevolution
   AC_SUBST(docdir)
fi

AC_ARG_ENABLE(dbus-timeout-hack,
              AS_HELP_STRING([--enable-dbus-timeout-hack],
                             [Enables code which overrides the default timeout in dbus_connection_send_with_reply() so that the method call never times out. Needed for libecal/ebook >= 2.30, so enabled by default if either of these is enabled.]),
              [enable_dbus_timeout_hack=$enableval],
              [if test $enable_ebook = "yes" || test $enable_ecal = "yes"; then
                  enable_dbus_timeout_hack="yes"
               fi])
if test "$enable_dbus_timeout_hack" = "yes"; then
   AC_DEFINE(ENABLE_DBUS_TIMEOUT_HACK, 1, [overrides the default D-Bus timeout so that synchronous calls never time out])
fi

# Avoid hard-coding paths in backends. These names are chosen so
# that a backend can alternatively use its own top-level configure
# with PKG_CHECK_MODULES(SYNCEVOLUTION, "syncevolution") to set them.
# need absolute path, use pwd instead of relative $srcdir
SYNCEVOLUTION_CFLAGS=-I`cd $srcdir && pwd`/src
SYNCEVOLUTION_LIBS=`pwd`/src/syncevo/libsyncevolution.la
AC_SUBST(SYNCEVOLUTION_CFLAGS)
AC_SUBST(SYNCEVOLUTION_LIBS)

# invoking syncevolution binary is allowed to fail when cross-compiling
AM_CONDITIONAL([COND_CROSS_COMPILING], [test "$cross_compiling" = "yes"])

AC_CONFIG_FILES(Makefile src/dbus/interfaces/Makefile src/gdbus/Makefile src/dbus/Makefile src/dbus/glib/Makefile src/Makefile src/syncevo/Makefile src/syncevo/syncevolution.pc src/syncevo/configs/Makefile src/synthesis-includes/Makefile src/gtk-ui/Makefile po/Makefile.in test/Makefile src/dbus/glib/syncevo-dbus.pc)
AC_OUTPUT

echo
echo CONFIGURATION SUMMARY
echo "Core SyncEvolution: $enable_core"
for backend in $BACKENDS; do
    eval echo $backend: \${enable_${backend}}
done
echo "DBus service: $enable_dbus_service"
echo "GNOME keyring: $enable_gnome_keyring"
echo "UI (DBus client): $enable_gui"
echo "Bluetooth transport: $have_bluetooth"
echo "GNOME Bluetooth panel plugin: $enable_gnome_bluetooth_panel"
echo "SHA-256: $have_sha"
echo "API documentation: $enable_doc"
echo "D-Bus Timeout Hack: $enable_dbus_timeout_hack"
echo
