BINARIES := davicald davical-test davical-admin davical-cron davical-cfg2env

# Version stamped into the binaries (-version), from the nearest git tag with the
# leading "v" stripped (v1.0.1-9-gd20af82 -> 1.0.1-9-gd20af82).
VERSION ?= $(patsubst v%,%,$(shell git describe --tags --always --dirty 2>/dev/null))
LDFLAGS := -X gitlab.com/davical-project/davical-go/internal/version.Version=$(VERSION)

.PHONY: all clean test test-setup

all: $(BINARIES)

davicald: $(shell find cmd/davicald internal -name '*.go')
	go build -ldflags "$(LDFLAGS)" -o $@ ./cmd/davicald

davical-test: $(shell find cmd/davical-test internal pkg -name '*.go')
	go build -ldflags "$(LDFLAGS)" -o $@ ./cmd/davical-test

davical-admin: $(shell find cmd/davical-admin internal dba -name '*.go')
	go build -ldflags "$(LDFLAGS)" -o $@ ./cmd/davical-admin

davical-cron: $(shell find cmd/davical-cron internal -name '*.go')
	go build -ldflags "$(LDFLAGS)" -o $@ ./cmd/davical-cron

davical-cfg2env: $(shell find cmd/davical-cfg2env internal -name '*.go')
	go build -ldflags "$(LDFLAGS)" -o $@ ./cmd/davical-cfg2env

test-setup: davical-test
	./davical-test setup --dsn "postgres://localhost:5433/regression"

FILTER ?=
test: davical-test
	./davical-test run -dsn "postgres://localhost:5433/regression" -verbose -fail-fast 1 $(if $(FILTER),--filter "$(FILTER)") 
# Note: 'make test' without FILTER auto-runs setup

clean:
	rm -f $(BINARIES)
