#####################################################
# Makefile for stunnel by Michal Trojnara 1998-2000 #
#####################################################

prefix=@prefix@
exec_prefix=@exec_prefix@
sbindir=@sbindir@
libdir=@libdir@
man8dir=@mandir@/man8
piddir=@localstatedir@
ssldir=@ssldir@
certdir=$(ssldir)/certs
@SET_MAKE@
VERSION=stunnel-@VERSION@
RANDOM_OPT=@RANDOM_OPT@
CC=@CC@
INSTALL=@INSTALL@
CFLAGS=@CFLAGS@ @DEFS@ -Dcertdir=\"$(certdir)\" -Dlibdir=\"$(libdir)\" -Dpiddir=\"$(piddir)\"
LIBS=@LIBS@
OBJS=stunnel.o ssl.o protocol.o sthreads.o pty.o log.o  truerand.o nonce.o
DESTFILES=$(sbindir)/klpstunnel $(libdir)/stunnel.so $(man8dir)/stunnel.8 $(certdir)/klpstunnel.pem

# standard external rules

all: klpstunnel stunnel.so klpstunnel.pem

install: all installdirs $(DESTFILES)

uninstall:
	rm -f $(DESTFILES)

install-strip:
	$(MAKE) INSTALL='$(INSTALL) -s' install

clean:
	rm -f klpstunnel stunnel.so $(OBJS) core config.log

distclean: clean
	rm -f stunnel.8 config.cache config.status Makefile

mostlyclean: distclean

maintainer-clean: distclean

dist: distclean ../dist/$(VERSION).exe
	cp -rp . ../dist/$(VERSION)
	tar -cf - -C ../dist $(VERSION) | gzip > ../dist/$(VERSION).tar.gz
	rm -rf ../dist/$(VERSION)
	gpg --yes --armor --detach-sign ../dist/$(VERSION).tar.gz

installdirs: mkinstalldirs
	./mkinstalldirs $(sbindir) $(libdir) $(man8dir) $(certdir) $(piddir)
	chmod a=rwx,+t $(piddir)

# non-standard external rules

stunnel.html: man2html.pl stunnel.8
	./man2html.pl < stunnel.8 > stunnel.html

cert:
	rm -f klpstunnel.pem
	$(MAKE) klpstunnel.pem

# internal rules

klpstunnel: $(OBJS)
	$(CC) $(LDFLAGS) -o klpstunnel $(OBJS) $(LIBS)

stunnel.so: Makefile env.c
	$(CC) -fPIC -shared $(LDFLAGS) -o stunnel.so env.c $(LIBS) || \
	touch stunnel.so
	@test -s klpstunnel.pem || echo
	@test -s klpstunnel.pem || echo "You can ignore this error"
	@test -s klpstunnel.pem || echo "Shared library won't work on your system"
	@test -s klpstunnel.pem || echo

Makefile: configure Makefile.in
	./configure
	$(MAKE) clean

configure: configure.in
	autoconf

$(OBJS): Makefile common.h

klpstunnel.pem: stunnel.cnf
	$(ssldir)/bin/openssl req -new -x509 -days 365 -nodes \
		-config stunnel.cnf -out klpstunnel.pem -keyout klpstunnel.pem
	$(ssldir)/bin/openssl gendh $(RANDOM_OPT) 512 >> klpstunnel.pem
	$(ssldir)/bin/openssl x509 -subject -dates -fingerprint -noout \
		-in klpstunnel.pem
	chmod 600 klpstunnel.pem

$(sbindir)/klpstunnel: klpstunnel
	$(INSTALL) -m 711 klpstunnel $(sbindir)

$(libdir)/stunnel.so: stunnel.so
	test -s stunnel.so && $(INSTALL) -m 755 stunnel.so $(libdir)

$(man8dir)/stunnel.8: stunnel.8
	$(INSTALL) -m 644 stunnel.8 $(man8dir)

$(certdir)/klpstunnel.pem: klpstunnel.pem
	$(INSTALL) -m 600 klpstunnel.pem $(certdir)

../dist/$(VERSION).exe: stunnel.exe
	./mkinstalldirs ../dist
	cp -f stunnel.exe ../dist/$(VERSION).exe
	gpg --yes --armor --detach-sign ../dist/$(VERSION).exe

