# Panel — build/test/install entrypoints.
#
# The single source of truth for the build version is
# github.com/dbledeez/panel/pkg/version.Version (defaults to "dev"),
# injected here via -ldflags -X. Override with:  make build VERSION=v1.2.3

VERSION ?= $(shell git describe --tags --always --dirty 2>/dev/null || echo dev)
LDFLAGS  = -s -w -X github.com/dbledeez/panel/pkg/version.Version=$(VERSION)
GOFLAGS  = -trimpath
BIN_DIR ?= bin
PREFIX  ?= /opt/panel

# Windows-friendly: `make build` on Windows produces .exe automatically
# because go respects GOOS; output names stay extension-less here and Go
# appends .exe itself only with -o omitted, so be explicit:
EXE :=
ifeq ($(OS),Windows_NT)
EXE := .exe
endif

.PHONY: all build test vet docker version install-local clean

all: build

build:
	go build $(GOFLAGS) -ldflags "$(LDFLAGS)" -o $(BIN_DIR)/controller$(EXE) ./controller/cmd/controller
	go build $(GOFLAGS) -ldflags "$(LDFLAGS)" -o $(BIN_DIR)/agent$(EXE)      ./agent/cmd/agent
	go build $(GOFLAGS) -ldflags "$(LDFLAGS)" -o $(BIN_DIR)/panelctl$(EXE)   ./controller/cmd/panelctl

test:
	go test ./controller/... ./agent/... ./pkg/...

vet:
	go vet ./controller/... ./agent/... ./pkg/...

# Production container images (see deploy/docker-compose.yml).
docker:
	docker build -f deploy/Dockerfile.controller --build-arg VERSION=$(VERSION) -t panel-controller:$(VERSION) .
	docker build -f deploy/Dockerfile.agent      --build-arg VERSION=$(VERSION) -t panel-agent:$(VERSION) .

version:
	@echo $(VERSION)

# Install binaries + modules + systemd units on the local Linux box.
# Root-only step of install.sh; useful for upgrades from a git checkout.
install-local: build
	install -d $(PREFIX)/bin $(PREFIX)/data
	install -m 0755 $(BIN_DIR)/controller $(BIN_DIR)/agent $(BIN_DIR)/panelctl $(PREFIX)/bin/
	cp -r modules $(PREFIX)/
	install -m 0644 deploy/systemd/panel-controller.service deploy/systemd/panel-agent.service /etc/systemd/system/
	systemctl daemon-reload
	@echo "installed to $(PREFIX); enable with: systemctl enable --now panel-controller panel-agent"

clean:
	rm -rf $(BIN_DIR)
