#!/usr/bin/make -f

include /usr/share/dpkg/pkg-info.mk
include /usr/share/dpkg/buildflags.mk

# Our GOPATH and GOCACHE locations.
OUR_GOPATH = $(CURDIR)/.gopath
export GOPATH = $(OUR_GOPATH)
export GOCACHE = $(CURDIR)/.gocache

%:
	dh $@

override_dh_auto_clean:
	go clean -cache -modcache
	dh_auto_clean

override_dh_auto_build:
	go build -v -x \
		-buildmode=pie \
		-ldflags "-X main.version=$(DEB_VERSION) -extldflags \"$(LDFLAGS)\"" \
		-mod=vendor ./cmd/telegraf/
	rst2man debian/telegraf.rst debian/telegraf.1

# We don't want to call "make install" because it will try to execute
# "go build" without taking the vendor/ directory into consideration,
# which will cause go to try to download all of the modules.
override_dh_auto_install:

# The Apache license states that we must distribute the NOTICE files
# if they exists, which is the case for us.
override_dh_installdocs:
	find . -type f -name "NOTICE*" | \
	while read -r notice_file ; do \
		echo "-- $$notice_file --" >> NOTICE; \
		cat $$notice_file >> NOTICE; \
	done
	dh_installdocs
	rm NOTICE

# Which tests do we want to exclude?
#
#   - inputs/ping depends on the network, so it can't run while
#     building.
#
#   - processors/reverse_dns also depends on the network.
EXCLUDE_TESTS = \
	github.com/influxdata/telegraf/plugins/inputs/ping \
	github.com/influxdata/telegraf/plugins/processors/reverse_dns

# Obtain the list of all possible tests, and remove those that are not
# suitable.
ALL_TESTS = $(shell go list -mod=vendor ./...)
TEST_LIST = $(filter-out $(EXCLUDE_TESTS),$(ALL_TESTS))

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	go test -mod=vendor -short $(TEST_LIST)
endif
