#!/usr/bin/make -f

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

export DH_OPTIONS
export DH_GOPKG := github.com/coreos/etcd
export DH_GOLANG_EXCLUDES := github.com/coreos/etcd/Godeps etcdctlv3 tools/v3benchmark tools/benchmark clientv3 contrib integration tools e2e raft/rafttest

%:
	dh $@ --buildsystem=golang --with=golang,systemd --parallel --builddirectory=_build

override_dh_clean:
	dh_clean
	## Remove Files-Excluded (when built from checkout or non-DFSG tarball):
	$(RM) -rv `perl -0nE 'say $$1 if m{^Files\-Excluded\:\s*(.*?)(?:\n\n|Files:|Comment:)}sm;' debian/copyright`

PB_GO_FILES = $(patsubst %.proto, %.pb.go, $(wildcard */*/*.proto))
$(PB_GO_FILES):
	grep -Pq "syntax\s*=\s*\"proto3\"" $(@:.pb.go=.proto) \
        || protoc -I/usr/share/gocode/src/github.com/gogo/protobuf -I/usr/include -I$(dir $@) --gogo_out=$(dir $@) $(@:.pb.go=.proto)
	perl -pi -E 's{import _ "gogoproto"}{}g;' "$@"

override_dh_auto_configure: $(PB_GO_FILES)
	dh_auto_configure
	for I in $$(cat debian/third_party_exclude); do \
            printf "Removing third party bundled $$I\n" ;\
            sed -i "s,github.com/coreos/etcd/Godeps/_workspace/src/$I,$I," $$(find _build -type f -name "*.go") ;\
        done

override_dh_auto_test:
	GOMAXPROCS=2 PATH="$(CURDIR)/_build/bin:$$PATH" dh_auto_test

override_dh_auto_clean:
	dh_auto_clean
	rm -rf bin/
	rm -rf gopath/
