libsocket_SRC=libsocket/C++/unixbase.cpp libsocket/C++/unixclientstream.cpp libsocket/C++/unixserverstream.cpp libsocket/C++/socket.cpp libsocket/C++/exception.cpp libsocket/C++/streamclient.cpp
libbotan_SRC=botan/botan_all.cpp
SRC_C=libsocket/C/unix/libunixsocket.c
SRC=main.cpp netstring.cpp crypto.cpp socket.cpp \
	$(libsocket_SRC) $(libbotan_SRC)
OBJ=$(SRC:.cpp=.o) $(SRC_C:.c=.o)
NAME=tempmail2

OPTI:=-O3

CFLAGS=-I libsocket/headers/ -fPIC -fpie $(OPTI)
CXXFLAGS=-I cppcodec -I libsocket/headers -I botan -DMIXED -DnoUSE_AEAD_MODE -DMAYBE_FIXED_IV -fPIC -fpie $(OPTI)
LIBS=-lboost_program_options -lpthread

.PHONY: libsocket submodules

all: submodules $(NAME)
$(NAME): $(OBJ) $(LSPP)
	$(CXX) -o $@ $(OBJ) $(LIBS)

# .botan-opts contains botan configuration
$(libbotan_SRC): botan/configure.py .botan-opts
	rm -f botan/botan_all*.{cpp,h}
	(cd botan ; python configure.py --amalgamation $(shell cat .botan-opts))
crypto.o: crypto.cpp $(libbotan_SRC)
socket.o: socket.cpp libsocket/headers/conf.h
$(libsocket_SRC): libsocket/headers/conf.h
$(SRC_C): libsocket/headers/conf.h
libsocket/headers/conf.h:
	(cd libsocket;cmake .)

clean:
	rm -Rf $(OBJ) *~
clean-botan:
	(cd botan ; git clean -fxd)
distclean: clean clean-botan
	rm -Rf $(NAME) $(libbotan_SRC)

install: $(NAME)
	install -d $(DESTDIR)/usr/sbin
	install -d $(DESTDIR)/etc/postfix
	install -d $(DESTDIR)/var/spool/postfix
	install -d $(DESTDIR)/lib/systemd/system
	install -d -o postfix $(DESTDIR)/var/run/tempmail2
	install -s $(NAME) $(DESTDIR)/usr/sbin/
	install -m 644 postfix-tempmail2.service $(DESTDIR)/lib/systemd/system/
	install -T -m 640 -g postfix conf.dist $(DESTDIR)/etc/postfix/tempmail2.conf

test: $(NAME)
	LANG=C ./$(NAME) --test --noiv -d
	LANG=C ./$(NAME) --test --noiv -d --encoding=base58
test-sok: test
	@(s=$$(./$(NAME) -g test@example.com) ; \
		fs="mail $$s" ; n=$$(echo -n $$fs|wc -c) ; \
		echo "test with 'echo -n \"$$n:$$fs,\" | socat STDIO UNIX:/tmp/sok ; echo'")
	LANG=C ./$(NAME) -c conf.test

submodules:
	git submodule update
