#!/usr/bin/make -f
# -*- makefile -*-

export VERSION := $(shell cat VERSION)

export DOCKER_GITCOMMIT := $(shell ./debian/helpers/gitcommit.sh $(VERSION))

# temporary build path (see http://golang.org/doc/code.html#GOPATH)
OUR_GOPATH := $(CURDIR)/.gopath
export GOPATH := $(OUR_GOPATH)
export PATH := /usr/lib/go-1.10/bin:$(PATH)

# AppArmor can be optionally used in Debian and is there by default in Ubuntu, so we need support for it compiled into our binary
# same story with SELinux
export DOCKER_BUILDTAGS := apparmor seccomp selinux

ifneq (,$(filter parallel=%, $(DEB_BUILD_OPTIONS)))
	NUMJOBS := $(patsubst parallel=%, %, $(filter parallel=%, $(DEB_BUILD_OPTIONS)))
	MAKEFLAGS += -j '$(NUMJOBS)'
endif

override_dh_gencontrol:
	# if we're on Ubuntu, we need to Recommends: apparmor
	echo 'apparmor:Recommends=$(shell dpkg-vendor --is Ubuntu && echo apparmor)' >> debian/docker.io.substvars
	echo 'libc:Built-Using=$(shell dpkg-query -f '$${source:Package} (= $${source:Version})' -W libc-dev-bin)' >> debian/docker.io.substvars
	dh_gencontrol

override_dh_auto_configure:
	# set up GOPATH symlink farm
	# libnetwork
	mkdir -p '$(OUR_GOPATH)/src/github.com/docker'
	ln -sfT '$(CURDIR)/libnetwork' '$(OUR_GOPATH)/src/github.com/docker/libnetwork'
	# cli
	mkdir -p '$(OUR_GOPATH)/src/github.com/docker'
	ln -sfT '$(CURDIR)/components/cli' '$(OUR_GOPATH)/src/github.com/docker/cli'
	# engine
	mkdir -p '$(OUR_GOPATH)/src/github.com/docker'
	ln -sfT '$(CURDIR)/components/engine' '$(OUR_GOPATH)/src/github.com/docker/docker'

_require-gitcommit:
	@bash -c '{ [ "$$DOCKER_GITCOMMIT" ]; } || { echo; echo "error: missing DOCKER_GITCOMMIT - see debian/upstream-version-gitcommits"; echo; exit 2; } >&2'

override_dh_auto_build: _build-tini _build-proxy _build-cli _build-manpages _build-engine
	@#

# several "supporting binaries" need to be built and installed
# (https://github.com/docker/docker-ce/blob/v17.12.1-ce/components/engine/hack/dockerfile/install-binaries.sh)

_build-tini: # (docker-init)
	mkdir -p tini/build
	cd tini/build \
		&& cmake .. \
		&& make tini-static

_build-proxy: # (from libnetwork)
	go build -o libnetwork/docker-proxy github.com/docker/libnetwork/cmd/proxy

_build-cli: _require-gitcommit
	DISABLE_WARN_OUTSIDE_CONTAINER=1 \
		make -C components/cli \
			LDFLAGS='' \
			VERSION='$(VERSION)' \
			GITCOMMIT='$(DOCKER_GITCOMMIT)' \
			dynbinary

# https://github.com/docker/docker-ce/blob/v17.12.1-ce/components/cli/scripts/docs/generate-man.sh
# (replacing hard-coded "/tmp/gen-manpages" with "debian/tmp/gen-manpages")
_build-manpages:
	cd '$(OUR_GOPATH)/src/github.com/docker/cli' \
		&& export PATH='$(OUR_GOPATH)/bin':"$$PATH" \
		&& sed 's!/tmp/gen-manpages!$(CURDIR)/debian/tmp/gen-manpages!g' scripts/docs/generate-man.sh \
			| bash

_build-engine: _require-gitcommit
	cd components/engine \
		&& PRODUCT=docker ./hack/make.sh dynbinary

# basic smoke test to ensure binaries built successfully
# (especially since their tests are _really_ involved)
override_dh_auto_test:
	# tini
	./tini/build/tini-static --version
	# cli
	./components/cli/build/docker --version
	# engine
	./components/engine/bundles/latest/dynbinary-daemon/dockerd --version

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

override_dh_auto_install:
	# we use dh_install / dh-exec for installing files where they need to be

# "--no-start" because we have a debconf prompt to determine whether to restart during upgrades
# (which means we get to handle both "start" and "stop" ourselves too)
override_dh_installinit:
	dh_installinit --name=docker --no-start

override_dh_installudev:
	# use priority z80 to match the upstream priority of 80
	dh_installudev --priority=z80

override_dh_install:
	dh_install
	dh_apparmor --profile-name=docker.io -pdocker.io

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

override_dh_auto_clean:
	@# stop debhelper from doing "make clean"

override_dh_systemd_start:
	# We take care of determining whether the docker.io service should be
	# restarted during upgrades or not ourselves, based on the debconf
	# choice made by the user during installation.  For this reason, we
	# invoke dh_systemd_start with "-r" in order to avoid indiscriminately
	# stopping the service during upgrades.
	dh_systemd_start --package=docker.io -r

%:
	dh $@ --with=bash-completion,systemd
