#!/usr/bin/make -f

# use Go 1.24 from golang-for-gce-agents
export PATH := /usr/lib/go-1.24/bin:$(PATH)

# Force local vendoring
export GOTOOLCHAIN := local
export GOPROXY := off
export GOFLAGS := -mod=vendor

export DH_GOLANG_EXCLUDES := e2e_tests
export DH_GOLANG_BUILDPKG := github.com/GoogleCloudPlatform/osconfig github.com/GoogleCloudPlatform/osconfig/agentconfig github.com/GoogleCloudPlatform/osconfig/agentendpoint github.com/GoogleCloudPlatform/osconfig/attributes github.com/GoogleCloudPlatform/osconfig/clog github.com/GoogleCloudPlatform/osconfig/external github.com/GoogleCloudPlatform/osconfig/inventory github.com/GoogleCloudPlatform/osconfig/osinfo github.com/GoogleCloudPlatform/osconfig/ospatch github.com/GoogleCloudPlatform/osconfig/packages github.com/GoogleCloudPlatform/osconfig/policies github.com/GoogleCloudPlatform/osconfig/policies/recipes github.com/GoogleCloudPlatform/osconfig/retryutil github.com/GoogleCloudPlatform/osconfig/tasker github.com/GoogleCloudPlatform/osconfig/util github.com/GoogleCloudPlatform/osconfig/util/mocks
export DH_GOLANG_INSTALL_EXTRA := vendor/google.golang.org/protobuf/internal/editiondefaults/editions_defaults.binpb

# dh-golang sets up source in _build/src/; GOPATH must point there
export GOPATH := $(CURDIR)/_build
export GOCACHE := $(CURDIR)/.gocache

# GO111MODULE needs to be set to "off" to avoid checking
# modules information online.
export GO111MODULE := off

%:
	dh $@ --builddirectory=_build --buildsystem=golang --with=golang,systemd

override_dh_auto_configure:
	cp -r debian/extra/vendor ./
	dh_auto_configure
	# dh-golang 1.12 only symlinks .go files into the build tree
	# so we copy the .binpb file needed by go:embed
	cp vendor/google.golang.org/protobuf/internal/editiondefaults/editions_defaults.binpb \
		_build/src/github.com/GoogleCloudPlatform/osconfig/vendor/google.golang.org/protobuf/internal/editiondefaults/

# Bypass dh_auto_build's -gcflags/-asmflags quoting which is broken
# with dh-golang < 1.43 and Go >= 1.24
override_dh_auto_build:
	cd _build && go install -trimpath -v -p 4 \
		-ldflags="-s -w -X main.version=$(shell dpkg-parsechangelog -S Version)" \
		$(DH_GOLANG_BUILDPKG)

# Skip tests: dh-golang 1.12 doesn't copy testdata/ into the build tree
override_dh_auto_test:

override_dh_auto_install:
	dh_auto_install -- --no-source
	mv debian/google-osconfig-agent/usr/bin/osconfig debian/google-osconfig-agent/usr/bin/google_osconfig_agent
	install -d debian/google-osconfig-agent/var/lib/google_osconfig_agent

override_dh_systemd_start:
	dh_systemd_start

override_dh_clean:
	dh_clean vendor/
