Use automake
diff --git a/Makefile b/Makefile
deleted file mode 100644
index d7d1671..0000000
--- a/Makefile
+++ /dev/null
@@ -1,15 +0,0 @@
-include config.mk
-
-subdirs = wayland compositor clients spec data
-
-all : subdirs-all
-
-subdirs-all subdirs-clean subdirs-install:
-	for f in $(subdirs); do $(MAKE) -C $$f $(@:subdirs-%=%); done
-
-install : subdirs-install
-
-clean : subdirs-clean
-
-config.mk : config.mk.in
-	./config.status
diff --git a/Makefile.am b/Makefile.am
new file mode 100644
index 0000000..46529e5
--- /dev/null
+++ b/Makefile.am
@@ -0,0 +1,3 @@
+SUBDIRS = wayland compositor clients data
+
+ACLOCAL_AMFLAGS = -I m4
diff --git a/autogen.sh b/autogen.sh
index 4e8b11b..8b6f58f 100755
--- a/autogen.sh
+++ b/autogen.sh
@@ -9,4 +9,4 @@
 autoreconf --force -v --install || exit 1
 cd $ORIGDIR || exit $?
 
-$srcdir/configure --enable-maintainer-mode "$@"
+$srcdir/configure "$@"
diff --git a/clients/Makefile b/clients/Makefile
deleted file mode 100644
index bd9d279..0000000
--- a/clients/Makefile
+++ /dev/null
@@ -1,34 +0,0 @@
-include ../config.mk
-
-CFLAGS += -I../wayland $(CLIENT_CFLAGS)
-LDLIBS += -L../wayland -lwayland-client $(CLIENT_LIBS) -lrt -lm
-
-egl_clients = gears
-cairo_clients = flower screenshot terminal image view dnd
-
-all : $(egl_clients) $(cairo_clients)
-
-clean :
-	rm -f $(egl_clients) $(cairo_clients) *.o .*.deps
-
-flower : flower.o window.o wayland-glib.o cairo-util.o
-gears : gears.o window.o wayland-glib.o cairo-util.o
-screenshot : screenshot.o screenshooter-protocol.o wayland-glib.o cairo-util.o
-terminal : terminal.o window.o wayland-glib.o cairo-util.o
-image : image.o window.o wayland-glib.o cairo-util.o
-view : view.o window.o wayland-glib.o cairo-util.o
-dnd : dnd.o window.o wayland-glib.o cairo-util.o
-
-screenshot.c : screenshooter-client-protocol.h
-
-screenshooter-protocol.c : ../compositor/screenshooter.xml
-	../wayland/scanner code < $< > $@
-
-screenshooter-client-protocol.h : ../compositor/screenshooter.xml
-	../wayland/scanner client-header < $< > $@
-
-terminal : LDLIBS += -lutil
-view : CFLAGS += $(POPPLER_CFLAGS)
-view : LDLIBS += $(POPPLER_LIBS)
-
-install :
\ No newline at end of file
diff --git a/clients/Makefile.am b/clients/Makefile.am
new file mode 100644
index 0000000..9277e77
--- /dev/null
+++ b/clients/Makefile.am
@@ -0,0 +1,36 @@
+noinst_PROGRAMS = gears flower screenshot terminal image view dnd
+noinst_LTLIBRARIES = libtoytoolkit.la
+
+INCLUDES = -I$(top_srcdir)/wayland $(CLIENT_CFLAGS) $(POPPLER_CFLAGS)
+
+LDADD = $(top_builddir)/wayland/libwayland-client.la	\
+	libtoytoolkit.la $(CLIENT_LIBS) -lrt -lm
+
+AM_CPPFLAGS = -DDATADIR='"$(datadir)"'
+
+libtoytoolkit_la_SOURCES =			\
+	window.c				\
+	window.h				\
+	wayland-glib.c				\
+	wayland-glib.h				\
+	cairo-util.c				\
+	cairo-util.h
+
+flower_SOURCES = flower.c
+gears_SOURCES = gears.c
+screenshot_SOURCES = screenshot.c screenshooter-protocol.c
+terminal_SOURCES = terminal.c
+image_SOURCES = image.c
+view_SOURCES = view.c
+dnd_SOURCES = dnd.c
+
+BUILT_SOURCES =					\
+	screenshooter-client-protocol.h		\
+	screenshooter-protocol.c
+
+CLEANFILES = $(BUILT_SOURCES)
+
+include $(top_srcdir)/wayland/scanner.mk
+
+terminal_LDADD = $(LDADD) -lutil
+view_LDADD = $(LDADD) $(POPPLER_LIBS)
diff --git a/compositor/Makefile b/compositor/Makefile
deleted file mode 100644
index 21d8353..0000000
--- a/compositor/Makefile
+++ /dev/null
@@ -1,29 +0,0 @@
-include ../config.mk
-
-CFLAGS += -I../wayland $(COMPOSITOR_CFLAGS)
-LDLIBS += -L../wayland -lwayland-server $(COMPOSITOR_LIBS) -rdynamic -lrt -lm
-
-all : compositor
-
-compositor :					\
-	compositor.o				\
-	compositor-drm.o			\
-	compositor-x11.o			\
-	screenshooter.o				\
-	screenshooter-protocol.o		\
-	drm.o					\
-	shm.o
-
-screenshooter.c : screenshooter-server-protocol.h
-
-screenshooter-protocol.c : screenshooter.xml
-	../wayland/scanner code < $< > $@
-
-screenshooter-server-protocol.h : screenshooter.xml
-	../wayland/scanner server-header < $< > $@
-
-clean :
-	rm -f compositor *.o .*.deps
-
-install :
-	install 70-wayland.rules ${udev_rules_dir}
diff --git a/compositor/Makefile.am b/compositor/Makefile.am
new file mode 100644
index 0000000..a7d121b
--- /dev/null
+++ b/compositor/Makefile.am
@@ -0,0 +1,27 @@
+noinst_PROGRAMS = compositor
+
+INCLUDES = -I$(top_srcdir)/wayland $(COMPOSITOR_CFLAGS)
+AM_CPPFLAGS = -DDATADIR='"$(datadir)"'
+
+compositor_LDADD =					\
+	$(top_builddir)/wayland/libwayland-server.la	\
+	$(COMPOSITOR_LIBS)
+
+compositor_SOURCES =				\
+	compositor.c				\
+	compositor.h				\
+	compositor-drm.c			\
+	compositor-x11.c			\
+	screenshooter.c				\
+	screenshooter-protocol.c		\
+	screenshooter-server-protocol.h		\
+	drm.c					\
+	shm.c
+
+BUILT_SOURCES =					\
+	screenshooter-server-protocol.h		\
+	screenshooter-protocol.c
+
+CLEANFILES = $(BUILT_SOURCES)
+
+include $(top_srcdir)/wayland/scanner.mk
diff --git a/config.mk.in b/config.mk.in
deleted file mode 100644
index 8c59e15..0000000
--- a/config.mk.in
+++ /dev/null
@@ -1,28 +0,0 @@
-CFLAGS = @GCC_CFLAGS@ -O -g -Wall -MMD -MF .$*.deps
-CPPFLAGS = -DDATADIR=\"$(datadir)\"
-
-prefix = @prefix@
-exec_prefix = @exec_prefix@
-udev_rules_dir = @sysconfdir@/udev/rules.d
-libdir = @libdir@
-includedir = @includedir@
-datadir = @datadir@
-datarootdir = @datarootdir@
-
-COMPOSITOR_CFLAGS = @COMPOSITOR_CFLAGS@
-COMPOSITOR_LIBS = @COMPOSITOR_LIBS@
-
-FFI_CFLAGS = @FFI_CFLAGS@
-FFI_LIBS = @FFI_LIBS@
-
-CLIENT_CFLAGS = @CLIENT_CFLAGS@
-CLIENT_LIBS = @CLIENT_LIBS@
-
-POPPLER_CFLAGS = @POPPLER_CFLAGS@
-POPPLER_LIBS = @POPPLER_LIBS@
-
-EXPAT_LIBS = @EXPAT_LIBS@
-
-first : all
-
--include .*.deps
diff --git a/configure.ac b/configure.ac
index 2e2743f..fdfd61c 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1,5 +1,9 @@
 AC_INIT(wayland, 0.1)
+AM_INIT_AUTOMAKE([foreign dist-bzip2])
 AC_PROG_CC
+AC_PROG_LIBTOOL
+AC_CONFIG_MACRO_DIR([m4])
+AM_SILENT_RULES([yes])
 
 PKG_PROG_PKG_CONFIG()
 PKG_CHECK_MODULES(FFI, [libffi])
@@ -25,5 +29,11 @@
 	     [AC_MSG_ERROR([Can't find expat library. Please install expat.])])
 AC_SUBST(EXPAT_LIBS)
 
-AC_CONFIG_FILES([config.mk wayland/wayland-server.pc wayland/wayland-client.pc])
+AC_CONFIG_FILES([wayland/wayland-server.pc
+		 wayland/wayland-client.pc
+		 Makefile
+		 wayland/Makefile
+		 compositor/Makefile
+		 clients/Makefile
+		 data/Makefile])
 AC_OUTPUT
diff --git a/data/Makefile b/data/Makefile.am
similarity index 72%
rename from data/Makefile
rename to data/Makefile.am
index f8b28b6..9de8ebd 100644
--- a/data/Makefile
+++ b/data/Makefile.am
@@ -1,6 +1,6 @@
-include ../config.mk
+waylanddatadir = $(datadir)/wayland
 
-cursor_images =					\
+dist_waylanddata_DATA =				\
 	bottom_left_corner.png			\
 	bottom_right_corner.png			\
 	bottom_side.png				\
@@ -19,11 +19,3 @@
 	top_right_corner.png			\
 	top_side.png				\
 	xterm.png
-
-all :
-
-clean :
-
-install :
-	install -d $(datadir)/wayland
-	install $(cursor_images) $(datadir)/wayland
diff --git a/m4/.gitignore b/m4/.gitignore
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/m4/.gitignore
diff --git a/compositor/screenshooter.xml b/protocol/screenshooter.xml
similarity index 100%
rename from compositor/screenshooter.xml
rename to protocol/screenshooter.xml
diff --git a/wayland/protocol.xml b/protocol/wayland.xml
similarity index 100%
rename from wayland/protocol.xml
rename to protocol/wayland.xml
diff --git a/spec/Makefile b/spec/Makefile
deleted file mode 100644
index 26a3e23..0000000
--- a/spec/Makefile
+++ /dev/null
@@ -1,11 +0,0 @@
-include ../config.mk
-
-all : main.pdf
-
-main.pdf : main.tex
-	pdflatex main.tex
-
-clean :
-	rm -f main.pdf main.aux main.log
-
-install :
diff --git a/wayland/Makefile b/wayland/Makefile
deleted file mode 100644
index e922bf6..0000000
--- a/wayland/Makefile
+++ /dev/null
@@ -1,61 +0,0 @@
-include ../config.mk
-
-libs = libwayland-server.so libwayland-client.so
-
-all : $(libs) scanner
-
-headers =					\
-	wayland-util.h				\
-	wayland-server-protocol.h		\
-	wayland-server.h			\
-	wayland-client-protocol.h		\
-	wayland-client.h \
-
-libwayland-server.so :				\
-	wayland-protocol.o			\
-	wayland-server.o			\
-	event-loop.o				\
-	connection.o				\
-	wayland-util.o				\
-	wayland-hash.o
-
-libwayland-client.so :				\
-	wayland-protocol.o			\
-	wayland-client.o			\
-	connection.o				\
-	wayland-util.o				\
-	wayland-hash.o
-
-wayland-server.o : wayland-server-protocol.h
-wayland-client.o : wayland-client-protocol.h
-
-wayland-protocol.c : protocol.xml scanner
-	./scanner code < $< > $@
-
-wayland-server-protocol.h : protocol.xml scanner
-	./scanner server-header < $< > $@
-
-wayland-client-protocol.h : protocol.xml scanner
-	./scanner client-header < $< > $@
-
-$(libs) : CFLAGS += -fPIC $(FFI_CFLAGS)
-$(libs) : LDLIBS += $(FFI_LIBS)
-$(libs) :
-	gcc -shared $^ $(LDLIBS)  -o $@
-
-scanner :					\
-	scanner.o				\
-	wayland-util.o
-
-scanner : LDLIBS += $(EXPAT_LIBS)
-
-install : $(libs)
-	install -d $(libdir) $(includedir) $(libdir)/pkgconfig
-	install wayland-server.pc wayland-client.pc $(libdir)/pkgconfig
-	install $(libs) $(libdir)
-	install $(headers) $(includedir)
-
-clean :
-	rm -f scanner *.o *.so .*.deps
-	rm -f wayland-protocol.c \
-		wayland-server-protocol.h wayland-client-protocol.h
diff --git a/wayland/Makefile.am b/wayland/Makefile.am
new file mode 100644
index 0000000..d46a7e0
--- /dev/null
+++ b/wayland/Makefile.am
@@ -0,0 +1,50 @@
+lib_LTLIBRARIES = libwayland-server.la libwayland-client.la
+noinst_LTLIBRARIES = libwayland-util.la
+
+include_HEADERS =				\
+	wayland-util.h				\
+	wayland-server-protocol.h		\
+	wayland-server.h			\
+	wayland-client-protocol.h		\
+	wayland-client.h
+
+libwayland_util_la_SOURCES =			\
+	connection.c				\
+	connection.h				\
+	wayland-util.c				\
+	wayland-util.h				\
+	wayland-hash.c
+
+libwayland_server_la_LIBADD = $(FFI_LIBS) libwayland-util.la
+libwayland_server_la_SOURCES =			\
+	wayland-protocol.c			\
+	wayland-server.c			\
+	event-loop.c
+
+libwayland_client_la_LIBADD = $(FFI_LIBS) libwayland-util.la
+libwayland_client_la_SOURCES =			\
+	wayland-protocol.c			\
+	wayland-client.c
+
+INCLUDES = $(FFI_CFLAGS)
+
+include $(top_srcdir)/wayland/scanner.mk
+
+noinst_PROGRAMS = scanner
+
+scanner_SOURCES =				\
+	scanner.c
+
+scanner_LDADD = $(EXPAT_LIBS) libwayland-util.la
+
+$(BUILT_SOURCES) : scanner
+
+BUILT_SOURCES =					\
+	wayland-server-protocol.h		\
+	wayland-client-protocol.h		\
+	wayland-protocol.c
+
+CLEANFILES = $(BUILT_SOURCES)
+
+foo :
+	echo $(top_builddir)
\ No newline at end of file
diff --git a/wayland/scanner.mk b/wayland/scanner.mk
new file mode 100644
index 0000000..0f03af1
--- /dev/null
+++ b/wayland/scanner.mk
@@ -0,0 +1,8 @@
+%-protocol.c : $(top_srcdir)/protocol/%.xml
+	$(top_builddir)/wayland/scanner code < $< > $@
+
+%-server-protocol.h : $(top_srcdir)/protocol/%.xml
+	$(top_builddir)/wayland/scanner server-header < $< > $@
+
+%-client-protocol.h : $(top_srcdir)/protocol/%.xml
+	$(top_builddir)/wayland/scanner client-header < $< > $@