#!/usr/bin/make -f

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

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

# cf. https://wiki.debian.org/ToolChain/LTO and https://github.com/golang/go/issues/45275
export DEB_BUILD_MAINT_OPTIONS=optimize=-lto

export GO111MODULE=off

export DH_GOLANG_GO_GENERATE := 1

export DH_GOLANG_INSTALL_EXTRA := cni test/e2e/testdata pkg/trust/testdata pkg/machine/compression/testdata

export DH_GOLANG_EXCLUDES := \
	contrib/perftest \
	dependencies \
	hack/podman-registry-go \
	cmd/podman/machine \
	pkg/bindings/test \
	podman/pkg/machine/e2e \
	test/e2e \
	test/endpoint \
	test/framework \
	test/goecho \
	test/testvol \
	test/utils \
	test/version

# machine/qemu only works on amd64, arm64
# set default build target explicitly, rely on dependencies
export DH_GOLANG_BUILDPKG := \
	github.com/containers/podman/cmd/podman \
	github.com/containers/podman/cmd/rootlessport \
	github.com/containers/podman/cmd/quadlet

export DH_GOLANG_TEST_EXCLUDES := $(DH_GOLANG_EXCLUDES) \
	hack \
	lock/file \
	network/cni \
	pkg/bindings \
	pkg/machine \
	podman/cmd/podman/common

## https://podman.io/getting-started/installation#build-tags
BUILDTAGS = apparmor seccomp selinux systemd libsubid cni
BUILDTAGS_REMOTE = remote exclude_graphdriver_btrfs btrfs_noversion exclude_graphdriver_devicemapper containers_image_openpgp

%:
	dh $@ --buildsystem=golang --with=golang,bash-completion --builddirectory=_output

execute_after_dh_auto_clean:
	# this is in addition to the golang cleanups, and cleans up docs among
	# other things. Use bash, as there is a $(<.gitignore) bashism in there.
	$(MAKE) clean SHELL=/bin/bash

execute_after_dh_auto_configure:
	# install extra source
	install -Dv -t _output/src/github.com/containers/podman/vendor/k8s.io/kubernetes/third_party/forked/golang/expansion debian/expand.go

override_dh_auto_build:
	DH_GOLANG_BUILDPKG="github.com/containers/podman/cmd/podman-testing" \
	dh_auto_build -v  --builddirectory=_output
	$(MAKE) docs docker-docs GOMD2MAN=/usr/bin/go-md2man
	DH_GOLANG_BUILDPKG="github.com/containers/podman/cmd/podman" \
	dh_auto_build -v  --builddirectory=_output -- -tags "$(BUILDTAGS_REMOTE)" \
          -ldflags "-X github.com/containers/podman/libpod/define.buildInfo=$(SOURCE_DATE_EPOCH) \
                    -X github.com/containers/podman/libpod/define.buildOrigin=$(DEB_VENDOR)"
	mv _output/bin/podman _output/bin/podman-remote
	dh_auto_build -v  --builddirectory=_output -- -tags "$(BUILDTAGS)" \
          -ldflags "-X github.com/containers/podman/libpod/define.buildInfo=$(SOURCE_DATE_EPOCH) \
                    -X github.com/containers/podman/libpod/define.buildOrigin=$(DEB_VENDOR)"

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	# make test binaries available where the tests expect them
	ln -sv  ../../../../bin  _output/src/github.com/containers/podman/bin
	mkdir -p -m700 $(CURDIR)/debian/tmp-home/.cache
	mkdir -p -m700 $(CURDIR)/debian/tmp-home/.config
	mkdir -p -m700 $(CURDIR)/debian/tmp-home/.local/share
	mkdir -p -m700 $(CURDIR)/debian/tmp-home/run
	env \
	  HOME=$(CURDIR)/debian/tmp-home/.cache \
	  XDG_CACHE_HOME=$(CURDIR)/debian/tmp-home/.cache \
	  XDG_CONFIG_HOME=$(CURDIR)/debian/tmp-home/.config \
	  XDG_DATA_HOME=$(CURDIR)/debian/tmp-home/.local/share \
	  XDG_RUNTIME_DIR=$(CURDIR)/debian/tmp-home/run \
	  PATH="$(CURDIR)/_output/bin:$$PATH" \
	  DH_GOLANG_EXCLUDES="$${DH_GOLANG_TEST_EXCLUDES}" \
	dh_auto_test -v --max-parallel=2 -- -tags "$(BUILDTAGS)"
	rm -rf $(CURDIR)/debian/tmp-home
endif

override_dh_auto_install:
	dh_auto_install \
		--destdir=debian/tmp \
		--buildsystem=golang \
		--builddirectory=_output \
		-- \
		--no-source
	# generate and install systemd unit files, manpages, etc.
	make DESTDIR=debian/tmp PREFIX=/usr LIBDIR=/usr/lib \
		install.systemd install.docker-full install.man

execute_after_dh_installman:
	# installed in the podman-remote package instead
	rm -f --verbose $(CURDIR)/debian/podman/usr/share/man/man1/podman-remote.1*
	# avoid file conflicts with the podman-compose/docker-compose packages, cf. #1054460
	rm -f --verbose $(CURDIR)/debian/podman/usr/share/man/man1/podman-compose.1*
	rm -f --verbose $(CURDIR)/debian/podman-docker/usr/share/man/man1/docker-compose.1*
	# don't ship "dockersh": podmansh is a podman-specific function
	rm -f --verbose $(CURDIR)/debian/podman-docker/usr/share/man/man1/dockersh.1*

override_dh_installsystemd:
	dh_installsystemd --no-enable --no-start

override_dh_installsystemduser:
	dh_installsystemduser --no-enable

