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

prefix=@prefix@
exec_prefix=@exec_prefix@
sbindir=@sbindir@
libdir=@libdir@
man8dir=@mandir@/man8
piddir=@localstatedir@/stunnel
ssldir=@ssldir@
sslcnf=@sslcnf@
certdir=$(sslcnf)/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 log.o
DESTFILES=$(sbindir)/stunnel $(libdir)/stunnel.so $(man8dir)/stunnel.8 $(certdir)/stunnel.pem

# standard external rules

all: stunnel stunnel.so stunnel.pem

install: installdirs $(DESTFILES)

uninstall:
	rm -f $(DESTFILES)

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

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

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

mostlyclean: distclean

maintainer-clean: distclean

dist: distclean
	./mkinstalldirs ../dist
	cp -f stunnel.exe ../dist/$(VERSION).exe
	cp -rp . ../dist/$(VERSION)
	tar -cf - -C ../dist $(VERSION) | gzip > ../dist/$(VERSION).tar.gz
	rm -rf ../dist/$(VERSION)

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 stunnel.pem
	$(MAKE) stunnel.pem

# internal rules

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

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

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

configure: configure.in
	autoconf

$(OBJS): Makefile common.h

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

$(sbindir)/stunnel: stunnel
	$(INSTALL) -m 711 stunnel $(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)/stunnel.pem: stunnel.pem
	$(INSTALL) -m 600 stunnel.pem $(certdir)

