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

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

# Acceptance tests need to be run separately.
export DH_GOLANG_EXCLUDES := test/.*/acceptance examples asset
export DH_GOLANG_EXCLUDES_ALL := 0
export DH_GOLANG_INSTALL_EXTRA := template/default.tmpl ui/app

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/')
GCCGO       = $(strip $(shell go version | grep gccgo))
VERSION     = $(shell echo '$(DEB_VERSION_UPSTREAM)' | tr '~' '-')
BUILDFLAGS  = -ldflags \
  " -X $(METAPKG)/version.Version=$(VERSION)\
    -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,bash-completion \
	    --builddirectory=build

WHATIS1 := "prometheus-alertmanager \\- The Prometheus alert manager"
WHATIS2 := "promtool \\- Tooling for the Prometheus alert manager"

override_dh_auto_build:
	dh_auto_build -- $(BUILDFLAGS)
	mv -v build/bin/alertmanager build/bin/prometheus-alertmanager
	# Build bash completion scripts.
	mkdir -vp build/bash_completion.d
	build/bin/amtool --completion-script-bash > \
	    build/bash_completion.d/amtool
	# Build man pages.
	mkdir -vp build/man
	build/bin/prometheus-alertmanager --help-man > \
	    build/man/prometheus-alertmanager.1
	build/bin/amtool --help-man > \
	    build/man/amtool.1
	# Remove build information headers, and fix whatis entry.
	sed -i '/^  /d; /^.SH "NAME"/,+1c.SH "NAME"\n'$(WHATIS1) \
	    build/man/prometheus-alertmanager.1
	sed -i '/^  /d; /^.SH "NAME"/,+1c.SH "NAME"\n'$(WHATIS2) \
	    build/man/amtool.1
	# Fix executable path in amtool examples
	sed -i 's/\.\/amtool/amtool/' build/man/amtool.1

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	dh_auto_test
	# Don't abort if acceptance tests fail, as they are too sensitive to
	# timing issues.
	( for pkg in cli/acceptance with_api_v1/acceptance \
	    with_api_v2/acceptance; do \
	    DH_GOLANG_EXCLUDES= DH_GOPKG=$(DH_GOPKG)/test/$$pkg \
	    dh_auto_test || { \
		echo "*** Acceptance tests failed ***"; \
		exit 1; \
		}; \
	  done; ) || true
endif

override_dh_auto_install-indep:
	dh_auto_install -- --no-binaries
	dh_install usr/share/gocode

override_dh_auto_install-arch:
	dh_auto_install -- --no-source
	dh_install usr/bin/amtool
	dh_install usr/bin/prometheus-alertmanager
