ABRA         := ./cmd/abra
KADABRA      := ./cmd/kadabra
COMMIT       := $(shell git rev-list -1 HEAD)
GOPATH       := $(shell go env GOPATH)
GOVERSION    := 1.22
LDFLAGS      := "-X 'main.Commit=$(COMMIT)'"
DIST_LDFLAGS := $(LDFLAGS)" -s -w"
GCFLAGS      := "all=-l -B"

export GOPRIVATE=coopcloud.tech

# NOTE(d1): default `make` optimised for Abra hacking
all: format check build-abra test

run-abra:
	@go run -gcflags=$(GCFLAGS) -ldflags=$(LDFLAGS) $(ABRA)

run-kadabra:
	@go run -gcflags=$(GCFLAGS) -ldflags=$(LDFLAGS) $(KADABRA)

install-abra:
	@go install -gcflags=$(GCFLAGS) -ldflags=$(LDFLAGS) $(ABRA)

install-kadabra:
	@go install -gcflags=$(GCFLAGS) -ldflags=$(LDFLAGS) $(KADABRA)

install: install-abra install-kadabra

build-abra:
	@go build -v -gcflags=$(GCFLAGS) -ldflags=$(DIST_LDFLAGS) $(ABRA)

build-kadabra:
	@go build -v -gcflags=$(GCFLAGS) -ldflags=$(DIST_LDFLAGS) $(KADABRA)

build: build-abra build-kadabra

build-docker-abra:
	@docker run -it -v $(PWD):/abra golang:$(GOVERSION) \
		bash -c 'cd /abra; ./scripts/docker/build.sh'

build-docker: build-docker-abra

clean:
	@rm '$(GOPATH)/bin/abra'
	@rm '$(GOPATH)/bin/kadabra'

format:
	@gofmt -s -w $$(find . -type f -name '*.go' | grep -v "/vendor/")

check:
	@test -z $$(gofmt -l $$(find . -type f -name '*.go' | grep -v "/vendor/")) || \
	(echo "gofmt: formatting issue - run 'make format' to resolve" && exit 1)

test:
	@go test ./... -cover -v

loc:
	@find . -name "*.go" | xargs wc -l

deps:
	@go get -t -u ./...
