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

# Uncomment this to turn on verbose mode.
export DH_VERBOSE=1

# DH_GOPKG is the upstream path which you would normally “go get”.
# Using it allows us to build applications without patching locations.
export DH_GOPKG := github.com/prometheus/prometheus

#export DH_GOLANG_EXCLUDES := web/blob

BUILDDIR := $(shell perl -w -MDebian::Debhelper::Buildsystem::golang -e \
    'print Debian::Debhelper::Buildsystem::golang->new()->get_builddir()')

# 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 $(DH_GOPKG)/version.Version=$(VERSION)\
    -X $(DH_GOPKG)/version.Revision=$(REV)\
    -X $(DH_GOPKG)/version.Branch=$(BRANCH)\
    -X $(DH_GOPKG)/version.BuildUser=$(USER)\
    -X $(DH_GOPKG)/version.BuildDate=$(BUILD_DATE)\
    -X $(DH_GOPKG)/version.GoVersion=$(GO_VERSION)"

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

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

override_dh_auto_configure:
	dh_auto_configure -O--buildsystem=golang
	# Include test fixtures.
	for i in retrieval/discovery/fixtures storage/local/fixtures \
                config/testdata promql/testdata retrieval/testdata; do \
            cp -av $$i $(BUILDDIR)/src/$(DH_GOPKG)/$$i; \
        done
	# 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 -O--buildsystem=golang -- $(BUILDFLAGS)

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

WEB_VENDORED_DIR := debian/prometheus/usr/share/prometheus/web/static/vendor
override_dh_auto_install:
	dh_auto_install
	# we don't want the golang source (yet?)
	rm -r debian/prometheus/usr/share/gocode
	# Build and install datetimepicker.
	mkdir -vp $(WEB_VENDORED_DIR)/bootstrap-datetimepicker
	lessc web/blob/static/vendor/bootstrap-datetimepicker/bootstrap-datetimepicker.less > \
            $(WEB_VENDORED_DIR)/bootstrap-datetimepicker/bootstrap-datetimepicker.css
	cp -v web/blob/static/vendor/bootstrap-datetimepicker/bootstrap-datetimepicker.js \
            $(WEB_VENDORED_DIR)/bootstrap-datetimepicker/

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
