diff --git a/.hgignore b/.hgignore
index 3e6b042e13fd1b835d62cdb99789516b6a3f7930..681d58ac8a179505f8038afe2fefd555f8ea7739 100644
--- a/.hgignore
+++ b/.hgignore
@@ -33,6 +33,11 @@ dovecot-config.in
 ChangeLog
 Makefile
 Makefile.in
+m4/libtool.m4
+m4/ltoptions.m4
+m4/ltsugar.m4
+m4/ltversion.m4
+m4/lt~obsolete.m4
 
 *~
 *.o
diff --git a/Makefile.am b/Makefile.am
index 59d1fb8d39712f7062235080a1a166472aede4b8..f8fea5ac31dbcc184af96f582ae2b68a08041f80 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1,5 +1,7 @@
 SUBDIRS = src doc
 
+ACLOCAL_AMFLAGS = -I m4 --install
+
 EXTRA_DIST = \
 	tests \
 	examples \
diff --git a/configure.in b/configure.in
index 13c31a29d4a995f4267ab2b2281f3e69d359eda3..71c9c613ec7f579ac037b1b72327c3c8233910cc 100644
--- a/configure.in
+++ b/configure.in
@@ -1,5 +1,6 @@
 AC_INIT([Pigeonhole], [0.2.UNSTABLE], [dovecot@dovecot.org], [dovecot-2.0-pigeonhole])
 AC_CONFIG_SRCDIR([src])
+AC_CONFIG_MACRO_DIR([m4])
 
 # 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 
@@ -20,10 +21,9 @@ 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"
-)
+DC_DOVECOT
+LIBDOVECOT_INCLUDE="$LIBDOVECOT_INCLUDE $LIBDOVECOT_STORAGE_INCLUDE" 
+AC_SUBST(LIBDOVECOT_INCLUDE)
 
 # Extensions under development
 #
@@ -78,39 +78,6 @@ AC_ARG_WITH(managesieve,
         want_managesieve=yes)
 AM_CONDITIONAL(BUILD_MANAGESIEVE, test "$want_managesieve" = "yes")
 
-if test "$dovecotdir" = ""; then
-  test "x$prefix" = xNONE && prefix=$ac_default_prefix
-  dovecotdir=$prefix/lib/dovecot
-fi
-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
-eval `cat $dovecotdir/dovecot-config`
-
-AC_SUBST(dovecot_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_LOGIN)
-
-AC_SUBST(LIBDOVECOT_DEPS)
-AC_SUBST(LIBDOVECOT_STORAGE_DEPS)
-AC_SUBST(LIBDOVECOT_LOGIN_DEPS)
-
-LIBDOVECOT_INCLUDE="$LIBDOVECOT_INCLUDE $LIBDOVECOT_STORAGE_INCLUDE" 
-AC_SUBST(LIBDOVECOT_INCLUDE)
-AC_SUBST(LIBDOVECOT_LDA_INCLUDE)
-AC_SUBST(LIBDOVECOT_SERVICE_INCLUDE)
-AC_SUBST(LIBDOVECOT_LOGIN_INCLUDE)
-
 AC_CONFIG_FILES([
 Makefile
 doc/Makefile
diff --git a/m4/dovecot.m4 b/m4/dovecot.m4
new file mode 100644
index 0000000000000000000000000000000000000000..d727bbdeeb586b5404c8878b1c6c1e2421baba1d
--- /dev/null
+++ b/m4/dovecot.m4
@@ -0,0 +1,62 @@
+# dovecot.m4 - Check presence of dovecot -*-Autoconf-*-
+#
+#   Copyright (C) 2010 Dennis Schridde
+#
+# This file is free software; the authors give
+# unlimited permission to copy and/or distribute it, with or without
+# modifications, as long as this notice is preserved.
+
+# serial 1
+
+AC_DEFUN([DC_PLUGIN_DEPS],[
+	_plugin_deps=yes
+	AC_MSG_CHECKING([whether OS supports plugin dependencies])
+	case "$host_os" in
+	  darwin*)
+	    # OSX loads the plugins twice, which breaks stuff
+	    _plugin_deps=no
+	    ;;
+	esac
+	AC_MSG_RESULT([$_plugin_deps])
+	AM_CONDITIONAL([DOVECOT_PLUGIN_DEPS], [test "x$_plugin_deps" = "xyes"])
+	unset _plugin_deps
+])
+
+# Substitute every var in the given comma seperated list
+AC_DEFUN([AX_SUBST_L],[
+	m4_foreach([__var__], [$@], [AC_SUBST(__var__)])
+])
+
+AC_DEFUN([DC_DOVECOT],[
+	AC_ARG_WITH(dovecot,
+	  [  --with-dovecot=DIR      Dovecot base directory],
+			[ dovecotdir="$withval" ], [
+			  dc_prefix=$prefix
+			  test "x$dc_prefix" = xNONE && dc_prefix=$ac_default_prefix
+			  dovecotdir="$dc_prefix/lib/dovecot"
+			]
+	)
+
+	AC_MSG_CHECKING([for dovecot-config in "$dovecotdir"])
+	if test -f "$dovecotdir/dovecot-config"; then
+		AC_MSG_RESULT([$dovecotdir/dovecot-config])
+	else
+		AC_MSG_RESULT([not found])
+		AC_MSG_NOTICE([])
+		AC_MSG_NOTICE([Use --with-dovecot=DIR to provide the path to the dovecot-config file.])
+		AC_MSG_ERROR([dovecot-config not found])
+	fi
+
+	eval `grep \
+		-e ^dovecot_[[a-z]]*= \
+		-e ^DOVECOT_[[A-Z_]]*= \
+		-e ^LIBDOVECOT[[A-Z_]]*= \
+		"$dovecotdir"/dovecot-config`
+	AX_SUBST_L([dovecot_moduledir], [dovecot_pkgincludedir], [dovecot_pkglibexecdir], [dovecot_pkglibdir], [dovecot_docdir])
+	AX_SUBST_L([DOVECOT_CFLAGS], [DOVECOT_LIBS], [DOVECOT_SSL_LIBS])
+	AX_SUBST_L([LIBDOVECOT], [LIBDOVECOT_LOGIN], [LIBDOVECOT_SQL], [LIBDOVECOT_STORAGE])
+	AX_SUBST_L([LIBDOVECOT_DEPS], [LIBDOVECOT_LOGIN_DEPS], [LIBDOVECOT_SQL_DEPS], [LIBDOVECOT_STORAGE_DEPS])
+	AX_SUBST_L([LIBDOVECOT_INCLUDE], [LIBDOVECOT_LDA_INCLUDE], [LIBDOVECOT_SERVICE_INCLUDE], [LIBDOVECOT_STORAGE_INCLUDE], [LIBDOVECOT_LOGIN_INCLUDE])
+
+	DC_PLUGIN_DEPS
+])