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

# Excluded binaries. List all binary names:
# go list -f '{{ if eq .Name "main" }}{{ .ImportPath }}{{ end }}' ./...
# (cd gopls && go list -f '{{ if eq .Name "main" }}{{ .ImportPath }}{{ end }}' ./...)
export DH_GOLANG_EXCLUDES := \
	$(addprefix /,$(addsuffix (/|$$),\
		$(filter-out debian gopls,\
			$(sort $(shell find * -name '*.go' | cut -d'/' -f1))\
	))) \
	gopls/api-diff \
	gopls/doc \
	gopls/integration/govim \
	gopls/internal/coverage \
	gopls/internal/analysis/unusedparams/cmd \
	gopls/internal/protocol/generate \
	gopls/internal/telemetry/cmd/stacks \
	gopls/release \

# Excluded tests
export DH_GOLANG_EXCLUDES += \
	gopls/internal/regtest

# needed for tests.
export DH_GOLANG_INSTALL_EXTRA := \
	godoc/static \
	gopls/go.sum \
	gopls/internal/cmd/usage \
	gopls/internal/doc/api.json \
	gopls/internal/server/assets

VERSION := $(shell dpkg-parsechangelog --show-field Version | sed -E 's/([0-9]+:)?([0-9.~pre]+).*/\2/')
LDFLAGS := -ldflags '-X "main.version=v$(VERSION)"'

execute_before_dh_auto_configure:
	# Copy golang.org/x/telemetry for gopls opt-in transparent telemetry;
	# see https://go.dev/issue/58894
	mkdir -p _build
	cp -a debian/go/src _build/

ifdef AUTOPKGTEST_TMP
ifeq ($(wildcard "$(AUTOPKGTEST_TMP)/_build/src/golang.org/x/tools/gopls),)
execute_after_dh_auto_configure:
	@echo "[info] _build/src/golang.org/x/tools/gopls not found"
	@echo "[info] dh_golang_autopkgtest replaced gopls source with golang-golang-x-tools-dev"
	@echo "[info] Undoing dh_golang_autopkgtest's mistake"
	rm -rf _build
	mkdir _build
	cp -a debian/go/src _build/
	dh_auto_configure -O--builddirectory=_build -O--buildsystem=golang
endif
endif

override_dh_auto_build:
	dh_auto_build -- $(LDFLAGS)

override_dh_auto_install:
	dh_auto_install -- --no-source

override_dh_auto_test:
ifeq (,$(findstring gccgo, $(shell go version)))
	# use short tests where possible (especially to skip tests like
	# TestWebIndex which are very slow)
	dh_auto_test -- -short -timeout=30m
endif

%:
	dh $@ --builddirectory=_build --buildsystem=golang
