#!/usr/bin/make -f

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

export DH_GOPKG := google.golang.org/grpc
export DH_GOLANG_EXCLUDES := \
    benchmark examples naming/etcd test stress \
    interop/client interop/server
export DH_GOLANG_INSTALL_EXTRA := testdata transport/testdata

%:
	dh $@ --buildsystem=golang --with=golang --parallel

PB_GO_FILES = $(patsubst %.proto, %.pb.go, $(wildcard */*/*.proto))
$(PB_GO_FILES):
	protoc -I$(dir $@) --go_out=plugins=grpc:$(dir $@) $(dir $@)*.proto

override_dh_clean:
	dh_clean $(PB_GO_FILES)

override_dh_auto_configure: $(PB_GO_FILES)
	dh_auto_configure

## http://wiki.debian.org/onlyjob/get-orig-source
.PHONY: get-orig-source
PKD   = $(abspath $(dir $(MAKEFILE_LIST)))
PKG   = $(word 2,$(shell dpkg-parsechangelog -l$(PKD)/changelog | grep ^Source))
VER  ?= $(shell dpkg-parsechangelog -l$(PKD)/changelog -SVersion | perl -ne 'print $$1 if m{^(?:\d+:)?(\d.*)(?:\-\d+.*)};')
REV   = $(shell sh -c 'printf "$${1\#\#*.0.}"' -- "$(VER)")

get-orig-source: $(PKG)_$(VER).orig.tar.gz $(info I: $(PKG)_$(VER))
	@

UURL = https://github.com/grpc/grpc-go
$(PKG)_$(VER).orig.tar.gz:
	$(if $(wildcard $(PKG)-$(VER)),$(error $(PKG)-$(VER) exist, aborting..))
	@echo "# Downloading..."
	wget --tries=3 --timeout=40 --read-timeout=40 --continue -O $@ \
          $(UURL)/archive/$(REV).tar.gz \
        || uscan --noconf --verbose --rename --destdir=$(CURDIR) --check-dirname-level=0 --force-download --download-current-version $(PKD)
