AC_INIT([Dovecot Sieve], [0.1.13], [dovecot@dovecot.org], [dovecot-2.0-sieve])
AC_CONFIG_SRCDIR([src])

# Autoheader is not needed and does more harm than good for this package. However, it is 
# tightly integrated in autoconf/automake and therefore it is difficult not to use it. As 
# a workaround we give autoheader a dummy config header to chew on and we handle the 
# real config header ourselves.
AC_CONFIG_HEADERS([dummy-config.h dsieve-config.h])

AC_DEFINE_UNQUOTED(SIEVE_NAME, "$PACKAGE_NAME", 
	[Define to the full name of this Sieve implementation.])
AC_DEFINE_UNQUOTED(SIEVE_VERSION, "$PACKAGE_VERSION", 
	[Define to the version of this Sieve implementation.])

AM_INIT_AUTOMAKE([no-define foreign])

AM_MAINTAINER_MODE

AC_PROG_CC
AC_PROG_CPP
AC_PROG_LIBTOOL

AC_ARG_WITH(dovecot,
[AC_HELP_STRING([--with-dovecot=DIR], [Dovecot base directory [../dovecot]])],
	dovecotdir="$withval",
	dovecotdir=../dovecot-2.0
)
old=`pwd`
cd $dovecotdir
dovecotdir=`pwd`
cd $old
AC_SUBST(dovecotdir)

if ! test -f "$dovecotdir/dovecot-config"; then
  echo
  echo "dovecot-config not found from $dovecotdir, use --with-dovecot=PATH"
  echo "to give path to compiled Dovecot sources or to a directory with the"
  echo "installed dovecot-config file."
  AC_MSG_ERROR([dovecot-config not found])
fi

if test -d "$dovecotdir/src"; then
  # compiling against sources
  have_dovecot_sources=yes
else
  # compiling against installed headers
  have_dovecot_sources=no
fi

# Extensions under development
#

AC_ARG_WITH(unfinished-features,
[AC_HELP_STRING([--with-unfinished-features], 
	[Build unfinished new features/extensions [default=no]])],
        if test x$withval = xno || test x$withval = xauto; then
                want_unfinished_features=$withval
        else
                want_unfinished_features=yes
        fi,
        want_unfinished_features=no)
AM_CONDITIONAL(BUILD_UNFINISHED, test "$want_unfinished_features" = "yes")

if test "$want_unfinished_features" = "yes"; then
	AC_DEFINE(HAVE_SIEVE_UNFINISHED,,
		[Define to build Sieve unfinished features/extensions.])
fi

#
#

AC_ARG_ENABLE(header-install,
[  --enable-header-install Install development headers],
    if test x$enableval = xno; then
        want_headers=no
    else
        want_headers=yes
    fi,
    want_headers=no)
AM_CONDITIONAL(INSTALL_HEADERS, test "$want_headers" = "yes")

AC_ARG_ENABLE(valgrind,
[AC_HELP_STRING([--enable-valgrind], [Enable Valgrind memory leak checks in testsuite [default=no]])],
    if test x$enableval = xno || test x$enableval = xauto; then
        want_valgrind=$enableval
    else
        want_valgrind=yes
    fi,
    want_valgrind=no)
AM_CONDITIONAL(TESTSUITE_VALGRIND, test "$want_valgrind" = "yes")

dnl replace relative ../ paths in the file with full paths
eval `cat $dovecotdir/dovecot-config|sed 's,\$(top_builddir)/,$dovecotdir/,g'`

if test $have_dovecot_sources = yes; then
  dovecot_incdir="$dovecotdir"
  dovecot_libdir="$dovecotdir"

  dovecot_pkgincludedir='$(includedir)/dovecot'
  dovecot_pkglibdir='$(libdir)/dovecot'

  LIBDOVECOT='$(dovecot_libdir)/src/lib-dovecot/libdovecot.la'
  LIBDOVECOT_STORAGE='$(dovecot_libdir)/src/lib-storage/libdovecot-storage.la'

  LIBDOVECOT_INCLUDE='-I$(dovecot_incdir) -I$(dovecot_incdir)/src/lib -I$(dovecot_incdir)/src/lib-dict -I$(dovecot_incdir)/src/lib-mail -I$(dovecot_incdir)/src/lib-storage -I$(dovecot_incdir)/src/lib-imap -I$(dovecot_incdir)/src/lib-master'
  LIBDOVECOT_LDA_INCLUDE='-I$(dovecot_incdir)/src/lib-lda -I$(dovecot_incdir)/src/lda'
  LIBDOVECOT_STORAGE_INCLUDE='-I$(dovecot_incdir)/src/lib-settings  -I$(dovecot_incdir)/src/lib-index -I$(dovecot_incdir)/src/lib-storage/index -I$(dovecot_incdir)/src/lib-storage/index/raw'
else
  dovecot_libdir="$dovecotdir"
  
  dovecot_pkgincludedir='$(dovecot_incdir)'
  dovecot_pkglibdir='$(dovecotdir)'

  LIBDOVECOT='$(dovecot_libdir)/libdovecot.la'
  LIBDOVECOT_STORAGE='$(dovecot_libdir)/libdovecot-storage.la'

  LIBDOVECOT_INCLUDE='-I$(dovecot_incdir)'
  LIBDOVECOT_LDA_INCLUDE=''
  LIBDOVECOT_STORAGE_INCLUDE=''
fi

dovecot_pkglibexecdir='$(libexecdir)/dovecot'
dovecot_docdir='$(datadir)/doc/dovecot'

AC_SUBST(LIBICONV)
AC_SUBST(RAND_LIBS)
AC_SUBST(MODULE_LIBS)
AC_SUBST(dovecot_incdir)
AC_SUBST(dovecot_libdir)
AC_SUBST(moduledir)
AC_SUBST(dovecot_pkgincludedir)
AC_SUBST(dovecot_pkglibexecdir)
AC_SUBST(dovecot_pkglibdir)
AC_SUBST(dovecot_docdir)

AC_SUBST(LIBDOVECOT)
AC_SUBST(LIBDOVECOT_STORAGE)
AC_SUBST(LIBDOVECOT_INCLUDE)
AC_SUBST(LIBDOVECOT_LDA_INCLUDE)
AC_SUBST(LIBDOVECOT_STORAGE_INCLUDE)

AC_CONFIG_FILES([
Makefile
doc/Makefile
doc/man/Makefile
doc/example-config/Makefile
doc/example-config/conf.d/Makefile
src/Makefile
src/lib-sieve/Makefile
src/lib-sieve/plugins/Makefile
src/lib-sieve/plugins/vacation/Makefile
src/lib-sieve/plugins/subaddress/Makefile
src/lib-sieve/plugins/comparator-i-ascii-numeric/Makefile
src/lib-sieve/plugins/relational/Makefile
src/lib-sieve/plugins/regex/Makefile
src/lib-sieve/plugins/imap4flags/Makefile
src/lib-sieve/plugins/copy/Makefile
src/lib-sieve/plugins/include/Makefile
src/lib-sieve/plugins/body/Makefile
src/lib-sieve/plugins/variables/Makefile
src/lib-sieve/plugins/enotify/Makefile
src/lib-sieve/plugins/notify/Makefile
src/lib-sieve/plugins/environment/Makefile
src/lib-sieve/plugins/mailbox/Makefile
src/lib-sieve/plugins/date/Makefile
src/lib-sieve/plugins/spamvirustest/Makefile
src/lib-sieve-tool/Makefile
src/plugins/Makefile
src/plugins/lda-sieve/Makefile
src/sieve-tools/Makefile
src/sieve-tools/debug/Makefile
src/testsuite/Makefile
stamp.h])

AC_OUTPUT

echo "NOTE: This is the UNSTABLE development branch."