#!/usr/bin/make -f

PKGNAME=podman-toolbox
GOPKG=github.com/containers/toolbox
GOPATH_TMP=$(CURDIR)/debian/tmp
GOPATH_BUNDLED_VENDOR=$(CURDIR)/debian/gocode
GOPATH_PKG_DESTDIR=$(GOPATH_TMP)/src/$(GOPKG)

# Only search for modules under GOPATH and vendor directories
export GO111MODULE=off
export GOPATH=$(CURDIR):$(GOPATH_TMP):$(GOPATH_BUNDLED_VENDOR):/usr/share/gocode
export GOCACHE=$(GOPATH_TMP)/go-build

%:
	dh $@ --buildsystem=meson

override_dh_auto_configure:
	# Create links to the internal modules under a dir (in GOPATH)
	# named after the exported package name. This is needed to
	# satisfy `go build` when GO111MODULE=off is set
	mkdir -p "$(GOPATH_PKG_DESTDIR)" && \
		cd "$(CURDIR)/src" && \
		find . -mindepth 2 -name '*.go' ! \
			-path "./vendor/*" ! \
			-path "./github.com/containers/toolbox/*" ! \
			-iname '.*' | cut -d'/' -f2 | uniq | \
			xargs -I '{}' ln -sf "$(CURDIR)/src/{}" "$(GOPATH_PKG_DESTDIR)/{}"
	dh_auto_configure -- --buildtype=plain -Dprofile_dir=/etc/profile.d \
		-Dfish_completions_dir=/usr/share/fish/vendor_completions.d

ifeq ($(FAKEROOTKEY),)
override_dh_auto_build:
	fakeroot dh_auto_build
endif

override_dh_auto_install:
	dh_auto_install
	# Do not install tests
	rm -rf $(CURDIR)/debian/$(PKGNAME)/usr/share/toolbox/test

override_dh_auto_clean:
	dh_auto_clean
	rm -rf $(GOPATH_TMP)

override_dh_dwz:
	-dh_dwz

override_dh_auto_test:
	-dh_auto_test