#!/usr/bin/make -f

# examples/ is a demo program; tests/ needs a running cluster.
export DH_GOLANG_EXCLUDES := examples tests
export DH_GOLANG_INSTALL_EXTRA := pkg/jobs/template

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

# This test starts a k3s cluster in Docker through testcontainers-go.
# dh-go 1.69 replaces modules with src/ directories that have no go.mod
# (go-flowrate ships none; cpuid v1 only exists as the parent of cpuid/v2),
# and go then fails to read them: give each one a go.mod.
execute_after_dh_auto_configure:
	rm -f debian/_build/src/github.com/aquasecurity/trivy-kubernetes/pkg/trivyk8s/trivyk8s_test.go
	# Under autopkgtest dh-go writes go.work later, inside dh_auto_build, and
	# the module directories are copies of the installed sources: fix those.
	if [ -e debian/_build/go.work ]; then $(MAKE) -f debian/rules fix-go-work; \
	else $(MAKE) -f debian/rules fix-copied-sources; fi

M := src/github.com/aquasecurity/trivy-kubernetes

# Stub the go.mod of every required module that ships none, require what Debian's
# k8s imports, and give diskv its v2 module path.
fix-copied-sources:
	cd debian/_build && find src -name go.mod -exec awk '/^require \(/{r=1;next} r&&/^\)/{r=0} r{print $$1} /^require [^(]/{print $$2}' {} + | sort -u | \
	while read m; do \
		[ -d src/$$m ] && [ ! -h src/$$m ] && [ ! -e src/$$m/go.mod ] || continue; \
		printf 'module %s\n\ngo 1.21\n' $$m > src/$$m/go.mod; \
	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 $(M)/go.mod
	sed -i 's|^module .*|module github.com/peterbourgon/diskv|' debian/_build/src/github.com/peterbourgon/diskv/go.mod

# Debian's k8s imports json-patch/v5, which dh-go does not map and no
# go.mod in the graph requires; Debian ships diskv v3 at the import path
# client-go uses for v2.
fix-go-work:
	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-kubernetes/go.mod && \
	  echo 'replace github.com/evanphx/json-patch/v5 => ./src/github.com/evanphx/json-patch/v5' >> go.work
	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
