AC_PREREQ(2.60)
AC_INIT([MateConf], [1.2.1], [https://github.com/mate-desktop/mate-conf/issues], [MateConf], [http://www.mate-desktop.org])

AC_CONFIG_SRCDIR([mateconf/mateconf.h])
AC_CONFIG_HEADERS([config.h])

AM_INIT_AUTOMAKE([1.9 no-dist-gzip dist-bzip2 -Wall])

m4_ifdef([AM_SILENT_RULES], [AM_SILENT_RULES([no])])

AM_MAINTAINER_MODE

AC_CANONICAL_HOST
case "$host" in
  *-*-mingw*)
    os_win32=yes
    ;;
  *)
    os_win32=no
    ;;
esac

AM_CONDITIONAL(OS_WIN32, [test "x$os_win32" = "xyes"])

if test "x$os_win32" = "xyes"; then
  if test "x$enable_static" = "xyes" -o "x$enable_static" = "x"; then
    AC_MSG_WARN([Disabling static library build, must build as DLL on Windows.])
    enable_static=no
  fi
  if test "x$enable_shared" = "xno"; then
    AC_MSG_WARN([Enabling shared library build, must build as DLL on Windows.])
  fi
  enable_shared=yes
fi

AC_PROG_CC
AC_PROG_CXX
AC_ISC_POSIX
AM_PROG_CC_STDC
AC_HEADER_STDC
AC_LIBTOOL_WIN32_DLL
AM_PROG_LIBTOOL
AC_PATH_PROG(GLIB_GENMARSHAL, glib-genmarshal)

changequote(,)dnl
if test "x$GCC" = "xyes"; then
  case " $CFLAGS " in
    *[\ \	]-Wall[\ \	]*) ;;
    *) CFLAGS="$CFLAGS -Wall" ;;
  esac

  if test "x$enable_ansi" = "xyes"; then
    case " $CFLAGS " in
      *[\ \	]-ansi[\ \	]*) ;;
      *) CFLAGS="$CFLAGS -ansi" ;;
    esac

    case " $CFLAGS " in
      *[\ \	]-pedantic[\ \	]*) ;;
      *) CFLAGS="$CFLAGS -pedantic" ;;
    esac
  fi
fi
changequote([,])dnl

dnl used to rename everything and support simultaneous installs.
dnl not incremented for bugfix or unstable releases.
MAJOR_VERSION=2
AC_SUBST(MAJOR_VERSION)

GETTEXT_PACKAGE=mate-conf
AC_SUBST(GETTEXT_PACKAGE)
AC_DEFINE_UNQUOTED(GETTEXT_PACKAGE,"$GETTEXT_PACKAGE", [Gettext package])

dnl libtool versioning for libmateconf

dnl increment if the interface has additions, changes, removals.
MATECONF_CURRENT=5

dnl increment any time the source changes; set to
dnl  0 if you increment CURRENT
MATECONF_REVISION=5

dnl increment if any interfaces have been added; set to 0
dnl  if any interfaces have been removed. removal has
dnl  precedence over adding, so set to 0 if both happened.
MATECONF_AGE=1

AC_SUBST(MATECONF_CURRENT)
AC_SUBST(MATECONF_REVISION)
AC_SUBST(MATECONF_AGE)

# find the actual value for $prefix that we'll end up with
REAL_PREFIX=
if test "x$prefix" = "xNONE"; then
  REAL_PREFIX=$ac_default_prefix
else
  REAL_PREFIX=$prefix
fi

AC_ARG_WITH(sysconfsubdir,
  AS_HELP_STRING([--with-sysconfsubdir],
    [directory name used under sysconfdir @<:@default=mateconf@:>@]),
  , with_sysconfsubdir=mateconf)
AC_SUBST(with_sysconfsubdir)

if test "x${with_sysconfsubdir}" != "x"; then
  sysmateconfdir='${sysconfdir}/${with_sysconfsubdir}'
else
  sysmateconfdir='${sysconfdir}'
fi
mateconfdefaultpath="$sysconfdir/$with_sysconfsubdir/$MAJOR_VERSION/path"
AC_SUBST(sysmateconfdir)
AC_SUBST(mateconfdefaultpath)

dnl Save flags to aclocal
ACLOCAL="$ACLOCAL $ACLOCAL_FLAGS"

dnl Default to debug spew in unstable branch
AC_ARG_ENABLE(debug,
  AS_HELP_STRING([--enable-debug],
    [Compile with debug checks. @<:@no/yes/minimum, default=minimum@:>@]),
  , enable_debug=minimum)

if test "x$enable_debug" = "xyes"; then
  CFLAGS="$CFLAGS -DMATECONF_ENABLE_DEBUG=1"
  AC_MSG_NOTICE([Will build with debugging spew and checks])
else
  if test "x$enable_debug" = "xno"; then
    CFLAGS="$CFLAGS -DG_DISABLE_CHECKS=1 -DG_DISABLE_ASSERT=1"
    AC_MSG_NOTICE([Will build without *any* debugging code])
  else
    AC_MSG_NOTICE([Will build with debug checks but no debug spew])
  fi
fi

GTK_DOC_CHECK([1.0])

AC_ARG_ENABLE(gtk,
  AS_HELP_STRING([--enable-gtk],
    [Enable GTK+ support (for mateconf-sanity-check) @<:@default=auto@:>@]),
  , enable_gtk=auto)


dnl ---------------------------------------------------------------------------
dnl GTK library version
dnl ---------------------------------------------------------------------------
GTK_API_VERSION=2.0
GTK_REQUIRED=2.14.0

AC_MSG_CHECKING([which gtk+ version to compile against])
AC_ARG_WITH([gtk],
  [AS_HELP_STRING([--with-gtk=2.0|3.0],[which gtk+ version to compile against (default: 2.0)])],
  [case "$with_gtk" in
     2.0|3.0) ;;
     *) AC_MSG_ERROR([invalid gtk version specified]) ;;
   esac],
  [with_gtk=2.0])
AC_MSG_RESULT([$with_gtk])

case "$with_gtk" in
  2.0) GTK_API_VERSION=2.0
       GTK_REQUIRED=2.14.0
       ;;
  3.0) GTK_API_VERSION=3.0
       GTK_REQUIRED=3.0.0
       ;;
esac






PKGCONFIG_MODULES='glib-2.0 > 2.14.0 gio-2.0 >= 2.25.9 gthread-2.0 gmodule-2.0 >= 2.7.0 gobject-2.0 >= 2.7.0 MateCORBA-2.0'
PKGCONFIG_MODULES_WITH_XML="$PKGCONFIG_MODULES libxml-2.0"
PKGCONFIG_MODULES_WITH_GTK=" $PKGCONFIG_MODULES gtk+-$GTK_API_VERSION >= $GTK_REQUIRED"
PKGCONFIG_MODULES_WITH_XML_AND_GTK=" $PKGCONFIG_MODULES gtk+-$GTK_API_VERSION libxml-2.0"
PKGCONFIG_MODULES_DBUS=" dbus-1 >= 1.0.0 dbus-glib-1 >= 0.74"

PKG_CHECK_MODULES(DEPENDENT, $PKGCONFIG_MODULES)
PKG_CHECK_MODULES(DEPENDENT_WITH_XML, $PKGCONFIG_MODULES_WITH_XML)

if test "x$enable_gtk" != "xno"; then
  PKG_CHECK_MODULES(DEPENDENT_WITH_GTK, $PKGCONFIG_MODULES_WITH_GTK, HAVE_GTK=yes, HAVE_GTK=no)
  PKG_CHECK_MODULES(DEPENDENT_WITH_XML_AND_GTK, $PKGCONFIG_MODULES_WITH_XML_AND_GTK, , DEPENDENT_WITH_XML_AND_GTK_CFLAGS=$DEPENDENT_WITH_XML_CFLAGS DEPENDENT_WITH_XML_AND_GTK_LIBS=$DEPENDENT_WITH_XML_LIBS)

  if test "x$enable_gtk" = "xyes" && test "x$HAVE_GTK" = "xno"; then
    AC_MSG_ERROR([[
*** Could not find GTK+ $GTK_REQUIRED or greater.
*** Go to http://www.gtk.org/ to get it.]])
  fi
else
  HAVE_GTK=no
  DEPENDENT_WITH_XML_AND_GTK_CFLAGS=$DEPENDENT_WITH_XML_CFLAGS
  DEPENDENT_WITH_XML_AND_GTK_LIBS=$DEPENDENT_WITH_XML_LIBS
fi

PKG_CHECK_MODULES([DEPENDENT_DBUS],[$PKGCONFIG_MODULES_DBUS])

AM_CONDITIONAL(GTK, [test "x$HAVE_GTK" != "xno"])

AC_SUBST(DEPENDENT_LIBS)
AC_SUBST(DEPENDENT_CFLAGS)
AC_SUBST(DEPENDENT_WITH_XML_LIBS)
AC_SUBST(DEPENDENT_WITH_XML_CFLAGS)
AC_SUBST(DEPENDENT_WITH_GTK_LIBS)
AC_SUBST(DEPENDENT_WITH_GTK_CFLAGS)
AC_SUBST(DEPENDENT_WITH_XML_AND_GTK_LIBS)
AC_SUBST(DEPENDENT_WITH_XML_AND_GTK_CFLAGS)
AC_SUBST([DEPENDENT_DBUS_CFLAGS])
AC_SUBST([DEPENDENT_DBUS_LIBS])

HAVE_POLKIT=no
AC_ARG_ENABLE(defaults_service,
  AS_HELP_STRING([--enable-defaults-service],
    [build the defaults DBus service @<:@default=auto@:>@]),
  , enable_defaults_service=auto)

if test "x$enable_defaults_service" != "xno" ; then
  PKG_CHECK_MODULES(DEFAULTS, glib-2.0 gthread-2.0 gobject-2.0 dbus-1 dbus-glib-1 polkit-gobject-1, HAVE_POLKIT=yes, HAVE_POLKIT=no)
  if test "x$HAVE_POLKIT" = "xno"; then
    if test "x$enable_defaults_service" = "xyes" ; then
      AC_MSG_ERROR([[
*** Could not find PolicyKit.]])
    else
      enable_defaults_service=no
    fi
  else
    enable_defaults_service=yes
  fi
fi

if test "x$enable_defaults_service" != "xno" ; then
  AC_DEFINE(ENABLE_DEFAULTS_SERVICE, 1, [enable defaults DBus service])
fi

AM_CONDITIONAL(ENABLE_DEFAULTS_SERVICE, [test "x$enable_defaults_service" != "xno"])

AC_ARG_ENABLE(gsettings_backend,
  AS_HELP_STRING([--enable-gsettings-backend],
    [build the gsettings backend @<:@default=auto@:>@]),
  , enable_gsettings_backend=auto)

if test "x$enable_gsettings_backend" != "xno" ; then
  PKG_CHECK_MODULES(GSETTINGS, gio-2.0 >= 2.25.0, HAVE_GSETTINGS=yes, HAVE_GSETTINGS=no)
  if test "x$HAVE_GSETTINGS" = "xno"; then
    if test "x$enable_gsettings_backend" = "xyes" ; then
      AC_MSG_ERROR([[
*** Could not find GIO 2.25.0.]])
    fi
  else
    enable_gsettings_backend=yes
    AC_SUBST(GIO_MODULE_DIR,
             `pkg-config --variable giomoduledir gio-2.0`)
    AC_PATH_PROG(GIO_QUERYMODULES, gio-querymodules, no)
  fi
fi

if test "x$enable_gsettings_backend" != "xno" ; then
  AC_DEFINE(ENABLE_GSETTINGS_BACKEND, 1, [enable defaults DBus service])
fi

AM_CONDITIONAL(ENABLE_GSETTINGS_BACKEND, [test "x$enable_gsettings_backend" = "xyes"])

test x$MATECORBA_IDL = x && MATECORBA_IDL="`$PKG_CONFIG --variable=matecorba_idl MateCORBA-2.0`"
AC_SUBST(MATECORBA_IDL)

AC_CHECK_HEADER(pthread.h, have_pthreads=yes)
AM_CONDITIONAL(PTHREADS, [test -n "$have_pthreads"])

AC_CHECK_HEADERS(syslog.h sys/wait.h)

AC_CHECK_FUNCS(getuid sigaction fsync fchmod fdwalk)

dnl **************************************************
dnl LDAP support.
dnl **************************************************

AC_ARG_WITH(openldap,
  AS_HELP_STRING([--with-openldap],
    [Enable LDAP backend @<:@default=auto@:>@]),
  , with_openldap=auto)

LDAP_LIBS=

if test "x$with_openldap" != "xno"; then
  AC_CHECK_HEADER(ldap.h, [
    AC_CHECK_LIB(ldap, ldap_init, AC_CHECK_LIB(lber, ber_free, [LDAP_LIBS="-lldap -llber"]))
  ])

  if test "x$os_win32" != "xyes"; then
    SAVE_CFLAGS="$CFLAGS"
    SAVE_LIBS="$LIBS"
    LDAP_CFLAGS="-DLDAP_DEPRECATED"
    CFLAGS="$CFLAGS $LDAP_CFLAGS"
    LIBS="$LIBS $LDAP_LIBS"
    AC_CHECK_FUNCS(ldap_ntlm_bind)
    CFLAGS="$SAVE_CFLAGS"
    LIBS="$SAVE_LIBS"

  else # Win32
    LDAP_CFLAGS="-DLDAP_DEPRECATED"
    LDAP_LIBS="-lwldap32"
    AC_SUBST(LDAP_CFLAGS)
    AC_SUBST(LDAP_LIBS)
    AC_DEFINE(HAVE_LDAP,1,[Define if you have LDAP support])
    msg_ldap="yes"

  fi # Win32

  if test -z "$LDAP_LIBS"; then
    if test "x$with_openldap" = "xyes"; then
      AC_MSG_ERROR([*** LDAP support explicitely requested but not found ***])
    else
      AC_MSG_WARN([*** LDAP support not found ***])
    fi
  fi

  have_ldap=yes
else
  have_ldap=no
fi
AC_SUBST(LDAP_CFLAGS)
AC_SUBST(LDAP_LIBS)
AM_CONDITIONAL(LDAP_SUPPORT, [test -n "$LDAP_LIBS"])

IT_PROG_INTLTOOL([0.35.0])

AM_GLIB_GNU_GETTEXT

AC_CHECK_FUNCS(bind_textdomain_codeset)

GOBJECT_INTROSPECTION_CHECK([0.9.5])

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

## Just for debugging purposes
absolute_top_srcdir=`pwd`
AC_SUBST(absolute_top_srcdir)

AC_CONFIG_FILES([
Makefile
mateconf-2.m4
mateconf-zip
mateconf/Makefile
backends/Makefile
po/Makefile.in
doc/Makefile
doc/mateconf/Makefile
examples/Makefile
tests/Makefile
defaults/Makefile
gsettings/Makefile
mateconf-2.0.pc
])

AC_OUTPUT

echo "
    sysconfsubdir:  ${with_sysconfsubdir}

    Gtk+:           ${HAVE_GTK}
    Gtk+ version:   ${GTK_API_VERSION}
    ldap:           ${have_ldap}
    policykit:      ${HAVE_POLKIT}
    gsettings:      ${HAVE_GSETTINGS}
    introspection:  ${found_introspection}

"
