BIN := ipabusedb-docker-reporter
IMAGE := git.eeqj.de/sneak/$(BIN)
TAG := $(shell date -u +%Y-%m-%d)

# Each arch is built natively on a daemon of that arch: arm64 on the local
# daemon, amd64 on fsn1app1. Override on the command line if needed.
ARCHES := amd64 arm64
DOCKER_HOST_amd64 ?= ssh://root@fsn1app1
DOCKER_HOST_arm64 ?=
docker_for = $(if $(DOCKER_HOST_$(1)),DOCKER_HOST=$(DOCKER_HOST_$(1))) docker

.PHONY: build test lint fmt fmt-check check clean image push manifest \
	$(ARCHES:%=image-%) $(ARCHES:%=push-%)

build:
	go build -o $(BIN) .

# Local image for this machine's arch, for testing.
image:
	docker build -t $(BIN) .

# image-ARCH builds on that arch's daemon; push-ARCH pushes the arch tag.
define arch_rules
image-$(1):
	$$(call docker_for,$(1)) build --platform linux/$(1) -t $$(IMAGE):$(1) .

push-$(1): image-$(1)
	$$(call docker_for,$(1)) push $$(IMAGE):$(1)
endef
$(foreach a,$(ARCHES),$(eval $(call arch_rules,$(a))))

# Join the per-arch tags into the dated and latest multi-arch manifests.
manifest:
	docker buildx imagetools create -t $(IMAGE):$(TAG) -t $(IMAGE):latest \
		$(ARCHES:%=$(IMAGE):%)

push: $(ARCHES:%=push-%) manifest

test:
	go test ./...

fmt:
	gofmt -w *.go

fmt-check:
	@test -z "$$(gofmt -l .)" || { gofmt -l .; echo "gofmt: files need formatting"; exit 1; }

lint: fmt-check
	go vet ./...

check: lint test build

clean:
	rm -f $(BIN) *.csv
