#!/usr/bin/make -f

#VERSION ?= $(shell cat engine/VERSION)

export HOME=/root
export GOPATH=$(shell pwd)/go
VERSION="20.10.3"
PREFIX="$(shell pwd)/build"

override_dh_auto_build:
	mkdir -p $(shell pwd)/build
	mkdir -p $(shell pwd)/go/src/github.com/docker
	ln -snf $(shell pwd)/sources/engine $(shell pwd)/go/src/github.com/docker/docker
	ln -snf $(shell pwd)/sources/cli $(shell pwd)/go/src/github.com/docker/cli
	# Build the daemon and dependencies
	cd sources/engine && VERSION=$(VERSION) PRODUCT=docker ./hack/make.sh dynbinary
	PREFIX="$(PREFIX)" TMP_GOPATH="$(shell pwd)/go" installer/install.sh tini
	PREFIX="$(PREFIX)" TMP_GOPATH="$(shell pwd)/go" installer/install.sh proxy dynamic
	PREFIX="$(PREFIX)" TMP_GOPATH="$(shell pwd)/go" installer/install.sh rootlesskit dynamic
	# Build the CLI
	cd $(shell pwd)/go/src/github.com/docker/cli && \
		LDFLAGS='' DISABLE_WARN_OUTSIDE_CONTAINER=1 make VERSION=$(VERSION) GITCOMMIT=$(CLI_GITCOMMIT) dynbinary 
	# Make sure to set LDFLAGS="" since, dpkg-buildflags sets it to some weird values
	set -e; LDFLAGS='' bash installer/plugins/buildx.installer build 
override_dh_auto_test:
	./sources/engine/bundles/dynbinary-daemon/dockerd -v
	./sources/cli/build/docker -v

override_dh_strip:
	# Go has lots of problems with stripping, so just don't

override_dh_auto_install:
	# docker-ce-cli install
	install -D -m 0644 $(shell pwd)/go/src/github.com/docker/cli/contrib/completion/fish/docker.fish debian/docker-ce-cli/usr/share/fish/vendor_completions.d/docker.fish
	install -D -m 0644 $(shell pwd)/go/src/github.com/docker/cli/contrib/completion/zsh/_docker debian/docker-ce-cli/usr/share/zsh/vendor-completions/_docker
	install -D -m 0755 $(shell pwd)/go/src/github.com/docker/cli/build/docker debian/docker-ce-cli/usr/bin/docker
	install -D -m 0755 $(shell pwd)/go/src/github.com/docker/buildx/bin/docker-buildx debian/docker-ce-cli/usr/libexec/docker/cli-plugins/docker-buildx
	# docker-ce install
	install -D -m 0644 $(shell pwd)/sources/docker.service debian/docker-ce/lib/systemd/system/docker.service
	install -D -m 0644 $(shell pwd)/sources/docker.socket debian/docker-ce/lib/systemd/system/docker.socket
	install -D -m 0755 $(shell readlink -e sources/engine/bundles/dynbinary-daemon/dockerd) debian/docker-ce/usr/bin/dockerd
	install -D -m 0755 $(PREFIX)/docker-proxy debian/docker-ce/usr/bin/docker-proxy
	install -D -m 0755 $(PREFIX)/docker-init debian/docker-ce/usr/bin/docker-init
	# docker-ce-rootless-extras install
	install -D -m 0755 $(PREFIX)/rootlesskit debian/docker-ce-rootless-extras/usr/bin/rootlesskit
	install -D -m 0755 $(PREFIX)/rootlesskit-docker-proxy debian/docker-ce-rootless-extras/usr/bin/rootlesskit-docker-proxy
	install -D -m 0755 sources/engine/contrib/dockerd-rootless.sh debian/docker-ce-rootless-extras/usr/bin/dockerd-rootless.sh
	install -D -m 0755 sources/engine/contrib/dockerd-rootless-setuptool.sh debian/docker-ce-rootless-extras/usr/bin/dockerd-rootless-setuptool.sh
	# TODO: how can we install vpnkit?

override_dh_installinit:
	# use "docker" as our service name, not "docker-ce"
	dh_installinit --name=docker

override_dh_shlibdeps:
	dh_shlibdeps --dpkg-shlibdeps-params=--ignore-missing-info

override_dh_install:
	dh_install
	# TODO Can we do this from within our container?
	dh_apparmor --profile-name=docker-ce -pdocker-ce

%:
	dh $@ --with=bash-completion $(shell command -v dh_systemd_enable > /dev/null 2>&1 && echo --with=systemd)
