.PHONY: help build run test clean lint lint-fix install release

VERSION ?= $(shell git describe --tags --always --dirty)
LDFLAGS := -ldflags "-s -w -X codeberg.org/romaintb/fgj/cmd.version=$(VERSION)"

TARGETS := \
	linux/386 \
	linux/amd64 \
	linux/arm \
	linux/arm64 \
	darwin/amd64 \
	darwin/arm64 \
	windows/386 \
	windows/amd64 \
	windows/arm64 \
	freebsd/amd64 \
	freebsd/arm64

help:
	@echo "Available commands:"
	@echo "  make build      - Build the application"
	@echo "  make install    - Install the binary to /usr/bin"
	@echo "  make run        - Run the application"
	@echo "  make test       - Run tests"
	@echo "  make lint       - Run golangci-lint"
	@echo "  make lint-fix   - Run golangci-lint with auto-fix"
	@echo "  make clean      - Clean build artifacts"
	@echo "  make release    - Build release binaries for all platforms"

build:
	go build $(LDFLAGS) -o bin/fgj .

release:
	@mkdir -p dist
	@for target in $(TARGETS); do \
		os=$$(echo $$target | cut -d/ -f1); \
		arch=$$(echo $$target | cut -d/ -f2); \
		name=fgj_$${os}_$${arch}; \
		ext=""; \
		[ "$$os" = "windows" ] && ext=".exe"; \
		echo "Building $$name..."; \
		GOOS=$$os GOARCH=$$arch go build $(LDFLAGS) -o dist/$${name}$${ext} . || exit 1; \
	done
	@echo "Done. Binaries in dist/"

install: build
	install -Dm755 bin/fgj /usr/bin/fgj

run:
	go run .

test:
	go test -v -race ./...

lint:
	golangci-lint run ./...

lint-fix:
	golangci-lint run --fix ./...

clean:
	rm -rf bin/
	go clean
