#!/usr/bin/make -f
include /usr/share/dpkg/pkg-info.mk

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

BUILDDIR := $(CURDIR)/build
METAPKG := github.com/prometheus/common

BRANCH     := debian/sid
USER       := pkg-go-maintainers@lists.alioth.debian.org
BUILD_DATE := $(shell date --utc --date='@$(SOURCE_DATE_EPOCH)' \
    +%Y%m%d-%H:%M:%S)
GO_VERSION := $(shell go version | sed 's/go version \(\S*\).*/\1/')
BUILDTAGS  :=
BUILDFLAGS  = -tags "$(BUILDTAGS)" -ldflags \
  " -X $(METAPKG)/version.Version=$(DEB_VERSION_UPSTREAM)\
    -X $(METAPKG)/version.Revision=$(DEB_VERSION)\
    -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 \
            --builddirectory=$(BUILDDIR)

# ZFS (or its tests) are not working in 32-bit arches.
DEB_HOST_ARCH_BITS := $(shell dpkg-architecture -qDEB_HOST_ARCH_BITS)
ifeq ($(DEB_HOST_ARCH_BITS),32)
BUILDTAGS += nozfs
endif

BINNAME := $(DEB_SOURCE)
WHATIS := "$(BINNAME) \\- Prometheus exporter for machine metrics"

override_dh_auto_build:
	dh_auto_build -- $(BUILDFLAGS)
	# Rename the binary to match the debian package.
	cp -v $(BUILDDIR)/bin/node_exporter $(BUILDDIR)/bin/$(BINNAME)
	build/bin/$(BINNAME) --help-man > build/$(BINNAME).1
	# Remove build user/build date/go version headers, which is ugly.
	sed -i -e '/^  /d' build/$(BINNAME).1
	# Fix whatis entry.
	sed -i '/^.SH "NAME"/,+1c.SH "NAME"\n'$(WHATIS) build/$(BINNAME).1
	# Remove default values as they create unwieldy long lines.
	sed -i 's/\\fB--\(.*\)=".*"\\fR/\\fB--\1\\fR/' build/$(BINNAME).1

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	$(CURDIR)/ttar -C $(BUILDDIR)/src/$(DH_GOPKG)/collector/fixtures \
            -x -f collector/fixtures/sys.ttar
	dh_auto_test -- $(BUILDFLAGS)
endif

override_dh_auto_install:
