SHELL := /usr/bin/env bash
.DEFAULT_GOAL := help

GO ?= go
GO_TOOL := $(GO) tool
API_DIR := $(abspath ../api)
API_PREPARED ?= 0
# labd runs only on the Linux app host, so lint analyzes Linux code. The
# linter binary is built for the host first; GOOS then selects only the
# analyzed packages.
LINT_GOOS ?= linux
GOLANGCI_LINT := bin/golangci-lint
# Architecture of the labd binary for the app host.
LABD_GOARCH ?= amd64
OGEN_ARGS := -config codegen/ogen.yml --package api --clean ../api/labd/openapi.bundle.yml

##@ Build

.PHONY: build
build: ## Build the labd binary for the Linux app host (LABD_GOARCH, default amd64)
	@mkdir -p bin
	CGO_ENABLED=0 GOOS=linux GOARCH=$(LABD_GOARCH) $(GO) build -buildvcs=false -trimpath -o bin/lincodex-labd ./cmd/labd

.PHONY: deps install
deps: ## Download pinned Go module dependencies
	$(GO) mod download

install: deps ## Install module-pinned Go tools without global changes
	@$(GO) version
	@$(GO_TOOL) ogen --version
	@$(GO_TOOL) golangci-lint version

.PHONY: tidy
tidy: ## Tidy application and tool dependencies
	$(GO) mod tidy

.PHONY: clean
clean: ## Remove local build and test artifacts
	rm -rf bin coverage.out

##@ Code generation

.PHONY: generate gen
generate gen: ## Generate the ogen server from the lab provisioner contract
	@if [ "$(API_PREPARED)" != "1" ]; then $(MAKE) -C "$(API_DIR)" bundle; fi
	$(GO_TOOL) ogen --target internal/api $(OGEN_ARGS)

.PHONY: check-generated
check-generated: ## Check the generated server against the bundled contract without rewriting it
	@set -euo pipefail; mkdir -p bin; temporary_dir=$$(mktemp -d "$(CURDIR)/bin/ogen-check.XXXXXX"); trap 'rm -rf "$$temporary_dir"' EXIT; \
	$(GO_TOOL) ogen --target "$$temporary_dir" $(OGEN_ARGS) >/dev/null 2>&1; \
	diff -r internal/api "$$temporary_dir"

##@ Format, lint, and test

.PHONY: $(GOLANGCI_LINT)
$(GOLANGCI_LINT):
	@mkdir -p bin
	$(GO) build -o $(GOLANGCI_LINT) github.com/golangci/golangci-lint/v2/cmd/golangci-lint

.PHONY: fmt
fmt: $(GOLANGCI_LINT) ## Format Go sources
	GOOS=$(LINT_GOOS) $(GOLANGCI_LINT) fmt ./...

.PHONY: fmt-check
fmt-check: $(GOLANGCI_LINT) ## Check Go formatting without modifying files
	@unformatted=$$(gofmt -l $$(find . -name '*.go' -type f -not -path './bin/*')); \
	if [ -n "$$unformatted" ]; then printf '%s\n' "$$unformatted"; exit 1; fi
	@formatter_diff=$$(GOOS=$(LINT_GOOS) $(GOLANGCI_LINT) fmt --diff ./...); \
	if [ -n "$$formatter_diff" ]; then printf '%s\n' "$$formatter_diff"; exit 1; fi

.PHONY: lint
lint: $(GOLANGCI_LINT) ## Run golangci-lint for Linux
	GOOS=$(LINT_GOOS) $(GOLANGCI_LINT) run ./...

.PHONY: test
test: ## Run unit tests with the race detector against the in-memory LXD fake
	$(GO) test -race -count=1 ./...

.PHONY: test-live
test-live: ## Create and delete one lab on a real LXD (set LABD_LIVE_SOCKET and LABD_LIVE_SOURCE; never the production host)
	@if [ -z "$$LABD_LIVE_SOCKET" ] || [ -z "$$LABD_LIVE_SOURCE" ]; then \
		echo 'set LABD_LIVE_SOCKET and LABD_LIVE_SOURCE (instance/snapshot), optionally LABD_LIVE_PROFILE and LABD_LIVE_PROJECT' >&2; exit 2; \
	fi
	$(GO) test -count=1 -v -run '^TestLiveLXD' ./internal/lxd/

.PHONY: check
check: ## Run formatting, build, unit, generated drift, and lint checks
	$(MAKE) --no-print-directory fmt-check
	$(MAKE) --no-print-directory build
	$(MAKE) --no-print-directory test
	$(MAKE) --no-print-directory check-generated
	$(MAKE) --no-print-directory lint

##@ Help

.PHONY: help
help: ## Show available targets
	@awk 'BEGIN{FS=":.*##"; printf "\nUsage:\n  make -C labd <target>\n"} /^[a-zA-Z0-9_-]+:.*##/ {printf "  %-16s %s\n", $$1, $$2} /^##@/ {printf "\n%s\n", substr($$0, 5)}' $(MAKEFILE_LIST)
