-# slock - simple screen locker
-# © 2006-2007 Anselm R. Garbe, Sander van Dijk
-# © 2009 Michael Stapelberg
-
-include config.mk
-
-SRC = i3lock.c
-OBJ = ${SRC:.c=.o}
-
-all: options i3lock
-
-options:
- @echo i3lock build options:
- @echo "CFLAGS = ${CFLAGS}"
- @echo "LDFLAGS = ${LDFLAGS}"
- @echo "CC = ${CC}"
-
-.c.o:
- @echo CC $<
- @${CC} -c ${CFLAGS} $<
-
-${OBJ}: config.mk
-
-i3lock: ${OBJ}
- @echo CC -o $@
- @${CC} -o $@ ${OBJ} ${LDFLAGS}
+TOPDIR=$(shell pwd)
+
+INSTALL=install
+PREFIX=/usr
+SYSCONFDIR=/etc
+PKG_CONFIG=pkg-config
+
+# Check if pkg-config is installed, we need it for building CFLAGS/LIBS
+ifeq ($(shell which $(PKG_CONFIG) 2>/dev/null 1>/dev/null || echo 1),1)
+$(error "$(PKG_CONFIG) was not found")
+endif
+
+CFLAGS += -std=c99
+CFLAGS += -pipe
+CFLAGS += -Wall
+CPPFLAGS += -D_GNU_SOURCE
+CFLAGS += $(shell $(PKG_CONFIG) --cflags cairo xcb-dpms xcb-xinerama xcb-atom xcb-image xcb-xkb xkbcommon xkbcommon-x11)
+LIBS += $(shell $(PKG_CONFIG) --libs cairo xcb-dpms xcb-xinerama xcb-atom xcb-image xcb-xkb xkbcommon xkbcommon-x11)
+LIBS += -lpam
+LIBS += -lev
+LIBS += -lm
+
+FILES:=$(wildcard *.c)
+FILES:=$(FILES:.c=.o)
+
+ifeq ($(wildcard .git),)
+ # not in git repository
+ VERSION := $(shell [ -f $(TOPDIR)/I3LOCK_VERSION ] && cat $(TOPDIR)/I3LOCK_VERSION | cut -d '-' -f 1)
+ I3LOCK_VERSION := '$(shell [ -f $(TOPDIR)/I3LOCK_VERSION ] && cat $(TOPDIR)/I3LOCK_VERSION)'
+else
+ VERSION:=$(shell git describe --tags --abbrev=0)
+ I3LOCK_VERSION:="$(shell git describe --tags --always) ($(shell git log --pretty=format:%cd --date=short -n1))"
+endif
+CPPFLAGS += -DVERSION=\"${I3LOCK_VERSION}\"
+
+.PHONY: install clean uninstall
+
+all: i3lock
+
+i3lock: ${FILES}
+ $(CC) $(LDFLAGS) -o $@ $^ $(LIBS)