--- Makefile	2013-08-21 00:35:26.024299153 +0200
+++ Makefile	2013-08-21 01:06:19.256160856 +0200
@@ -1,23 +1,64 @@
+#!/usr/bin/make -f <
 
-# Editable options
-TOP=$(HOME)/openssl
-SSLINC=/usr/local/ssl/include
-SSLLIB=/usr/local/ssl/lib
+NAME	= pvk
 
-CFLAGS=-Wall -I$(SSLINC)
+# Source files
+SRC	= \
+	pvk.c \
+	pvkerr.c \
+	pvkread.c \
+	pvkwrite.c
 
-all: pvk
+# Usefull programs
+PKG_CONFIG	?= pkg-config
 
-OBJS=pvkread.o pvkwrite.o pvkerr.o
+CC	?= cc
 
-$(OBJS): pvk.h
+INSTALL	?= install
+RM	?= rm -f
 
-pvk: $(OBJS) pvk.o pvk.h
-	$(CC) $(CFLAGS) -o pvk pvk.o $(OBJS) -L$(SSLLIB) -lcrypto
+# Installation destination
+DESTDIR	=
+PREFIX	= /usr/local
+BINDIR	= $(PREFIX)/bin
 
-errors:
-	perl $(TOP)/util/mkerr.pl -nostatic -conf pvk.ec -write *.c
+# Compiler flags, if user environnement does not define them
+CFLAGS	?= \
+	-W -Wall
 
-clean:
-	rm -f *.o pvk
+CRYPTO_CFLAGS	?= \
+	$(shell sh -c '$(PKG_CONFIG) --cflags libcrypto')
+
+CRYPTO_LDFLAGS	?= \
+	$(shell sh -c '$(PKG_CONFIG) --libs libcrypto')
+
+# Compiler flags to apply in any cases
+SPEC_CFLAGS	= \
+	$(CRYPTO_CFLAGS)
+
+SPEC_LDFLAGS	= \
+	$(CRYPTO_LDFLAGS)
+
+# Build pvk by default
+all:	$(NAME)
+
+# Compile rules
+$(NAME):	$(SRC:c=o)
+	$(CC) -o $@ $^ $(SPEC_LDFLAGS) $(LDFLAGS)
 
+$(NAME).o:	$(NAME).c $(NAME).h
+	$(CC) -c $(SPEC_CFLAGS) $(CFLAGS) -o $@ $<
+
+%.o:	%.c
+	$(CC) -c $(SPEC_CFLAGS) $(CFLAGS) -o $@ $<
+
+# Install rules
+install:	$(DESTDIR)$(BINDIR)/$(NAME)
+
+$(DESTDIR)$(BINDIR)/$(NAME):	$(NAME)
+	$(INSTALL) -D -m 755 $< $@
+
+# Cleanup rules
+clean:
+	$(RM) $(SRC:c=o)
+	$(RM) $(NAME)