diff --git a/.gitignore b/.gitignore
index c5bf0e368ea92859d2d4f725f2ba80777770426c..3ee372d850c9449b7289735daee117b5081a30ba 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,7 +1,44 @@
-.idea
 *.log
 *.o
-Makefile.deploy
+*.so
+*.swp
+*.o
+*.swo
+*.pyc
+Makefile
+.libs/
+.deps/
+*~
+*.la
+*.lo
+Makefile.in
+*.info
+*.bz2
+*.tar
+configure
+aclocal.m4
+autom4te.cache/
+libscrambler-config.h
+libscrambler-config.h.in
+config.log
+config.status
+stamp-h1
+libtool
+tags
+/config/
+
+# cscope and ctags
+cscope.*
+tags
+TAGS
+
+# m4 macros not automatically generated
+/m4/libtool.m4
+/m4/ltoptions.m4
+/m4/ltsugar.m4
+/m4/ltversion.m4
+/m4/lt~obsolete.m4
+
 sudo.password
 attachment.*
 dovecot/*.tar.gz
@@ -18,6 +55,3 @@ dovecot/configuration/conf.d/10-logging.conf
 dovecot/configuration/conf.d/80-mailfilter.conf
 dovecot/configuration/conf.d/auth-passwdfile.conf.ext
 dovecot/configuration/conf.d/auth-sql.conf.ext
-
-cscope.files
-cscope.out
diff --git a/Makefile b/Makefile
deleted file mode 100644
index 6fa4f5446612b068a0649bd99480428b3b0f323c..0000000000000000000000000000000000000000
--- a/Makefile
+++ /dev/null
@@ -1,75 +0,0 @@
-
-DOVECOT_VERSION=2.2.21
-DOVECOT_DIR=$(abspath dovecot)
-DOVECOT_KEYRING_FILE=$(DOVECOT_DIR)/dovecot.gpg
-DOVECOT_SOURCE_URL=http://dovecot.org/releases/2.2/dovecot-$(DOVECOT_VERSION).tar.gz
-DOVECOT_SOURCE_SIG_URL=$(DOVECOT_SOURCE_URL).sig
-DOVECOT_SOURCE_FILE=$(DOVECOT_DIR)/dovecot-$(DOVECOT_VERSION).tar.gz
-DOVECOT_SOURCE_SIG_FILE=$(DOVECOT_SOURCE_FILE).sig
-DOVECOT_SOURCE_DIR=$(DOVECOT_DIR)/source
-DOVECOT_TARGET_DIR=$(DOVECOT_DIR)/target
-DOVECOT_INCLUDE_DIR=dovecot/target/include
-
-SOURCE_DIR=src
-TARGET_LIB_SO=dovecot/target/lib/dovecot/lib18_scrambler_plugin.so
-
-C_FILES=$(shell ls $(SOURCE_DIR)/*.c)
-H_FILES=$(C_FILES:.c=.h)
-O_FILES=$(C_FILES:.c=.o)
-
-CC=gcc
-CFLAGS=-std=gnu99 \
-	-Wall -W -Wmissing-prototypes -Wmissing-declarations -Wpointer-arith -Wchar-subscripts -Wformat=2 \
-	-Wbad-function-cast -fno-builtin-strftime -Wstrict-aliasing=2 -Wl,-z,relro,-z,now \
-	-fPIC -fstack-check -ftrapv -DPIC -D_FORTIFY_SOURCE=2 -DHAVE_CONFIG_H \
-	-I$(DOVECOT_INCLUDE_DIR)
-LDFLAGS=-gs -shared -lsodium -rdynamic -Wl,-soname,lib18_scrambler_plugin.so.1
-
-ifeq ($(DEBUG), 1)
-	CFLAGS+=-DDEBUG_STREAMS -g
-endif
-
-all: $(TARGET_LIB_SO)
-
-$(SOURCE_DIR)/%.o: %.c $(H_FILES)
-	$(CC) -c -o $@ $< $(CFLAGS)
-
-$(TARGET_LIB_SO): $(O_FILES)
-	mkdir -p $(shell dirname $(TARGET_LIB_SO))
-	$(CC) -o $@ $^ $(LDFLAGS)
-
-.PHONY: clean
-
-dovecot-download:
-	-test ! -f $(DOVECOT_SOURCE_FILE) && curl -o $(DOVECOT_SOURCE_FILE) $(DOVECOT_SOURCE_URL)
-
-dovecot-download-verify: dovecot-download
-	-test ! -f $(DOVECOT_SOURCE_SIG_FILE) && curl -o $(DOVECOT_SOURCE_SIG_FILE) $(DOVECOT_SOURCE_SIG_URL)
-	gpg --verify --keyring $(DOVECOT_KEYRING_FILE) $(DOVECOT_SOURCE_SIG_FILE)
-
-dovecot-extract: dovecot-download-verify
-	-test ! -d $(DOVECOT_SOURCE_DIR) && \
-		(tar xzf $(DOVECOT_SOURCE_FILE) -C $(DOVECOT_DIR) && mv $(DOVECOT_DIR)/dovecot-$(DOVECOT_VERSION) $(DOVECOT_SOURCE_DIR))
-
-dovecot-configure: dovecot-extract
-	cd $(DOVECOT_SOURCE_DIR) && ./configure --prefix $(DOVECOT_TARGET_DIR) --with-sqlite
-
-dovecot-compile: dovecot-configure
-	cd $(DOVECOT_SOURCE_DIR) && make
-
-dovecot-install: dovecot-compile
-	cd $(DOVECOT_SOURCE_DIR) && make install
-
-clean:
-	rm -f $(O_FILES) $(TARGET_LIB_SO)
-
-spec-all: $(TARGET_LIB_SO)
-	bash --login -c 'rake spec:integration'
-
-spec-focus: $(TARGET_LIB_SO)
-	bash --login -c 'rake spec:integration:focus'
-
-log:
-	tail -f dovecot/log/dovecot.log
-
--include Makefile.deploy
diff --git a/Makefile.am b/Makefile.am
new file mode 100644
index 0000000000000000000000000000000000000000..f8238b2370ae0a82a37bb0d8fc82e579bd9f2811
--- /dev/null
+++ b/Makefile.am
@@ -0,0 +1,3 @@
+ACLOCAL_AMFLAGS = -I m4
+
+SUBDIRS = src
diff --git a/autogen.sh b/autogen.sh
new file mode 100755
index 0000000000000000000000000000000000000000..2bec3713512d2c4fab2371a3a66a5367bcb18c4a
--- /dev/null
+++ b/autogen.sh
@@ -0,0 +1,8 @@
+#!/bin/sh
+
+set -x
+if [ ! -d "config" ]; then
+	mkdir config
+fi
+
+autoreconf -vi
diff --git a/configure.ac b/configure.ac
new file mode 100644
index 0000000000000000000000000000000000000000..622b7753cf25fb6586601eaac2818149372e6788
--- /dev/null
+++ b/configure.ac
@@ -0,0 +1,79 @@
+AC_PREREQ([2.64])
+AC_INIT([tofu-scrambler],[1.0.0],[],[],[https://0xacab.org/riseuplabs/tofu-scrambler.git])
+
+AC_CONFIG_HEADERS([libscrambler-config.h])
+AC_CONFIG_AUX_DIR([config])
+AC_CONFIG_SRCDIR([src])
+AC_CONFIG_MACRO_DIR([m4])
+
+AC_CANONICAL_TARGET
+AC_CANONICAL_HOST
+
+AM_INIT_AUTOMAKE([foreign dist-bzip2 no-dist-gzip tar-pax])
+AM_MAINTAINER_MODE([enable])
+
+# Enable silent rules if available (Introduced in AM 1.11)
+m4_ifdef([AM_SILENT_RULES], [AM_SILENT_RULES([yes])])
+
+# Checks for C compiler
+AC_USE_SYSTEM_EXTENSIONS
+AC_SYS_LARGEFILE
+AC_PROG_CC
+AC_PROG_CC_STDC
+
+LT_INIT
+
+# Checks for typedefs, structures, and compiler characteristics.
+AC_C_INLINE
+AC_TYPE_INT32_T
+AC_TYPE_INT64_T
+AC_TYPE_MODE_T
+AC_TYPE_OFF_T
+AC_TYPE_PID_T
+AC_TYPE_SIZE_T
+AC_TYPE_SSIZE_T
+AC_TYPE_UID_T
+AC_TYPE_UINT16_T
+AC_TYPE_UINT32_T
+AC_TYPE_UINT64_T
+AC_TYPE_UINT8_T
+
+# Checks for standard C headers.
+AC_HEADER_STDC
+
+# Make sure we have libsodium
+AC_CHECK_LIB([sodium], [sodium_init], [],
+	[AC_MSG_ERROR([Cannot find libsodium. Use LDFLAGS=-Ldir to specify its location.])]
+)
+AC_CHECK_HEADERS([sodium.h])
+
+# Make sure we have dovecot develpment headers are available. We need to make
+# this trick so the m4 macro look in /usr/include and not /usr/local/include.
+# Better solution is welcome!
+orig_prefix=$prefix
+prefix="/usr"
+DC_DOVECOT
+DC_DOVECOT_MODULEDIR
+prefix=$orig_prefix
+LIBDOVECOT_INCLUDE="$LIBDOVECOT_INCLUDE"
+AC_SUBST(LIBDOVECOT_INCLUDE)
+
+CFLAGS="$CFLAGS $DOVECOT_CFLAGS"
+
+CFLAGS="$CFLAGS -Wall -Werror -fno-strict-aliasing -W -Wmissing-prototypes"
+CFLAGS="$CFLAGS -W -Wfloat-equal -Wundef -Wpointer-arith"
+CFLAGS="$CFLAGS -Wstrict-prototypes -Wwrite-strings"
+CFLAGS="$CFLAGS -Wredundant-decls -Wchar-subscripts -Wcomment"
+CFLAGS="$CFLAGS -Wnested-externs -Wbad-function-cast -Wswitch-enum"
+CFLAGS="$CFLAGS -Waggregate-return -Wpacked -Wunused"
+CFLAGS="$CFLAGS -Wunused-parameter "
+CFLAGS="$CFLAGS -Wpointer-arith -Wchar-subscripts -Wformat=2"
+CFLAGS="$CFLAGS -Wbad-function-cast -Wstrict-aliasing=2"
+CFLAGS="$CFLAGS -Wl,-z,relro,-z,now -fPIC -fstack-check -D_FORTIFY_SOURCE=2"
+
+AC_CONFIG_FILES([
+	Makefile
+  src/Makefile
+])
+
+AC_OUTPUT
diff --git a/m4/dovecot.m4 b/m4/dovecot.m4
new file mode 100644
index 0000000000000000000000000000000000000000..10047ec6527bf19d92e14100e88adab46da12f3d
--- /dev/null
+++ b/m4/dovecot.m4
@@ -0,0 +1,99 @@
+# 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 22
+
+AC_DEFUN([DC_DOVECOT_MODULEDIR],[
+	AC_ARG_WITH(moduledir,
+	[  --with-moduledir=DIR    Base directory for dynamically loadable modules],
+		moduledir="$withval",
+		moduledir=$libdir/dovecot
+	)
+	AC_SUBST(moduledir)
+])
+
+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_ARG_WITH(dovecot-install-dirs,
+		[AC_HELP_STRING([--with-dovecot-install-dirs],
+		[Use install directories configured for Dovecot (default)])],
+	if test x$withval = xno; then
+		use_install_dirs=no
+	else
+		use_install_dirs=yes
+	fi,
+	use_install_dirs=yes)
+
+	AC_MSG_CHECKING([for "$dovecotdir/dovecot-config"])
+	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
+
+	old=`pwd`
+	cd $dovecotdir
+	abs_dovecotdir=`pwd`
+	cd $old
+	DISTCHECK_CONFIGURE_FLAGS="--with-dovecot=$abs_dovecotdir --without-dovecot-install-dirs"
+
+	eval `grep -i '^dovecot_[[a-z_]]*=' "$dovecotdir"/dovecot-config`
+	eval `grep '^LIBDOVECOT[[A-Z0-9_]]*=' "$dovecotdir"/dovecot-config`
+
+	dovecot_installed_moduledir="$dovecot_moduledir"
+
+	if test "$use_install_dirs" = "no"; then
+		# the main purpose of these is to fix make distcheck for plugins
+		# other than that, they don't really make much sense
+		dovecot_pkgincludedir='$(pkgincludedir)'
+		dovecot_pkglibdir='$(pkglibdir)'
+		dovecot_pkglibexecdir='$(libexecdir)/dovecot'
+		dovecot_docdir='$(docdir)'
+		dovecot_moduledir='$(moduledir)'
+	fi
+
+	AX_SUBST_L([DISTCHECK_CONFIGURE_FLAGS], [dovecotdir], [dovecot_moduledir], [dovecot_installed_moduledir], [dovecot_pkgincludedir], [dovecot_pkglibexecdir], [dovecot_pkglibdir], [dovecot_docdir])
+	AX_SUBST_L([DOVECOT_INSTALLED], [DOVECOT_CFLAGS], [DOVECOT_LIBS], [DOVECOT_SSL_LIBS], [DOVECOT_SQL_LIBS], [DOVECOT_COMPRESS_LIBS])
+	AX_SUBST_L([LIBDOVECOT], [LIBDOVECOT_LOGIN], [LIBDOVECOT_SQL], [LIBDOVECOT_SSL], [LIBDOVECOT_COMPRESS], [LIBDOVECOT_LDA], [LIBDOVECOT_STORAGE], [LIBDOVECOT_DSYNC], [LIBDOVECOT_LIBFTS])
+	AX_SUBST_L([LIBDOVECOT_DEPS], [LIBDOVECOT_LOGIN_DEPS], [LIBDOVECOT_SQL_DEPS], [LIBDOVECOT_SSL_DEPS], [LIBDOVECOT_COMPRESS_DEPS], [LIBDOVECOT_LDA_DEPS], [LIBDOVECOT_STORAGE_DEPS], [LIBDOVECOT_DSYNC_DEPS], [LIBDOVECOT_LIBFTS_DEPS])
+	AX_SUBST_L([LIBDOVECOT_INCLUDE], [LIBDOVECOT_LDA_INCLUDE], [LIBDOVECOT_AUTH_INCLUDE], [LIBDOVECOT_DOVEADM_INCLUDE], [LIBDOVECOT_SERVICE_INCLUDE], [LIBDOVECOT_STORAGE_INCLUDE], [LIBDOVECOT_LOGIN_INCLUDE], [LIBDOVECOT_SQL_INCLUDE], [LIBDOVECOT_IMAP_LOGIN_INCLUDE], [LIBDOVECOT_CONFIG_INCLUDE], [LIBDOVECOT_IMAP_INCLUDE], [LIBDOVECOT_POP3_INCLUDE], [LIBDOVECOT_DSYNC_INCLUDE], [LIBDOVECOT_IMAPC_INCLUDE], [LIBDOVECOT_FTS_INCLUDE], [LIBDOVECOT_NOTIFY_INCLUDE], [LIBDOVECOT_ACL_INCLUDE], [LIBDOVECOT_LIBFTS_INCLUDE])
+
+	AM_CONDITIONAL(DOVECOT_INSTALLED, test "$DOVECOT_INSTALLED" = "yes")
+
+	DC_PLUGIN_DEPS
+])
diff --git a/src/Makefile.am b/src/Makefile.am
new file mode 100644
index 0000000000000000000000000000000000000000..42c6c0c7e62bbdac2805a610e1ce4d31b284c312
--- /dev/null
+++ b/src/Makefile.am
@@ -0,0 +1,16 @@
+AM_CPPFLAGS = -I$(top_srcdir)/src -I$(builddir) $(LIBDOVECOT_INCLUDE)
+
+lib_LTLIBRARIES = lib18_scrambler_plugin.la
+
+lib18_scrambler_plugin_la_SOURCES = \
+	scrambler-common.c  \
+	scrambler-common.h  \
+	scrambler-istream.c \
+	scrambler-istream.h \
+	scrambler-ostream.c \
+	scrambler-ostream.h \
+	scrambler-plugin.c  \
+	scrambler-plugin.h
+
+lib18_scrambler_plugin_la_LDFLAGS = \
+	-shared -rdynamic -avoid-version -module