#!/usr/bin/make -f

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

# Test helpers and tooling that need testcontainers, gitkit or go-ruleguard,
# none of which is in Debian; docs, examples and the mage build scripts.
export DH_GOLANG_EXCLUDES := ^(github.com/aquasecurity/trivy/)?(docs|examples|helpers|integration|magefiles|misc|internal/gittest|internal/testutil|pkg/iac/adapters/terraform/tftestutil|pkg/iac/adapters/cloudformation/testutil|pkg/iac/adapters/arm/adaptertest)(/|$$)
# go:embed targets.
export DH_GOLANG_INSTALL_EXTRA := $(wildcard pkg/iac/rego/schemas/*.json) \
	pkg/licensing/expression/licenses.json pkg/licensing/expression/exceptions.json \
	pkg/iac/scanners/cloudformation/test/examples
# Debian installs these old names as symlinks to the new modules.
export DH_GOLANG_MODULE_MAPPINGS := github.com/go-ini/ini:gopkg.in/ini.v1 \
	github.com/AdaLogics/go-fuzz-headers:github.com/AdamKorcz/go-fuzz-headers-1

%:
	dh $@ --builddirectory=debian/_build --buildsystem=golang --with=golang

# dh-go 1.69 go.work defects, see trivy-kubernetes: replaced directories
# without go.mod (https://bugs.debian.org/1149023), json-patch/v5 not mapped,
# diskv v3 installed at the v2 path; Debian's k8s also imports json-patch/v5
# and cel.dev/cel-go without requiring them in go.mod.
execute_after_dh_auto_configure:
	# Test helpers and tests that need testcontainers-go, aquasecurity/testdocker,
	# alicebob/miniredis or sosedoff/gitkit, none of which is in Debian; nothing else in testutil depends on them.
	cd debian/_build/src/github.com/aquasecurity/trivy && rm -f \
	  internal/testutil/localstack.go \
	  pkg/commands/auth/run_test.go pkg/remote/remote_test.go \
	  pkg/fanal/image/image_test.go pkg/fanal/image/daemon/podman_test.go \
	  pkg/fanal/image/daemon/image_test.go \
	  pkg/cache/redis_test.go \
	  pkg/plugin/manager_unix_test.go pkg/fanal/artifact/repo/git_test.go \
	  pkg/iac/scanners/terraform/parser/resolvers/cache_integration_test.go
	# internal/testutil reads this list at init; the rest of integration/ is
	# excluded.
	mkdir -p debian/_build/src/github.com/aquasecurity/trivy/integration
	cp integration/testimages.ini debian/_build/src/github.com/aquasecurity/trivy/integration/
	cd debian/_build && sed -n 's|^replace \(\S*\) => \./src/\(\S*\)$$|\1 \2|p' go.work | \
	while read m d; do \
		[ ! -e src/$$d/go.mod ] || continue; \
		v=$$(go env GOVERSION | sed -E 's/^go([0-9]+\.[0-9]+).*/\1/'); \
		r=$$(realpath src/$$d); \
		case $$r in \
		$$(realpath .)/*) printf 'module %s\n\ngo %s\n' $$m $$v > src/$$d/go.mod ;; \
		*) mkdir -p src/COMPAT/$$m && ln -s $$r/* src/COMPAT/$$m/ && \
		   printf 'module %s\n\ngo %s\n' $$m $$v > src/COMPAT/$$m/go.mod && \
		   sed -i "s|^replace $$m => .*|replace $$m => ./src/COMPAT/$$m|" go.work ;; \
		esac; \
	done
	cd debian/_build && \
	  go mod edit -require=github.com/evanphx/json-patch/v5@v5.7.0 -require=cel.dev/cel-go@v0.32.0 src/github.com/aquasecurity/trivy/go.mod && \
	  echo 'replace github.com/evanphx/json-patch/v5 => ./src/github.com/evanphx/json-patch/v5' >> go.work
	# Debian ships buildkit split into several modules; trivy's go.mod only
	# requires the root one.
	cd debian/_build && for m in $$(sed -n 's|^replace \(github.com/moby/buildkit/[^ ]*\) => .*|\1|p' go.work); do \
		go mod edit -require=$$m@v0.34.0 src/github.com/aquasecurity/trivy/go.mod; \
	done
	mkdir -p debian/_build/src/COMPAT/github.com/peterbourgon/diskv
	cd debian/_build/src/COMPAT/github.com/peterbourgon/diskv && \
	  ln -s /usr/share/gocode/src/github.com/peterbourgon/diskv/*.go . && \
	  sed 's|^module .*|module github.com/peterbourgon/diskv|' \
	    /usr/share/gocode/src/github.com/peterbourgon/diskv/go.mod > go.mod
	sed -i 's|^replace github.com/peterbourgon/diskv => .*|replace github.com/peterbourgon/diskv => ./src/COMPAT/github.com/peterbourgon/diskv|' \
	  debian/_build/go.work

# dh_golang also sets misc:Built-Using, which compat 14 applies unasked; the
# list is too long for that field and Static-Built-Using already carries it.
execute_after_dh_golang:
	find debian -maxdepth 1 -name '*.substvars' -exec sed -i '/^misc:Built-Using?\?=/d' {} +

override_dh_auto_build:
	dh_auto_build -- -buildmode=pie \
		-ldflags="-X github.com/aquasecurity/trivy/pkg/version/app.ver=$(DEB_VERSION_UPSTREAM)"

override_dh_auto_install:
	dh_auto_install --destdir=debian/trivy -- --no-source

# Test_rpmArchiveAnalyzer_Analyze needs an RPM that upstream downloads with
# "mage rpm:fixtures", and TestArtifact_Inspect/git_repository a git fixture
# that mage also generates; TestDockerImage needs a Docker daemon;
# TestManager_Register loads WebAssembly modules built with TinyGo. The
# Unidecode PKG-INFO fixture is excluded from the orig (no licence).
override_dh_auto_test:
	dh_auto_test -- -skip 'Test_rpmArchiveAnalyzer_Analyze|TestArtifact_Inspect/git_repository|TestDockerImage|TestManager_Register|TestParse/egg_PKG-INFO_with_description_containing_non-RFC_7230_bytes'
