VERSION=$(shell dpkg-parsechangelog -l../../debian/changelog | grep ^Version: | cut -d' ' -f2)
CFLAGS=$(shell pkg-config --cflags gio-2.0 glib-2.0 libcurl) \
		$(shell libgcrypt-config --cflags) \
		-g -I../../lib -I.. -DVERSION=\"$(VERSION)\" -DTEST -DTEST_DIR=\"$(CURDIR)\" -Wall
LIBS=$(shell pkg-config --libs gio-2.0 glib-2.0 libcurl) -lcap \
		$(shell libgcrypt-config --libs)

test_parse_report_SOURCES=test_parse_report.c \
							../whoopsie.c \
							../logging.c \
							../utils.c \
							../identifier.c \
							../../lib/bson/bson.c \
							../../lib/bson/encoding.c \
							../../lib/bson/numbers.c
test_parse_report_EXECUTABLE=test_parse_report
test_parse_report_OBJECTS=$(test_parse_report_SOURCES:.c=.test.o)

test_utils_SOURCES=test_utils.c \
					../utils.c
test_utils_EXECUTABLE=test_utils
test_utils_OBJECTS=$(test_utils_SOURCES:.c=.test.o)

test_monitor_SOURCES=test_monitor.c \
					../monitor.c \
					../logging.c \
					../utils.c
test_monitor_EXECUTABLE=test_monitor
test_monitor_OBJECTS=$(test_monitor_SOURCES:.c=.test.o)

test_identifier_SOURCES=test_identifier.c \
					../identifier.c
test_identifier_EXECUTABLE=test_identifier
test_identifier_OBJECTS=$(test_identifier_SOURCES:.c=.test.o)

test_logging_SOURCES=test_logging.c \
					../logging.c
test_logging_EXECUTABLE=test_logging
test_logging_OBJECTS=$(test_logging_SOURCES:.c=.test.o)

.PHONY: all clean check

all: check

check: $(test_parse_report_EXECUTABLE) $(test_utils_EXECUTABLE) $(test_monitor_EXECUTABLE) $(test_identifier_EXECUTABLE) $(test_logging_EXECUTABLE)
	$(foreach p, $^, ./$p -k;)

$(test_parse_report_EXECUTABLE): $(test_parse_report_OBJECTS)
	$(CC) -std=c99 $^ $(LIBS) -o $@
$(test_utils_EXECUTABLE): $(test_utils_OBJECTS)
	$(CC) -std=c99 $^ $(LIBS) -o $@
$(test_monitor_EXECUTABLE): $(test_monitor_OBJECTS)
	$(CC) -std=c99 $^ $(LIBS) -o $@
$(test_identifier_EXECUTABLE): $(test_identifier_OBJECTS)
	$(CC) -std=c99 $^ $(LIBS) -o $@
$(test_logging_EXECUTABLE): $(test_logging_OBJECTS)
	$(CC) -std=c99 $^ $(LIBS) -o $@

test_parse_report_coverage: $(test_parse_report_OBJECTS:.test.o=.coverage.o)
	$(CC) -std=c99 $^ $(LIBS) -lgcov -o $@
test_utils_coverage: $(test_utils_OBJECTS:.test.o=.coverage.o)
	$(CC) -std=c99 $^ $(LIBS) -lgcov -o $@
test_monitor_coverage: $(test_monitor_OBJECTS:.test.o=.coverage.o)
	$(CC) -std=c99 $^ $(LIBS) -lgcov -o $@
test_identifier_coverage: $(test_identifier_OBJECTS:.test.o=.coverage.o)
	$(CC) -std=c99 $^ $(LIBS) -lgcov -o $@
test_logging_coverage: $(test_logging_OBJECTS:.test.o=.coverage.o)
	$(CC) -std=c99 $^ $(LIBS) -lgcov -o $@
coverage: test_parse_report_coverage test_utils_coverage test_monitor_coverage test_identifier_coverage test_logging_coverage
	$(foreach p, $^, ./$p -k;)
	$(foreach p, $(wildcard ../*.c), gcov $p -o $(p:.c=.coverage.o);)

clean:
	rm -f $(test_parse_report_EXECUTABLE) \
		$(test_parse_report_OBJECTS) \
		$(test_utils_OBJECTS) \
		$(test_utils_EXECUTABLE) \
		$(test_monitor_OBJECTS) \
		$(test_monitor_EXECUTABLE) \
		$(test_identifier_OBJECTS) \
		$(test_identifier_EXECUTABLE) \
		$(test_logging_EXECUTABLE) \
		test_parse_report_coverage \
		test_utils_coverage \
		test_identifier_coverage \
		test_logging_coverage \
		coverage
	find ../.. \( -name '*.coverage.o' -o \
		-name '*.gcda' -o \
		-name '*.gcno' -o \
		-name '*.gcov' \) -delete

%.coverage.o: %.c
	$(CC) -std=c99 -c -o $@ $^ $(CFLAGS) --coverage -O0
%.test.o: %.c
	$(CC) -std=c99 -c -o $@ $^ $(CFLAGS)
