CRYPT_ROOT=..
BACK_END := $(CRYPT_ROOT)/back_end_src
PORTABILITY := $(CRYPT_ROOT)/deps/stlplus/portability
BACK_END_DEPS := portability libencryptmsg state_machine
#TRACE_STATE_MACHINE=on

include ../deps/makefiles/platform.mak
#libencryptmsg
ifneq ($(USE_SYSTEM_LIBS),on)
	BACK_END_DEPS += zlib
	BOTAN_CXXFLAGS=-I../deps/botan/build/include
	OVERRIDE_BOTAN_FLAGS=on
endif

all : back_end
back_end : $(BACK_END_DEPS)
	$(MAKE) -C $(BACK_END) RELEASE=$(RELEASE) USE_SYSTEM_LIBS=$(USE_SYSTEM_LIBS)

libencryptmsg : symlinks
	$(MAKE) -C $(CRYPT_ROOT)/deps/libencryptmsg lib RELEASE=$(RELEASE) BOTAN_CXXFLAGS=$(BOTAN_CXXFLAGS) OVERRIDE_BOTAN_FLAGS=$(OVERRIDE_BOTAN_FLAGS) TRACE_STATE_MACHINE=$(TRACE_STATE_MACHINE)
portability :
	$(MAKE) -C $(PORTABILITY) RELEASE=$(RELEASE)
zlib :
	$(MAKE) -f Makefile.zlib
state_machine:
	$(MAKE) -C $(CRYPT_ROOT)/deps/state_machine RELEASE=$(RELEASE) TRACE_STATE_MACHINE=$(TRACE_STATE_MACHINE)

clean: clean-children | clean-symlinks
clean-children : symlinks
	$(MAKE) -C $(BACK_END) clean RELEASE=$(RELEASE)
	$(MAKE) -C $(PORTABILITY) clean RELEASE=$(RELEASE)
#	$(MAKE) -f Makefile.zlib clean
	$(MAKE) -C $(CRYPT_ROOT)/deps/libencryptmsg clean RELEASE=$(RELEASE)

LIBENCRYPTMSG=$(CRYPT_ROOT)/deps/libencryptmsg
LNDIRNAME=makefiles stlplus gtest botan plog state_machine
LNDIR=$(LNDIRNAME:%=$(LIBENCRYPTMSG)/deps/%)
CLEANLNDIR=$(LNDIR:%=clean-%)

.PHONY: $(CLEANLNDIR)

symlinks: $(LNDIR)

clean-symlinks: $(CLEANLNDIR)

$(LNDIR):
ifneq ($(WINDOWS),on)
	ln -s ../../$(@:$(LIBENCRYPTMSG)/deps/%=%) $(LIBENCRYPTMSG)/deps/
else
	cp -r ../deps/$(@:$(LIBENCRYPTMSG)/deps/%=%) $(LIBENCRYPTMSG)/deps/
endif

$(CLEANLNDIR):
	rm -r $(@:clean-%=%)
