ROOT := $(shell pwd)
LOCAL_BIN := $(ROOT)/bin
VERSION := $(shell git show-ref --tags -d || git rev-parse --short HEAD)

.PHONY: build
build:
	echo "$(VERSION)" > static/app/dist/version.txt
	go build \
		-ldflags "-X backbone/internal/pkg/version.Version=$(VERSION)" \
		-o $(LOCAL_BIN)/app \
		internal/app/cmd/main.go

.PHONY: deps
proto-deps:
	GOBIN=$(LOCAL_BIN) go install google.golang.org/protobuf/cmd/protoc-gen-go
	GOBIN=$(LOCAL_BIN) go install google.golang.org/grpc/cmd/protoc-gen-go-grpc
	GOBIN=$(LOCAL_BIN) go install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2
	GOBIN=$(LOCAL_BIN) go install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway

.PHONY: proto
proto:
	@PATH=$(PATH):$(LOCAL_BIN) protoc \
		-I . \
		-I pkg/3rdparty \
		--go_out=pkg/pb \
		--go_opt=Mgoogle/api/http.proto=google.golang.org/genproto/googleapis/api/annotations \
		--go-grpc_opt=Mgoogle/api/http.proto=google.golang.org/genproto/googleapis/api/annotations \
		--go_opt=Mgoogle/api/annotations.proto=google.golang.org/genproto/googleapis/api/annotations \
		--go-grpc_opt=Mgoogle/api/annotations.proto=google.golang.org/genproto/googleapis/api/annotations \
		--go_opt=paths=source_relative \
		--go-grpc_out=pkg/pb \
		--go-grpc_opt=paths=source_relative \
		--grpc-gateway_out=pkg/pb \
		--grpc-gateway_opt=Mgoogle/api/http.proto=google.golang.org/genproto/googleapis/api/annotations \
		--grpc-gateway_opt=Mgoogle/api/annotations.proto=google.golang.org/genproto/googleapis/api/annotations \
		--grpc-gateway_opt=paths=source_relative \
		--openapiv2_opt=Mgoogle/api/http.proto=google.golang.org/genproto/googleapis/api/annotations \
		--openapiv2_opt=Mgoogle/api/annotations.proto=google.golang.org/genproto/googleapis/api/annotations \
		--openapiv2_out=logtostderr=true,allow_merge=true,merge_file_name=api:./api \
		api/messages/*.proto \
		api/services/*.proto
