VERSION ?= $(shell git describe --tags --always --dirty 2>/dev/null || echo dev)
BUILD_TIME ?= $(shell date -u +%Y-%m-%dT%H:%M:%SZ)
LDFLAGS := -s -w -X main.version=$(VERSION) -X main.buildTime=$(BUILD_TIME) -X main.commit=$(shell git rev-parse --short HEAD 2>/dev/null || echo unknown)

.PHONY: build clean test docker install build-linux build-linux-arm64 build-all

# Build for current platform
build:
	go build -ldflags "$(LDFLAGS)" -tags "with_quic with_utls with_wireguard with_clash_api" -o oo-node ./cmd/oo-node
	go build -ldflags "$(LDFLAGS)" -o ooctl ./cmd/ooctl

# Build for Linux amd64
build-linux:
	CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "$(LDFLAGS)" -tags "with_quic with_utls with_wireguard with_acme with_clash_api" -o oo-node-linux-amd64 ./cmd/oo-node
	CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "$(LDFLAGS)" -o ooctl-linux-amd64 ./cmd/ooctl

# Build for Linux arm64
build-linux-arm64:
	CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -ldflags "$(LDFLAGS)" -tags "with_quic with_utls with_wireguard with_acme with_clash_api" -o oo-node-linux-arm64 ./cmd/oo-node
	CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -ldflags "$(LDFLAGS)" -o ooctl-linux-arm64 ./cmd/ooctl

# Build all platforms
build-all: build-linux build-linux-arm64

# Run tests
test:
	go test -v -race -count=1 ./internal/...

# Clean build artifacts
clean:
	rm -f oo-node ooctl oo-node-linux-* ooctl-linux-*

# Build Docker image
docker:
	docker build -t oo-node:$(VERSION) -t oo-node:latest .

# Install to system (single node, legacy compat)
install: build
	sudo cp oo-node /usr/local/bin/
	sudo cp ooctl /usr/local/bin/
	sudo mkdir -p /etc/oo
	@if [ ! -f /etc/oo/config.yml ]; then \
		sudo cp config.yml.example /etc/oo/config.yml; \
		echo "Config copied to /etc/oo/config.yml - please edit it"; \
	fi
