#!/usr/bin/make -f

# Common variables
REPO ?= $(shell go list -m)
VERSION ?= $(shell git describe --tags --dirty --match "v*" --always --abbrev=8 2>/dev/null || cat VERSION 2>/dev/null || echo "develop")
GO_VERSION ?= 1.20
LINT_VERSION ?= 1.54.0
BINDIR = bin


# Binaries to build
CMDS = $(addprefix s3-tests-, $(notdir $(wildcard cmd/*)))
BINS = $(addprefix $(BINDIR)/, $(CMDS))

# Variables for docker
REPO_BASENAME = $(shell basename `go list -m`)
HUB_IMAGE ?= "truecloudlab/$(REPO_BASENAME)"
HUB_TAG ?= "$(shell echo ${VERSION} | sed 's/^v//')"

# Make all binaries
.PHONY: all
all: $(BINS)

.PHONY: $(BINS)
$(BINS): $(BINDIR) dep
	@echo "⇒ Build $@"
	CGO_ENABLED=0 \
	go build -v -trimpath \
	-ldflags "-X $(REPO)/internal/version.Version=$(VERSION)" \
	-o $@ ./cmd/$(subst s3-tests-,,$(notdir $@))


.PHONY: $(BINDIR)
$(BINDIR):
	@echo "⇒ Ensure dir: $@"
	@mkdir -p $@

# Pull go dependencies
.PHONY: dep
dep:
	@printf "⇒ Download requirements: "
	@CGO_ENABLED=0 \
	go mod download && echo OK
	@printf "⇒ Tidy requirements: "
	@CGO_ENABLED=0 \
	go mod tidy -v && echo OK

# Run `make %` in Golang container, for more information run `make help.docker/%`
.PHONY: docker/
docker/%:
	$(if $(filter $*,all $(BINS)), \
		@echo "=> Running 'make $*' in clean Docker environment" && \
		docker run --rm -t \
		  -v `pwd`:/src \
		  -w /src \
		  -u `stat -c "%u:%g" .` \
		  --env HOME=/src \
		  golang:$(GO_VERSION) make $*,\
	  	@echo "supported docker targets: all $(BINS) lint")

# Run tests
.PHONY: test
test:
	@go test ./... -cover

# Run tests with race detection and produce coverage output
.PHONY: cover
cover:
	@go test -v -race ./... -coverprofile=coverage.txt -covermode=atomic
	@go tool cover -html=coverage.txt -o coverage.html

# Reformat code
.PHONY: format
format:
	@echo "⇒ Processing gofmt check"
	@gofmt -s -w ./

# Build clean Docker image
.PHONY: image
image:
	@echo "⇒ Build S3 Tests Parser docker image "
	@docker build \
		--build-arg REPO=$(REPO) \
		--build-arg VERSION=$(VERSION) \
		--rm \
		-f .docker/Dockerfile \
		-t $(HUB_IMAGE):$(HUB_TAG) .

# Push Docker image to the hub
.PHONY: image-push
image-push:
	@echo "⇒ Publish image"
	@docker push $(HUB_IMAGE):$(HUB_TAG)

# Build dirty Docker image
.PHONY: dirty-image
dirty-image:
	@echo "⇒ Build S3 Tests Parser image"
	@docker build \
		--build-arg REPO=$(REPO) \
		--build-arg VERSION=$(VERSION) \
		--rm \
		-f .docker/Dockerfile.dirty \
		-t $(HUB_IMAGE)-dirty:$(HUB_TAG) .

# Run linters
.PHONY: lint
lint:
	@golangci-lint --timeout=5m run

# Run linters in Docker
.PHONY: docker/lint
docker/lint:
	docker run --rm -it \
	-v `pwd`:/src \
	-u `stat -c "%u:%g" .` \
	--env HOME=/src \
	golangci/golangci-lint:v$(LINT_VERSION) bash -c 'cd /src/ && make lint'

# Activate pre-commit hooks
.PHONY: pre-commit
pre-commit:
	pre-commit install -t pre-commit -t commit-msg

# Deactivate pre-commit hooks
.PHONY: unpre-commit
unpre-commit:
	pre-commit uninstall -t pre-commit -t commit-msg

# Show current version
.PHONY: version
version:
	@echo $(VERSION)

# Clean up files
.PHONY: clean
clean:
	rm -rf .cache
	rm -rf $(BINDIR)