CURDIR=$(shell pwd)
BINDIR=${CURDIR}/bin

# Устанавливаем proto описания google/protobuf
vendor-proto/google/protobuf:
	git clone -b main --single-branch -n --depth=1 \
		https://github.com/protocolbuffers/protobuf vendor-proto/protobuf &&\
	cd vendor-proto/protobuf &&\
	git sparse-checkout set --no-cone src/google/protobuf &&\
	git checkout
	mkdir -p vendor-proto/google
	mv vendor-proto/protobuf/src/google/protobuf vendor-proto/google
	rm -rf vendor-proto/protobuf

# Устанавливаем proto описания validate
vendor-proto/validate:
	git clone -b main --single-branch --depth=2 \
		https://github.com/bufbuild/protoc-gen-validate vendor-proto/tmp && \
		cd vendor-proto/tmp && \
		git sparse-checkout set --no-cone validate &&\
		git checkout
		mkdir -p vendor-proto/validate
		mv vendor-proto/tmp/validate vendor-proto/
		rm -rf vendor-proto/tmp

# Устанавливаем proto описания google/googleapis
vendor-proto/google/api:
	git clone -b master --single-branch -n --depth=1 \
 		https://github.com/googleapis/googleapis vendor-proto/googleapis && \
 	cd vendor-proto/googleapis && \
	git sparse-checkout set --no-cone google/api && \
	git checkout
	mkdir -p  vendor-proto/google
	mv vendor-proto/googleapis/google/api vendor-proto/google
	rm -rf vendor-proto/googleapis

# Устанавливаем proto описания protoc-gen-openapiv2/options
vendor-proto/protoc-gen-openapiv2/options:
	git clone -b main --single-branch -n --depth=1 \
 		https://github.com/grpc-ecosystem/grpc-gateway vendor-proto/grpc-ecosystem && \
 	cd vendor-proto/grpc-ecosystem && \
	git sparse-checkout set --no-cone protoc-gen-openapiv2/options && \
	git checkout
	mkdir -p vendor-proto/protoc-gen-openapiv2
	mv vendor-proto/grpc-ecosystem/protoc-gen-openapiv2/options vendor-proto/protoc-gen-openapiv2
	rm -rf vendor-proto/grpc-ecosystem

.PHONY: .vendor-rm
.vendor-rm:
	rm -rf vendor-proto

.vendor-proto: .vendor-rm vendor-proto/google/protobuf vendor-proto/validate vendor-proto/google/api vendor-proto/protoc-gen-openapiv2/options

define generate
	@if [ -f "$(1)/go.mod" ]; then \
		echo "===== running go generate for $(1) ====="; \
		if [ ! -d "$(1)/internal" ]; then \
			echo "no internal package in $(1)"; \
			exit 0; \
		fi; \
		PATH=$(PATH):$(BINDIR) go generate ./$(1)/internal/...; \
	fi
endef

define proto_gen
	@echo "== generating $(1) =="
	@protoc \
		-I api \
		-I vendor-proto \
		--plugin=protoc-gen-go=$(BINDIR)/protoc-gen-go \
		--go_out=pkg/api/ --go_opt paths=source_relative \
		--plugin=protoc-gen-go-grpc=$(BINDIR)/protoc-gen-go-grpc \
		--go-grpc_out=pkg/api/ --go-grpc_opt paths=source_relative \
		--plugin=protoc-gen-validate=$(BINDIR)/protoc-gen-validate \
		--validate_out="lang=go,paths=source_relative:pkg/api/" \
		--plugin=protoc-gen-grpc-gateway=$(BINDIR)/protoc-gen-grpc-gateway \
		--grpc-gateway_out=pkg/api/ \
		--grpc-gateway_opt=logtostderr=true,paths=source_relative,generate_unbound_methods=true \
		--plugin=protoc-gen-openapiv2=$(BINDIR)/protoc-gen-openapiv2 \
		--openapiv2_out=api/openapiv2 \
		--openapiv2_opt=logtostderr=true \
		$(1)
endef

cart-generate:
	$(call generate,cart)
	cd cart && go mod tidy

loms-generate:
	$(call generate,loms)
	mkdir -p "api/openapiv2"
	$(foreach f,$(shell find api/loms/v1 -type f -name '*.proto'),$(call proto_gen,$(f),loms))
	cd loms && go mod tidy

notifier-generate:
	$(call generate,notifier)

comments-generate:
	$(call generate,comments)
