#!/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 | sed -n -e 's/^Version: //p')
VERSION    ?= $(shell echo '$(DEBVERS)' | sed 's/^[[:digit:]]*://; s/[-].*//')
DEBPKGNAME ?= $(shell dpkg-parsechangelog | sed -n -e 's/^Source: //p')
REV        := $(DEBVERS)
BRANCH     := debian/sid
USER       := pkg-go-maintainers@lists.alioth.debian.org
HOSTNAME   := $(shell hostname -f)
BUILD_DATE := $(shell date +%Y%m%d-%H:%M:%S)
GO_VERSION := $(shell go version | sed 's/go version \(\S*\).*/\1/')
BUILDFLAGS := -ldflags \
  " -X main.buildVersion $(VERSION)\
    -X main.buildRevision $(REV)\
    -X main.buildBranch $(BRANCH)\
    -X main.buildUser $(USER)\
    -X main.buildDate $(BUILD_DATE)\
    -X main.goVersion $(GO_VERSION)"

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

override_dh_auto_configure:
	dh_auto_configure -O--buildsystem=golang
	# Include test fixtures.
	for i in retrieval/discovery/fixtures config/testdata promql/testdata \
                storage/local/fixtures; do \
            cp -av $$i $(BUILDDIR)/src/$(DH_GOPKG)/$$i; \
        done
	# Include vendored dependencies.
	for i in github.com/hashicorp; do \
            cp -av Godeps/_workspace/src/$$i $(BUILDDIR)/src/$$i; \
        done

override_dh_auto_build:
	dh_auto_build -O--buildsystem=golang -- $(BUILDFLAGS)

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/static/vendor/bootstrap-datetimepicker/bootstrap-datetimepicker.less > \
            $(WEB_VENDORED_DIR)/bootstrap-datetimepicker/bootstrap-datetimepicker.css
	cp -v web/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
