BINARY  := ./bin/applephotos
MODULE  := github.com/einand/applephotos
BRIDGE_DIR := bridge
OBJ := $(BRIDGE_DIR)/photokit_bridge.o
LIB := $(BRIDGE_DIR)/libphotokit_bridge.a
STUB_OBJ := $(BRIDGE_DIR)/photokit_bridge_stub.o
STUB_LIB := $(BRIDGE_DIR)/libphotokit_bridge_stub.a

.PHONY: build clean test coverage

$(LIB): $(OBJ)
	ar rcs $@ $<

$(OBJ): $(BRIDGE_DIR)/photokit_bridge.m $(BRIDGE_DIR)/photokit_bridge.h
	cc -c -x objective-c -fobjc-arc -framework Photos -framework Foundation -o $@ $<

$(STUB_LIB): $(STUB_OBJ)
	ar rcs $@ $<

$(STUB_OBJ): $(BRIDGE_DIR)/photokit_bridge_stub.c $(BRIDGE_DIR)/photokit_bridge.h
	cc -c -o $@ $<

build: $(LIB)
	go build -o $(BINARY) $(MODULE)/cmd/applephotos

test: $(STUB_LIB)
	go test -tags=test -coverprofile=coverage.out -covermode=atomic -coverpkg=./... ./...
	@grep -v 'main_main.go' coverage.out > coverage_filtered.out 2>/dev/null || true
	@mv coverage_filtered.out coverage.out 2>/dev/null || true
	@go tool cover -func=coverage.out | tail -1

coverage: $(STUB_LIB)
	go test -tags=test -coverprofile=coverage.out -covermode=atomic -coverpkg=./... ./...
	@grep -v 'main_main.go' coverage.out > coverage_filtered.out 2>/dev/null || true
	@mv coverage_filtered.out coverage.out 2>/dev/null || true
	go tool cover -func=coverage.out

clean:
	rm -f $(BINARY) $(OBJ) $(LIB) $(STUB_OBJ) $(STUB_LIB) coverage.out