BINARY  := vpnportal
PKG     := git.ravensburg.dev/cabele/opnsense-portal/cmd/vpnportal
VERSION ?= $(shell git describe --tags --always --dirty 2>/dev/null || echo dev)
COMMIT  ?= $(shell git rev-parse --short HEAD 2>/dev/null || echo none)
DATE    ?= $(shell date -u +%Y-%m-%dT%H:%M:%SZ)
LDFLAGS := -s -w -X main.version=$(VERSION) -X main.commit=$(COMMIT) -X main.date=$(DATE)

# sha256sum gibt es auf macOS nicht; shasum ist dort das Äquivalent.
SHA256 := $(shell command -v sha256sum >/dev/null 2>&1 && echo sha256sum || echo "shasum -a 256")

export CGO_ENABLED := 0

.PHONY: all build test race vet lint release clean

all: vet test build

build:
	go build -trimpath -ldflags '$(LDFLAGS)' -o dist/$(BINARY) $(PKG)

test:
	go test ./...

race:
	go test -race ./...

vet:
	go vet ./...

lint: vet
	@test -z "$$(gofmt -l . | tee /dev/stderr)" || (echo "gofmt-Fehler: bitte 'gofmt -w .' ausführen" && exit 1)

release: clean
	@set -e; for arch in amd64 arm64; do \
		echo "==> linux/$$arch"; \
		mkdir -p dist/linux-$$arch; \
		GOOS=linux GOARCH=$$arch go build -trimpath -ldflags '$(LDFLAGS)' \
			-o dist/linux-$$arch/$(BINARY) $(PKG); \
		cp deploy/config.example.yaml deploy/vpnportal.service README.md dist/linux-$$arch/; \
		tar -czf dist/$(BINARY)-$(VERSION)-linux-$$arch.tar.gz -C dist/linux-$$arch .; \
	done
	cd dist && $(SHA256) *.tar.gz > SHA256SUMS

clean:
	rm -rf dist
