#
# 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
PROGRAMS = $(TEST_OBJECTS:.lo=)

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@ --silent
CC = @CC@
CFLAGS = @CFLAGS@
CPPFLAGS = @CPPFLAGS@
INCLUDES = -I$(srcdir) -I@APR_INCLUDEDIR@ -I@APU_INCLUDEDIR@ $(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)

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

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

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

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

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


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

install: $(TARGET_LIB)
	$(MKDIR) $(DESTDIR)$(libdir) $(DESTDIR)$(includedir)
	$(LIBTOOL) --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)
	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) --mode=compile $(CC) $(CFLAGS) $(CPPFLAGS) $(INCLUDES) -c -o $@ $< && touch $@
