#!/usr/bin/make -f

# Every module of the repository that we build and test. dh-go always builds
# module-aware, and a module-aware `go list` only enumerates the main module, so
# without this list the submodules that carry their own go.mod (sdk, log,
# exporters, ...) are neither built nor tested, silently. dh-go writes go.work
# from DH_GOLANG_BUILDPKG during dh_auto_configure, where the /... patterns do
# not resolve yet, so the bare module paths go there and the patterns to
# dh_auto_build and dh_auto_test.
DH_GOLANG_MODULES := \
	go.opentelemetry.io/otel \
	go.opentelemetry.io/otel/log \
	go.opentelemetry.io/otel/log/logtest \
	go.opentelemetry.io/otel/metric \
	go.opentelemetry.io/otel/metric/x \
	go.opentelemetry.io/otel/schema \
	go.opentelemetry.io/otel/sdk \
	go.opentelemetry.io/otel/sdk/log \
	go.opentelemetry.io/otel/sdk/log/logtest \
	go.opentelemetry.io/otel/sdk/metric \
	go.opentelemetry.io/otel/trace \
	go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc \
	go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp \
	go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc \
	go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp \
	go.opentelemetry.io/otel/exporters/otlp/otlptrace \
	go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc \
	go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp \
	go.opentelemetry.io/otel/exporters/prometheus \
	go.opentelemetry.io/otel/exporters/stdout/stdoutlog \
	go.opentelemetry.io/otel/exporters/stdout/stdoutmetric \
	go.opentelemetry.io/otel/exporters/stdout/stdouttrace

# Modules we do not build, to keep the -dev package installing the same set of
# files it did before the switch to dh-go and to avoid pulling opencensus,
# opentracing and zipkin into the dependencies.
export DH_GOLANG_EXCLUDES := \
	example/ \
	internal/tools \
	bridge/opencensus \
	bridge/opentracing \
	exporters/zipkin \
	trace/internal/telemetry/test

export DH_GOLANG_INSTALL_EXTRA := \
	$(wildcard internal/tools/semconvkit/templates/*.tmpl) \
	$(wildcard internal/tools/semconvkit/templates/*/*.tmpl) \

# autopkgtest.ubuntu.com exports these variable and causes the tests hanging
unexport https_proxy
unexport http_proxy

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

override_dh_auto_configure:
	DH_GOLANG_BUILDPKG="$(DH_GOLANG_MODULES)" dh_auto_configure

override_dh_auto_build:
# dh_golang_autopkgtest disables override_dh_auto_configure (it renames it with
# a sed) and replaces it with one that only copies the installed sources, so in
# the autopkgtest there is no go.work and the /... patterns match nothing. Create
# the workspace first when it is missing; in a normal build dh_auto_configure
# already did it and this line does nothing.
	[ -f _build/go.work ] || DH_GOLANG_BUILDPKG="$(DH_GOLANG_MODULES)" dh_auto_build
	DH_GOLANG_BUILDPKG="$(addsuffix /...,$(DH_GOLANG_MODULES))" dh_auto_build

# https://github.com/open-telemetry/opentelemetry-go/issues/6476
# internal/global failed to setup with disable-sdk-auto.patch
override_dh_auto_test:
	DH_GOLANG_BUILDPKG="$(addsuffix /...,$(DH_GOLANG_MODULES))" \
	DH_GOLANG_EXCLUDES="$(DH_GOLANG_EXCLUDES) internal/global" \
	dh_auto_test $(DH_BUILD_OPTS) -- \
		-skip=TestTraceProviderDelegatesSameInstance

override_dh_fixperms:
	dh_fixperms
	# Drop executable bits to make lintian happy
	find debian/golang-opentelemetry-otel-dev -type f -executable \
		-not -name "*.sh" -exec chmod -x {} +
