#!/usr/bin/make -f

export DH_VERBOSE=1
export DH_GOPKG := github.com/prometheus/node_exporter

DEBVERS    ?= $(shell dpkg-parsechangelog | sed -n -e 's/^Version: //p')
VERSION    ?= $(shell echo '$(DEBVERS)' | sed 's/^[[:digit:]]*://; s/[-].*//')
DEBPKGNAME ?= $(shell dpkg-parsechangelog | sed -n -e 's/^Source: //p')
BUILDDIR := $(shell perl -w -MDebian::Debhelper::Buildsystem::golang -e \
        'print Debian::Debhelper::Buildsystem::golang->new()->get_builddir()')

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

override_dh_auto_configure:
	dh_auto_configure -O--buildsystem=golang
	# Include vendored dependencies.
	for i in github.com/beevik/ntp github.com/soundcloud/go-runit; do \
            DEST=`dirname $(BUILDDIR)/src/$(DH_GOPKG)/deps/$$i`; \
	    mkdir -p $$DEST; \
	    cp -av deps/$$i $$DEST; \
	done
	# Copy test fixtures.
	cp -av collector/fixtures $(BUILDDIR)/src/$(DH_GOPKG)/collector

override_dh_auto_build:
	dh_auto_build -O--buildsystem=golang -- $(BUILDFLAGS)

override_dh_auto_install:
	dh_auto_install
	# we don't want the golang source (yet?)
	rm -r debian/$(DEBPKGNAME)/usr/share/gocode
	# Rename the binary to match the debian package.
	mv -v debian/$(DEBPKGNAME)/usr/bin/node_exporter \
            debian/$(DEBPKGNAME)/usr/bin/$(DEBPKGNAME)

gen-orig-tgz:
	if [ ! -f ../$(DEBPKGNAME)_$(VERSION).orig.tar.gz ] ; then \
	    git-archive-all --format tar \
                --prefix $(DEBPKGNAME)-$(VERSION)/ -t upstream/$(VERSION) \
                -- - | gzip > ../$(DEBPKGNAME)_$(VERSION).orig.tar.gz ; \
	fi
