GO := $(shell command -v go 2>/dev/null || echo /usr/local/go/bin/go)
BIN_DIR=bin

DAEMON=$(BIN_DIR)/daemon
CLI=$(BIN_DIR)/cli

PROTO_DIR=proto
PROTO_OUT=internal/grpc/pb

.PHONY: all build clean proto daemon cli daemon-arm64

all: build

# =========================
# BUILD
# =========================

build: daemon cli daemon-arm64

daemon:
	@echo "Building daemon..."
	@mkdir -p $(BIN_DIR)
	@$(GO) build -o $(DAEMON) ./cmd/daemon

cli:
	@echo "Building cli..."
	@mkdir -p $(BIN_DIR)
	@$(GO) build -o $(CLI) ./cmd/cli

daemon-arm64:
	@echo "Building daemon for linux/arm64..."
	@mkdir -p $(BIN_DIR)
	@GOOS=linux GOARCH=arm64 CGO_ENABLED=0 $(GO) build -o $(BIN_DIR)/daemon-arm64 ./cmd/daemon


# =========================
# PROTO
# =========================

proto:
	@echo "Generating protobuf..."
	@mkdir -p $(PROTO_OUT)
	@protoc \
		--go_out=. \
		--go-grpc_out=. \
		-I $(PROTO_DIR) \
		$(PROTO_DIR)/*.proto


# =========================
# CLEAN
# =========================

clean:
	@echo "Cleaning binaries..."
	@rm -rf $(BIN_DIR)/*