#!/usr/bin/make -f

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

METAPKG := github.com/prometheus/common

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()')
REV        := $(DEBVERS)
BRANCH     := debian/sid
USER       := pkg-go-maintainers@lists.alioth.debian.org
HOSTNAME   := debian
BUILD_DATE := $(shell date --utc --date='$(DEBDATE)' +%Y%m%d-%H:%M:%S)
GO_VERSION := $(shell go version | sed 's/go version \(\S*\).*/\1/')
BUILDFLAGS := -ldflags \
  " -X $(METAPKG)/version.Version=$(VERSION)\
    -X $(METAPKG)/version.Revision=$(REV)\
    -X $(METAPKG)/version.Branch=$(BRANCH)\
    -X $(METAPKG)/version.BuildUser=$(USER)\
    -X $(METAPKG)/version.BuildDate=$(BUILD_DATE)\
    -X $(METAPKG)/version.GoVersion=$(GO_VERSION)"


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

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/blackbox_exporter \
            debian/$(DEBPKGNAME)/usr/bin/$(DEBPKGNAME)
	install -m 644 -D -v blackbox.yml debian/$(DEBPKGNAME)/etc/prometheus/blackbox.yml

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