#
# Makefile for Serf
#
srcdir = @srcdir@
VPATH = @srcdir@

SERF_MAJOR_VERSION=@SERF_MAJOR_VERSION@
SERF_DOTTED_VERSION=@SERF_DOTTED_VERSION@

OBJECTS = buckets/aggregate_buckets.lo buckets/request_buckets.lo context.lo \
          buckets/buckets.lo buckets/simple_buckets.lo buckets/file_buckets.lo \
          buckets/mmap_buckets.lo buckets/socket_buckets.lo \
          buckets/response_buckets.lo buckets/headers_buckets.lo \
          buckets/allocator.lo buckets/dechunk_buckets.lo \
          buckets/deflate_buckets.lo buckets/limit_buckets.lo \
          buckets/ssl_buckets.lo buckets/barrier_buckets.lo \
          buckets/chunk_buckets.lo

TARGET_LIB=libserf-$(SERF_MAJOR_VERSION).la

TEST_OBJECTS = test/serf_get.lo test/serf_response.lo test/serf_request.lo \
               test/serf_spider.lo

TEST_SUITE_OBJECTS = test/CuTest.lo test/test_all.lo test/test_util.lo \
                     test/test_buckets.lo test/test_context.lo \
                     test/test_ssl.lo 

PROGRAMS = $(TEST_OBJECTS:.lo=) test/test_all

TESTCASES = test/testcases/simple.response \
  test/testcases/chunked-empty.response test/testcases/chunked.response \
  test/testcases/chunked-trailers.response \
  test/testcases/deflate.response

HEADERS = serf.h serf_bucket_types.h serf_bucket_util.h serf_declare.h

prefix=@prefix@
exec_prefix=@exec_prefix@
libdir=@libdir@
includedir=@includedir@

LIBTOOL = @APR_LIBTOOL@
LTFLAGS = @LTFLAGS@
CC = @CC@
CFLAGS = @CFLAGS@
CPPFLAGS = @CPPFLAGS@
INCLUDES = -I$(srcdir) @APR_INCLUDES@ @APU_INCLUDES@ $(EXTRA_INCLUDES)
MKDIR = @mkdir_p@
INSTALL = @INSTALL@

LDFLAGS = @LDFLAGS@
LIBS = @SERF_LIBS@ -lz -lssl -lcrypto

all: $(TARGET_LIB) $(PROGRAMS)

context.lo: context.c $(HEADERS)
buckets/aggregate_buckets.lo: buckets/aggregate_buckets.c $(HEADERS)
buckets/request_buckets.lo: buckets/request_buckets.c $(HEADERS)
buckets/buckets.lo: buckets/buckets.c $(HEADERS)
buckets/simple_buckets.lo: buckets/simple_buckets.c $(HEADERS)
buckets/file_buckets.lo: buckets/file_buckets.c $(HEADERS)
buckets/mmap_buckets.lo: buckets/mmap_buckets.c $(HEADERS)
buckets/socket_buckets.lo: buckets/socket_buckets.c $(HEADERS)
buckets/response_buckets.lo: buckets/response_buckets.c $(HEADERS)
buckets/headers_buckets.lo: buckets/headers_buckets.c $(HEADERS)
buckets/allocator.lo: buckets/allocator.c $(HEADERS)
buckets/dechunk_buckets.lo: buckets/dechunk_buckets.c $(HEADERS)
buckets/deflate_buckets.lo: buckets/deflate_buckets.c $(HEADERS)
buckets/limit_buckets.lo: buckets/limit_buckets.c $(HEADERS)
buckets/ssl_buckets.lo: buckets/ssl_buckets.c $(HEADERS)
buckets/barrier_buckets.lo: buckets/barrier_buckets.c $(HEADERS)
buckets/chunk_buckets.lo: buckets/chunk_buckets.c $(HEADERS)

test/serf_get.lo: test/serf_get.c $(HEADERS)
test/serf_response.lo: test/serf_response.c $(HEADERS)
test/serf_request.lo: test/serf_request.c $(HEADERS)
test/serf_spider.lo: test/serf_spider.c $(HEADERS)

test/CuTest.lo: test/CuTest.c $(HEADERS)
test/test_all.lo: test/test_all.c $(HEADERS)
test/test_util.lo: test/test_util.c $(HEADERS)
test/test_buckets.lo: test/test_buckets.c $(HEADERS)
test/test_context.lo: test/test_context.c $(HEADERS)
test/test_ssl.lo: test/test_ssl.c $(HEADERS)

$(TARGET_LIB): $(OBJECTS)
	$(LIBTOOL) $(LTFLAGS) --mode=link $(CC) $(LDFLAGS) -rpath $(libdir) -o $@ $^ $(LIBS)

test/serf_get: $(TARGET_LIB) test/serf_get.lo
	$(LIBTOOL) $(LTFLAGS) --mode=link $(CC) $(LDFLAGS) -static -o $@ $^ $(LIBS)

test/serf_response: $(TARGET_LIB) test/serf_response.lo
	$(LIBTOOL) $(LTFLAGS) --mode=link $(CC) $(LDFLAGS) -static -o $@ $^ $(LIBS)

test/serf_request: $(TARGET_LIB) test/serf_request.lo
	$(LIBTOOL) $(LTFLAGS) --mode=link $(CC) $(LDFLAGS) -static -o $@ $^ $(LIBS)

test/serf_spider: $(TARGET_LIB) test/serf_spider.lo
	$(LIBTOOL) $(LTFLAGS) --mode=link $(CC) $(LDFLAGS) -static -o $@ $^ $(LIBS)

test/test_all: $(TARGET_LIB) $(TEST_SUITE_OBJECTS)
	$(LIBTOOL) $(LTFLAGS) --mode=link $(CC) $(LDFLAGS) -static -o $@ $^ $(LIBS)

check: test/serf_response test/test_all
	@for i in $(TESTCASES); \
		 do echo "== Testing $$i =="; \
		 ./test/serf_response $$i; \
	done;
	@echo "== Running test_all ==";
	@./test/test_all

install: $(TARGET_LIB)
	$(MKDIR) $(DESTDIR)$(libdir) $(DESTDIR)$(includedir)
	$(LIBTOOL) $(LTFLAGS) --mode=install $(INSTALL) -m 644 $(TARGET_LIB) $(DESTDIR)$(libdir)
	for i in $(srcdir)/*.h; do \
		$(INSTALL) -m 644 $$i $(DESTDIR)$(includedir); \
	done

clean:
	rm -f $(TARGET_LIB) $(OBJECTS) $(OBJECTS:.lo=.o) $(PROGRAMS) $(TEST_OBJECTS) $(TEST_OBJECTS:.lo=.o) $(TEST_SUITE_OBJECTS) $(TEST_SUITE_OBJECTS:.lo=.o)
	for subdir in . buckets test; do \
		(cd $$subdir && rm -rf .libs) ; \
	done

distclean: clean
	rm -f Makefile config.log config.status

.SUFFIXES:
.SUFFIXES: .c .lo .o

mkdir-vpath:
	@if [ ! -d buckets ]; then \
		$(MKDIR) buckets; \
	fi;
	@if [ ! -d test ]; then \
		$(MKDIR) test; \
	fi;

.c.lo:
	$(LIBTOOL) $(LTFLAGS) --mode=compile $(CC) $(CFLAGS) $(CPPFLAGS) $(INCLUDES) -c -o $@ $< && touch $@
