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

DEBIAN_DIR = $(CURDIR)/debian
TMP     = $(DEBIAN_DIR)/$(PACKAGE)
SOURCE = $(TMP)/../

export DH_GOLANG_GO_GENERATE := 1
export DH_GOPKG := github.com/influxdata/influxdb
export DH_GOLANG_INSTALL_EXTRA := query/tmpldata

# Excluding influx-tools just because there are failing go generate commands.
# One of the issue we are running into is that apparently stringer requires
# that packages be installed before it runs. dh-golang currently runs
# go generate before installing any packages. One way to fix this is that
# dh-golang could install all dependencies, run go generate, and then install
# the final package.
export DH_GOLANG_EXCLUDES := tests cmd/influx-tools

%:
	dh $@ --buildsystem=golang --with=golang,systemd --builddirectory=_build

override_dh_auto_configure:
	dh_auto_configure

	# Include tests files
	cp -v $(SOURCE)/stress/stress.toml _build/src/$(DH_GOPKG)/stress/stress.toml
	mkdir -v -p _build/src/$(DH_GOPKG)/stress/v2/iql
	cp -v $(SOURCE)/stress/v2/iql/file.iql _build/src/$(DH_GOPKG)/stress/v2/iql/file.iql
	cp -v $(SOURCE)/stress/v2/iql/default.iql _build/src/$(DH_GOPKG)/stress/v2/iql/default.iql

	# include tmpldata files
	cp -v $(SOURCE)/tsdb/engine/tsm1/*.tmpl* _build/src/$(DH_GOPKG)/tsdb/engine/tsm1
	cp -v $(SOURCE)/query/*.tmpl* _build/src/$(DH_GOPKG)/query
	cp -v $(SOURCE)/services/storage/*.tmpl* _build/src/$(DH_GOPKG)/services/storage
	cp -v $(SOURCE)/pkg/slices/*.tmpl* _build/src/$(DH_GOPKG)/pkg/slices

	# rename some imports
	find _build/src/$(DH_GOPKG) -type f -print0 | xargs -0 sed -i 's|github.com/uber-go/zap|go.uber.org/zap|g'
	find _build/src/$(DH_GOPKG) -type f -print0 | xargs -0 sed -i 's|github.com/dgrijalva/jwt-go|github.com/dgrijalva/jwt-go-v3|g'

# influxdb version
DEB_VERSION := $(shell dpkg-parsechangelog -l$(DEBIAN_DIR)/changelog --show-field Version)
DEB_NOEPOCH_VERSION := $(shell echo $(DEB_VERSION) | cut -d: -f2-)
DEB_UPSTREAM_VERSION := $(shell echo $(DEB_NOEPOCH_VERSION) | sed 's/-[^-]*$$//' | sed 's/[+~].*$$//')

override_dh_auto_build:
	dh_auto_build -- -ldflags="-X main.version=$(DEB_UPSTREAM_VERSION)"

override_dh_auto_test:
	# I could never get tests working reliably.
