
ifeq "${MP_CONFIG}" ""
MP_CONFIG := $(shell if test -x ../../metaproxy/metaproxy-config; then echo ../../metaproxy/metaproxy-config; else echo metaproxy-config; fi)
endif

VERSION = $(shell grep VERSION ../IDMETA | cut -d '=' -f2)
SHALINE=$(shell if test -d ../.git ; then git log | head -1; else head -1 ../ChangeLog; fi)
SHA=$(shell echo $(SHALINE) | cut -d" " -f2)
VERSIONS= -DCPROXY_VERSION=\"$(VERSION)\" -DCPROXY_SHA=\"$(SHA)\"

#MP_INTERNAL_INCLUDES = -I ../../metaproxy/src
# ### This isn't right, but we need access to some filters for creating a 
# proper filter chain for testing!

MP_CFLAGS := $(shell $(MP_CONFIG) --cflags) -fPIC  -g $(VERSIONS)
# $(MP_INTERNAL_INCLUDES)
MP_LIBS := $(shell $(MP_CONFIG) --libs)

MP_SO = metaproxy_filter_cproxy.so

$(MP_SO): filter_cproxy.cpp
	$(CXX) -shared $(CXXFLAGS) $(MP_CFLAGS) $< -o $(MP_SO) $(MP_LIBS)

TEST1 = test_filter_cproxy
$(TEST1): $(TEST1).cpp $(MP_SO)
	$(CXX)  $(CXXFLAGS) $(MP_CFLAGS) $< -o $(TEST1) $(MP_LIBS) ./$(MP_SO)


clean:
	rm -f $(MP_SO) $(TEST1) core ../test/debug*.log ../test/core
	rm -f ../test/*/core ../test/*/debug*log ../test/*/*~
	rm -rf cf.???.dump cf.???.p cf.???
	rm -f ../*/*~


check: unittest

unittest: $(TEST1)
	rm -rf cf.???.dump cf.???.p cf.???
	./$(TEST1) --test-verbose 2 --test-stop

unittest_valgrind: $(TEST1)
	rm -rf cf.???.dump cf.???.p cf.???
	valgrind --num-callers=20 --leak-check=full --show-reachable=yes --log-file=v ./$(TEST1) --test-verbose 2 --test-stop

install: $(MP_SO)
	mkdir -p $(DESTDIR)/etc/cf-proxy
	cd ../etc/; cp cproxy.cfg cproxyrewrite.xml cproxy.xml cf-proxy.route.xml cf-proxy.port.xml $(DESTDIR)/etc/cf-proxy/
	mkdir -p $(DESTDIR)$(libdir)/cf-proxy
	cp $(MP_SO) $(DESTDIR)$(libdir)/cf-proxy
	rm -rf /tmp/cf.[0-9][0-9]*   # Old session files, wrong owners

dist:
	cd .. ; ./mkdist.sh
