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

prefix=@prefix@
exec_prefix=@exec_prefix@
sbindir=@sbindir@
libdir=@libdir@
mandir=@mandir@/man8
ssldir=@ssldir@
certdir=$(ssldir)/certs
@SET_MAKE@
VERSION=stunnel-@VERSION@
CC=@CC@
INSTALL=@INSTALL@
CFLAGS=@CFLAGS@ -Dlibdir=\"@libdir@\"
LIBS=@LIBS@
OBJS=stunnel.o ssl.o protocol.o sthreads.o log.o
DESTFILES=$(sbindir)/stunnel $(libdir)/stunnel.so $(mandir)/stunnel.8 \
    $(certdir)/stunnel.pem

# standard external rules

all: stunnel stunnel.so

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 config.h

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) $(mandir) $(certdir)

# 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 && echo && echo "You can ignore this error" && echo)

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

configure: configure.in
	autoconf
	autoheader configure.in > config.h.in

$(OBJS): Makefile common.h

stunnel.pem:
	$(ssldir)/bin/openssl req -new -x509 -days 365 -config stunnel.cnf \
		-out stunnel.pem -keyout stunnel.pem
	$(ssldir)/bin/openssl gendh -rand \
		`test -c /dev/random && echo /dev/random` 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)

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

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

