# Makefile for vmbackupmanager

# Build variables
BINARY_NAME := vmbackupmanager
VERSION ?= $(shell git describe --tags --always --dirty 2>/dev/null || echo "unknown")
BUILD_TIME := $(shell date -u +"%Y-%m-%dT%H:%M:%SZ")
COMMIT_SHA := $(shell git rev-parse --short HEAD 2>/dev/null || echo "unknown")
GOFLAGS := -trimpath

# Docker variables
DOCKER_REGISTRY ?= 552185537300.dkr.ecr.us-west-2.amazonaws.com
DOCKER_REPO ?= observability/vmbackupmanager
DOCKER_TAG ?= $(VERSION)
DOCKER_IMAGE ?= $(DOCKER_REGISTRY)/$(DOCKER_REPO):$(DOCKER_TAG)
DOCKER_PLATFORMS ?= linux/amd64,linux/arm64
PACKAGE_LIST  := $(shell go list ./...| grep -vE "version")

# Go variables
GO := go
GOPATH := $(shell $(GO) env GOPATH)
GOARCH ?= $(shell $(GO) env GOARCH)
GOOS ?= $(shell $(GO) env GOOS)

# Linting variables
GOLANGCI_LINT := $(GOPATH)/bin/golangci-lint
GOLANGCI_LINT_VERSION ?= v2.1.6

# Build flags
LDFLAGS := -w -s \
	-X "github.com/tidbcloud/observability/vmbackupmanager/pkg/version.Version=$(VERSION)" \
	-X "github.com/tidbcloud/observability/vmbackupmanager/pkg/version.BuildTime=$(BUILD_TIME)" \
	-X "github.com/tidbcloud/observability/vmbackupmanager/pkg/version.GitCommit=$(COMMIT_SHA)"

.PHONY: all build clean lint test docker docker-push help build-for-docker build-multi-arch

all: build

# Build the binary using the current system's GOOS/GOARCH
build: tidy lint
	CGO_ENABLED=0 GOOS=$(GOOS) GOARCH=$(GOARCH) $(GO) build $(GOFLAGS) -ldflags '$(LDFLAGS)' -o bin/$(BINARY_NAME) ./main.go

# Build specifically for linux/amd64 (for Docker)
build-for-docker: lint
	CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(GO) build $(GOFLAGS) -ldflags '$(LDFLAGS)' -o bin/$(BINARY_NAME)-linux-amd64 ./main.go

# Build for multiple architectures
build-multi-arch: lint
	@echo "Building for multiple architectures..."
	CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(GO) build $(GOFLAGS) -ldflags '$(LDFLAGS)' -o bin/$(BINARY_NAME)-linux-amd64 ./main.go
	CGO_ENABLED=0 GOOS=linux GOARCH=arm64 $(GO) build $(GOFLAGS) -ldflags '$(LDFLAGS)' -o bin/$(BINARY_NAME)-linux-arm64 ./main.go

# Clean build artifacts
clean:
	rm -rf bin/
	rm -f $(BINARY_NAME)

# Install golangci-lint if not installed or version is not correct
# Run linting
lint:
	@if [ ! -f "$(GOLANGCI_LINT)" ] || [ "$$($(GOLANGCI_LINT) --version | grep -o 'version [0-9.]*' | cut -d' ' -f2)" != "$(GOLANGCI_LINT_VERSION:v%=%)" ]; then \
		echo "Installing golangci-lint $(GOLANGCI_LINT_VERSION)..."; \
		curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOPATH)/bin $(GOLANGCI_LINT_VERSION); \
	fi
	$(GOLANGCI_LINT) run --fix ./... --config .golangci.yml

# Run tests
test:
	$(GO) test -race -coverprofile=coverage.txt -covermode=atomic $(PACKAGE_LIST) -skip=./pkg/storage/localfs.go

# Build Docker image using locally built linux/amd64 binary
docker: build-for-docker
	docker build --platform linux/amd64 -t $(DOCKER_IMAGE) -f Dockerfile .

# Build multi-platform Docker image using Docker buildx
docker-multi-platform: build-multi-arch
	GOWORK=off docker buildx build --platform $(DOCKER_PLATFORMS) \
		-t $(DOCKER_IMAGE) -f Dockerfile .

# Push Docker image
docker-push: docker
	docker push $(DOCKER_IMAGE)

# Push multi-platform Docker image
docker-push-multi-platform: build-multi-arch
	GOWORK=off docker buildx build --platform $(DOCKER_PLATFORMS) \
		-t $(DOCKER_IMAGE) --push -f Dockerfile .

# Run Go mod tidy
tidy:
	$(GO) mod tidy

# Run the application
run:
	$(GO) run ./main.go

# Show help
help:
	@echo "Make targets:"
	@echo "  build                     - Build the binary for current platform"
	@echo "  build-for-docker          - Build the binary for linux/amd64 platform"
	@echo "  build-multi-arch          - Build binaries for multiple architectures"
	@echo "  clean                     - Clean build artifacts"
	@echo "  lint                      - Run golangci-lint"
	@echo "  test                      - Run tests"
	@echo "  docker                    - Build Docker image using linux/amd64 binary"
	@echo "  docker-multi-platform     - Build multi-platform Docker image"
	@echo "  docker-push               - Build and push Docker image"
	@echo "  docker-push-multi-platform - Build and push multi-platform Docker image"
	@echo "  tidy                      - Run go mod tidy"
	@echo "  run                       - Run the application locally"
	@echo "  help                      - Show this help"
