#!/usr/bin/make -f
# -*- Mode: makefile -*-

export DH_VERBOSE=1
export DH_GOPKG := github.com/prometheus/prometheus
# Include test fixtures.
export DH_GOLANG_INSTALL_EXTRA := retrieval/discovery/fixtures \
    storage/local/fixtures config/testdata promql/testdata retrieval/testdata \
    promql/fuzz-data

BUILDDIR := build
METAPKG := github.com/prometheus/common

# Taken and modified from the project's Makefile.

DEBVERS    ?= $(shell dpkg-parsechangelog -SVersion)
VERSION    ?= $(shell echo '$(DEBVERS)' | sed 's/^[[:digit:]]*://; s/[-].*//')
DEBPKGNAME ?= $(shell dpkg-parsechangelog -SSource)
DEBDATE    ?= $(shell dpkg-parsechangelog -SDate)
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)"

# Avoid some tests that fail on slow architectures.
ifeq ($(DEB_HOST_ARCH_CPU),arm)
SKIP_TESTS := github.com/prometheus/prometheus/retrieval
SHORT_TESTS := -short
else
SKIP_TESTS :=
SHORT_TESTS :=
endif

%:
	dh $@ --buildsystem=golang --with=golang --builddirectory=$(BUILDDIR)

override_dh_auto_configure:
	dh_auto_configure -O--buildsystem=golang
	# Include vendored dependencies.
	for i in github.com/hashicorp/consul \
                github.com/influxdb/influxdb; \
        do \
            dst=`dirname $(BUILDDIR)/src/$$i`; \
            mkdir -vp $$dst; \
            cp -av vendor/$$i $$dst; \
        done

override_dh_auto_build:
	dh_auto_build -- $(BUILDFLAGS)
	# Build and install datetimepicker.
	mkdir -vp $(BUILDDIR)/bootstrap-datetimepicker
	lessc web/ui/static/vendor/bootstrap-datetimepicker/bootstrap-datetimepicker.less > \
            $(BUILDDIR)/bootstrap-datetimepicker/bootstrap-datetimepicker.css
	cp web/ui/static/vendor/bootstrap-datetimepicker/bootstrap-datetimepicker.js \
            $(BUILDDIR)/bootstrap-datetimepicker/

override_dh_auto_test:
	# Avoid some tests that fail on slow architectures.
	DH_GOLANG_EXCLUDES="$(SKIP_TESTS)" dh_auto_test -- $(SHORT_TESTS)

override_dh_auto_install:
	dh_auto_install -- --no-source

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