#!/usr/bin/make -f

include /usr/share/dpkg/architecture.mk
include /usr/share/dpkg/default.mk

# Uncomment this to turn on verbose mode.
# export DH_VERBOSE=1

export DH_GOLANG_EXCLUDES := plugin/builtin plugin/someteam.example.com
# Setting DH_GOLANG_EXCLUDES_ALL to false to ensure that only files from the
# plugin folder are excluded during the build process, while keeping them in
# the source. These files are necessary for Autopkgtest to run successfully.
export DH_GOLANG_EXCLUDES_ALL := 0
export DH_GOLANG_INSTALL_EXTRA := plugin/someteam.example.com/v1/bashedconfigmap/BashedConfigMap

DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)

LDFLAGS := \
	-X 'sigs.k8s.io/kustomize/api/provenance.buildDate=2023-01-31T23:38:41Z' \
	-X 'sigs.k8s.io/kustomize/api/provenance.version=(test)'

# Default list of tests to skip that try to access the internet
SKIP_TESTS_LIST := \
	TestDefaultAbsPluginHomeNoXdgWithDotConfig|\
	TestDefaultAbsPluginHomeNoXdgJustHomeDir|\
	TestAnnoOriginRemoteBuiltinTransformer|\
	TestRemoteLoad_RemoteProtocols|\
	TestAnnoOriginRemoteBuiltinGenerator|\
	TestCustomOpenApiFieldFromOverlayWithRemoteSchema|\
	TestCustomOpenApiFieldFromBaseWithRemoteSchema|\
	TestCustomOpenApiFieldBasicUsageWithRemoteSchema|\
	TestExistingCacheDir|\
	TestRemoteRootNoRef|\
	TestResourcesRepoNotFile|\
	TestNestedRemoteRoots|\
	TestRemoteRoot|\
	TestRemoteFile|\
	TestBadArgs|\
	TestRemoteTargetDefaultDst|\
	ExampleFilter|\
	TestSedTransformer|\
	TestPluginEnvironment

# Additional tests to skip not supported on linux/riscv64
ifeq ($(DEB_HOST_ARCH),riscv64)
SKIP_TESTS_LIST := $(SKIP_TESTS_LIST)|\
	TestCompiler|\
	TestLoader|\
	TestReusableCustomTransformers|\
	TestIssue1251_Plugins_ProdVsDev|\
	TestIssue1251_Plugins_Local|\
	TestIssue1251_Plugins_Bundled|\
	TestPluginsNotEnabled
endif

# Remove spaces after "|"
SKIP_TESTS := $(subst | ,|,$(SKIP_TESTS_LIST))

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

execute_after_dh_auto_configure:
	# dh_auto_configure first copies source files to the build root, then
	# symlinks libraries from /usr/share/gocode/src.
	# The dependency symlink will NOT occur when the library package uses the
	# same XS-Go-Import-Path (DH_GOPKG) as the source package and ship files
	# under the same GOPATH, causing problems in dh_auto_build due to lack of
	# installed libraries.
	# dh_auto_configure: warning: "sigs.k8s.io/kustomize" is already installed.
	# https://salsa.debian.org/go-team/packages/dh-golang/-/blob/81892fe8d3a51a22c910a649052669dd9f18837e/lib/Debian/Debhelper/Buildsystem/golang.pm#L495
	ln -vs /usr/share/gocode/src/sigs.k8s.io/kustomize/kyaml $(CURDIR)/_build/src/sigs.k8s.io/kustomize/kyaml

override_dh_auto_test:
	# Set missing flags and skip tests that try to access the internet.
	dh_auto_test --builddirectory=_build -- -ldflags "$(LDFLAGS)" -skip="$(SKIP_TESTS)"

execute_before_dh_auto_install:
	find $(CURDIR)/_build/src/sigs.k8s.io/kustomize/plugin -type f -name "*.so" -delete

override_dh_auto_install:
	dh_auto_install -- --no-binaries
