AC_PREREQ(2.52)

AC_INIT(diversity-daemon, 0.1, diversity@openmoko.com)
AM_INIT_AUTOMAKE([subdir-objects])
AM_MAINTAINER_MODE

AM_CONFIG_HEADER(config.h)

dnl
dnl Require programs
dnl
AC_PROG_CC
AM_PROG_CC_C_O
AC_PROG_INSTALL
AC_PROG_LIBTOOL

dnl
dnl Required headers
dnl
AC_HEADER_STDC

dnl
dnl Checks for typedefs, structures, and compiler characteristics.
dnl
AC_HEADER_TIME

dnl
dnl Checks for library functions.
dnl

dnl
dnl Make sha1.c happy on big endian systems
dnl
AC_C_BIGENDIAN

PKG_CHECK_MODULES(GLIB, glib-2.0 >= 2.14 gmodule-2.0 gthread-2.0 gobject-2.0)
AC_PATH_PROG(GLIB_GENMARSHAL, glib-genmarshal)
if test -z "$GLIB_GENMARSHAL"; then
	AC_MSG_ERROR([glib-genmarshal did not found.])
fi

AC_ARG_ENABLE(session-bus,
	      AC_HELP_STRING([--enable-session-bus], [Use the session bus instead of system bus]),
	      [], enable_session_bus=no)

if test "x$enable_session_bus" = "xyes"; then
	AC_DEFINE(DIVERSITY_DBUS_BUS, DBUS_BUS_SESSION, [Use the session bus])
	dbus_services_dir="services"
else
	AC_DEFINE(DIVERSITY_DBUS_BUS, DBUS_BUS_SYSTEM, [Use the system bus])
	dbus_services_dir="system-services"
fi
AM_CONDITIONAL(USE_SYSTEM_BUS, test "x$enable_session_bus" != "xyes")

AC_ARG_WITH(dbus-sys, AC_HELP_STRING([--with-dbus-sys=DIR], [where D-BUS system.d directory is]))
AC_ARG_WITH(dbus-services, AC_HELP_STRING([--with-dbus-services=DIR], [where D-BUS services directory is]))

if ! test -z "$with_dbus_sys" ; then
    DBUS_SYS_DIR="$with_dbus_sys"
else
    DBUS_SYS_DIR="${sysconfdir}/dbus-1/system.d"
fi

if ! test -z "$with_dbus_services" ; then
    DBUS_SERVICES_DIR="$with_dbus_services"
else
    DBUS_SERVICES_DIR="${datadir}/dbus-1/${dbus_services_dir}"
fi

AC_SUBST(DBUS_SYS_DIR)
AC_SUBST(DBUS_SERVICES_DIR)

AC_ARG_WITH(diversity-user, AC_HELP_STRING([--with-diversity-user=DIR], [user for running diversity-daemon (root)]))
if ! test -z "$with_diversity_user" ; then
    DIVERSITY_USER="$with_diversity_user"
else
    DIVERSITY_USER="root"
fi

AC_SUBST(DIVERSITY_USER)

XSLTPROC=
enable_doc=no
AC_CHECK_PROGS([XSLTPROC], [xsltproc])
if test -n "$XSLTPROC"; then
	enable_doc=yes
fi

AM_CONDITIONAL(HAVE_DOC, test "x$enable_doc" = "xyes")

# backends
all_backends="connman control ebook geoclue gypsy iwlib nm nmea osm phonekit qtopia rae xmpp"
enabled_backends=""

AC_ARG_ENABLE(connman,
	      AC_HELP_STRING([--enable-connman], [enable Connman backend]),
	      [], enable_connman=no)
AC_ARG_ENABLE(control,
	      AC_HELP_STRING([--disable-control], [disable Control backend]),
	      [], enable_control=yes)
AC_ARG_ENABLE(ebook,
	      AC_HELP_STRING([--disable-ebook], [disable EBook backend]),
	      [], enable_ebook=yes)
AC_ARG_ENABLE(nmea,
	      AC_HELP_STRING([--disable-nmea], [disable NMEA backend]),
	      [], enable_nmea=yes)
AC_ARG_ENABLE(geoclue,
	      AC_HELP_STRING([--disable-geoclue], [disable GeoClue backend]),
	      [], enable_geoclue=yes)
AC_ARG_ENABLE(gypsy,
	      AC_HELP_STRING([--disable-gypsy], [disable Gypsy backend]),
	      [], enable_gypsy=yes)
AC_ARG_ENABLE(iwlib,
	      AC_HELP_STRING([--enable-iwlib], [enable iwlib backend]),
	      [], enable_iwlib=no)
AC_ARG_ENABLE(nm,
	      AC_HELP_STRING([--disable-nm], [disable NetworkManager backend]),
	      [], enable_nm=yes)
AC_ARG_ENABLE(osm,
	      AC_HELP_STRING([--disable-osm], [disable OpenStreetMap backend]),
	      [], enable_osm=yes)
AC_ARG_ENABLE(phonekit,
	      AC_HELP_STRING([--disable-phonekit], [disable PhoneKit backend]),
	      [], enable_phonekit=yes)
AC_ARG_ENABLE(qtopia,
	      AC_HELP_STRING([--disable-qtopia], [disable Qtopia backend]),
	      [], enable_qtopia=yes)
AC_ARG_ENABLE(rae,
	      AC_HELP_STRING([--enable-rae], [enable Rae backend]),
	      [], enable_rae=no)
AC_ARG_ENABLE(xmpp,
	      AC_HELP_STRING([--disable-xmpp], [disable XMPP backend]),
	      [], enable_xmpp=yes)

PKG_CHECK_MODULES(DBUS, dbus-glib-1 >= 0.60, [have_dbus=yes], [have_dbus=no])
AC_PATH_PROG(DBUS_BINDING_TOOL, dbus-binding-tool)
if test -z "$DBUS_BINDING_TOOL"; then
	have_dbus=no
fi

if test "x$have_dbus" != "xyes"; then
	enable_connman=no
	enable_control=no
	enable_geoclue=no
	enable_gypsy=no
	enable_nm=no
	enable_phonekit=no
	enable_qtopia=no
fi

if test "x$enable_connman" = "xyes"; then
	enable_iwlib="no (disabled by connman backend)"
	enable_nm="no (disabled by connman backend)"
fi
if test "x$enable_connman" = "xyes"; then
	AC_DEFINE(HAVE_CONNMAN, 1, [Compile Connman backend])
	enabled_backends="$enabled_backends connman"
fi
AM_CONDITIONAL(HAVE_CONNMAN, test "x$enable_connman" = "xyes")

if test "x$enable_control" = "xyes"; then
	AC_DEFINE(HAVE_CONTROL, 1, [Compile Control backend])
	enabled_backends="$enabled_backends control"
fi
AM_CONDITIONAL(HAVE_CONTROL, test "x$enable_control" = "xyes")

if test "x$enable_ebook" = "xyes"; then
	PKG_CHECK_MODULES(EBOOK, libebook-1.2,, [enable_ebook=no])
	PKG_CHECK_MODULES(JANA, libjana libjana-ecal,, [enable_ebook=no])
fi
if test "x$enable_ebook" = "xyes"; then
	AC_DEFINE(HAVE_EBOOK, 1, [Compile EBook backend])
	enabled_backends="$enabled_backends ebook"
fi
AM_CONDITIONAL(HAVE_EBOOK, test "x$enable_ebook" = "xyes")

if test "x$enable_nmea" = "xyes"; then
	AC_DEFINE(HAVE_NMEA, 1, [Compile NMEA backend])
	enabled_backends="$enabled_backends nmea"
	enable_geoclue="no (disabled by NMEA backend)"
	enable_gypsy="no (disabled by NMEA backend)"
fi
AM_CONDITIONAL(HAVE_NMEA, test "x$enable_nmea" = "xyes")

if test "x$enable_geoclue" = "xyes"; then
	PKG_CHECK_MODULES(GEOCLUE, geoclue,, [enable_geoclue=no])
fi
if test "x$enable_geoclue" = "xyes"; then
	AC_DEFINE(HAVE_GEOCLUE, 1, [Compile GeoClue backend])
	enabled_backends="$enabled_backends geoclue"
fi
AM_CONDITIONAL(HAVE_GEOCLUE, test "x$enable_geoclue" = "xyes")

if test "x$enable_geoclue" == "xyes"; then
	enable_gypsy="no (disabled by GeoClue backend)"
fi

if test "x$enable_gypsy" = "xyes"; then
	PKG_CHECK_MODULES(GYPSY, gypsy,, [enable_gypsy=no])
fi
if test "x$enable_gypsy" = "xyes"; then
	AC_DEFINE(HAVE_GYPSY, 1, [Compile Gypsy backend])
	enabled_backends="$enabled_backends gypsy"
fi
AM_CONDITIONAL(HAVE_GYPSY, test "x$enable_gypsy" = "xyes")

if test "x$enable_iwlib" = "xyes"; then
	enable_nm="no (disabled by iwlib backend)"
fi
if test "x$enable_iwlib" = "xyes"; then
	AC_DEFINE(HAVE_IWLIB, 1, [Compile iwlib backend])
	enabled_backends="$enabled_backends iwlib"
fi
AM_CONDITIONAL(HAVE_IWLIB, test "x$enable_iwlib" = "xyes")

if test "x$enable_nm" = "xyes"; then
	PKG_CHECK_MODULES(NM, NetworkManager = 0.6.5,, [enable_nm=no])
fi
if test "x$enable_nm" = "xyes"; then
	AC_DEFINE(HAVE_NM, 1, [Compile NetworkManager backend])
	enabled_backends="$enabled_backends nm"
fi
AM_CONDITIONAL(HAVE_NM, test "x$enable_nm" = "xyes")

if test "x$enable_osm" = "xyes"; then
	PKG_CHECK_MODULES(CURL, libcurl,, [enable_osm=no])
fi
if test "x$enable_osm" = "xyes"; then
	AC_DEFINE(HAVE_OSM, 1, [Compile OpenStreetMap backend])
	enabled_backends="$enabled_backends osm"
fi
AM_CONDITIONAL(HAVE_OSM, test "x$enable_osm" = "xyes")

if test "x$enable_phonekit" = "xyes"; then
	AC_DEFINE(HAVE_PHONEKIT, 1, [Compile PhoneKit backend])
	enabled_backends="$enabled_backends phonekit"
fi
AM_CONDITIONAL(HAVE_PHONEKIT, test "x$enable_phonekit" = "xyes")

if test "x$enable_qtopia" = "xyes"; then
	AC_DEFINE(HAVE_QTOPIA, 1, [Compile Qtopia backend])
	enabled_backends="$enabled_backends qtopia"
fi
AM_CONDITIONAL(HAVE_QTOPIA, test "x$enable_qtopia" = "xyes")

if test "x$enable_rae" = "xyes"; then
	PKG_CHECK_MODULES(SOUP, libsoup-2.4,, [enable_rae=no])
fi
if test "x$enable_rae" = "xyes"; then
	AC_DEFINE(HAVE_RAE, 1, [Compile Rae backend])
	enabled_backends="$enabled_backends rae"
fi
AM_CONDITIONAL(HAVE_RAE, test "x$enable_rae" = "xyes")

if test "x$enable_xmpp" = "xyes"; then
	PKG_CHECK_MODULES(LM, loudmouth-1.0,, [enable_xmpp=no])
fi
if test "x$enable_xmpp" = "xyes"; then
	AC_DEFINE(HAVE_XMPP, 1, [Compile XMPP backend])
	enabled_backends="$enabled_backends xmpp"
fi
AM_CONDITIONAL(HAVE_XMPP, test "x$enable_xmpp" = "xyes")

BUILTIN_BACKENDS=
AC_SUBST(BUILTIN_BACKENDS)

for backend in $enabled_backends; do
	backenddir=diversity-${backend}-backend
	backendla=libdiversity-${backend}-backend.la
	backendpath="\$(top_builddir)/$backenddir/$backendla"

	BUILTIN_BACKENDS="$BUILTIN_BACKENDS $backendpath"
done

AC_CONFIG_COMMANDS([src/builtin-backends.h],
[
cat > src/builtin-backends.h <<EOH
/* This file is automatically generated. Do not edit. */

#ifndef _BUILTIN_BACKENDS_H_
#define _BUILTIN_BACKENDS_H_

#include <diversity-module.h>

EOH

for backend in $enabled_backends; do
  cat >> src/builtin-backends.h <<EOH
extern DiversityModuleHandle _diversity_module_handle_${backend};
EOH
done 

cat >> src/builtin-backends.h <<EOH

extern DiversityModuleHandle * const _diversity_builtin_backends@<:@@:>@;

#endif /* _BUILTIN_BACKENDS_H_ */
EOH
],[
enabled_backends="$enabled_backends"
])

AC_CONFIG_COMMANDS([src/builtin-backends.c],
[
cat > src/builtin-backends.c <<EOH
/* This file is automatically generated. Do not edit. */

#include "builtin-backends.h"

DiversityModuleHandle * const _diversity_builtin_backends@<:@@:>@ = {
EOH

for backend in $enabled_backends; do
  cat >> src/builtin-backends.c <<EOH
	&_diversity_module_handle_${backend},
EOH
done 

cat >> src/builtin-backends.c <<EOH
	NULL
};
EOH
],[
enabled_backends="$enabled_backends"
])

CFLAGS="-Wall $CFLAGS"

AC_ARG_ENABLE(more-warnings,
AC_HELP_STRING([--enable-more-warnings], [Maximum compiler warnings]),
set_more_warnings="$enableval",[
if test -d "$srcdir/{arch}" || test -d "$srcdir/CVS"; then
	set_more_warnings=yes
else
	set_more_warnings=no
fi
])
AC_MSG_CHECKING(for more warnings, including -Werror)
if test "$GCC" = "yes" -a "$set_more_warnings" != "no"; then
	AC_MSG_RESULT(yes)
	CFLAGS="-Werror -std=gnu89 $CFLAGS"

	for option in -Wshadow -Wmissing-declarations -Wmissing-prototypes \
		      -Wdeclaration-after-statement -Wstrict-prototypes \
		      -Wfloat-equal -Wno-unused-parameter -Wno-sign-compare; do
		SAVE_CFLAGS="$CFLAGS"
		CFLAGS="$CFLAGS $option"
		AC_MSG_CHECKING([whether gcc understands $option])
		AC_TRY_COMPILE([], [],
			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
else
	AC_MSG_RESULT(no)
fi

AC_OUTPUT([
Makefile
data/Makefile
data/org.openmoko.Diversity.conf
docs/Makefile
include/Makefile
introspection/Makefile
diversity-connman-backend/Makefile
diversity-control-backend/Makefile
diversity-ebook-backend/Makefile
diversity-geoclue-backend/Makefile
diversity-gypsy-backend/Makefile
diversity-nm-backend/Makefile
diversity-nmea-backend/Makefile
diversity-iwlib-backend/Makefile
diversity-osm-backend/Makefile
diversity-phonekit-backend/Makefile
diversity-qtopia-backend/Makefile
diversity-rae-backend/Makefile
diversity-xmpp-backend/Makefile
src/Makefile
tests/Makefile
])

echo
echo "Documentations......................... : $enable_doc"
echo
echo "Connman backend........................ : $enable_connman"
echo "Control backend........................ : $enable_control"
echo "EBook backend.......................... : $enable_ebook"
echo "GeoClue backend........................ : $enable_geoclue"
echo "Gypsy backend.......................... : $enable_gypsy"
echo "iwlib backend.......................... : $enable_iwlib"
echo "NetworkManager backend................. : $enable_nm"
echo "NMEA backend........................... : $enable_nmea"
echo "OpenStreetMap backend.................. : $enable_osm"
echo "PhoneKit backend....................... : $enable_phonekit"
echo "Qtopia backend......................... : $enable_qtopia"
echo "Rae backend............................ : $enable_rae"
echo "XMPP backend........................... : $enable_xmpp"
echo
