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

include /usr/share/dpkg/default.mk

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

# The engine and cli are built and tested using upstream scripts
# and Makefiles, rather than dh_auto_*. Therefore we can't rely
# on dh to set the environment for us, we have to do it here.
export GOPATH := $(CURDIR)/_build
export GOCACHE := $(GOPATH)/go-build
export GO111MODULE := off
export GOPROXY := off

#export DH_GOLANG_BUILDPKG := github.com/docker/docker github.com/docker/cli
export DH_GOLANG_GO_GENERATE := 1
export DH_GOLANG_INSTALL_ALL := 1
export DH_GOLANG_INSTALL_EXTRA := testdata/
export DH_GOLANG_EXCLUDES := \
    integration-cli \
    vendor

# Version and commits
DOCKER_VERSION := $(DEB_VERSION_UPSTREAM)
ENGINE_GITCOMMIT := $(shell ./debian/helpers/gitcommit.sh engine $(DOCKER_VERSION))
ifndef ENGINE_GITCOMMIT
  $(error Missing ENGINE_GITCOMMIT - see debian/helpers/engine-gitcommits)
endif
CLI_GITCOMMIT    := $(shell ./debian/helpers/gitcommit.sh cli $(DOCKER_VERSION))
ifndef CLI_GITCOMMIT
  $(error Missing CLI_GITCOMMIT - see debian/helpers/cli-gitcommits)
endif

# cli does not support SOURCE_DATE_EPOCH
# https://github.com/docker/cli/pull/2855
CLI_BUILDTIME := $(shell date -u -d "@$(SOURCE_DATE_EPOCH)" --rfc-3339 ns | sed -e 's/ /T/')

# engine: have "make.sh" keep "bundle/$(DOCKER_VERSION)" around between runs
# (we clean it up appropriately ourselves)
ENGINE_KEEPBUNDLE := 1

ENGINE_BUILDTAGS := apparmor seccomp
ENGINE_TESTFLAGS := -test.short

BUILT_LIBC := $(shell dpkg-query -f '$${source:Package} (= $${source:Version})' -W libc-dev-bin)

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

override_dh_golang:
## Set DH_GOLANG_BUILDPKG so that Built-Using is properly generated.
	DH_GOLANG_BUILDPKG="\
	github.com/docker/cli/... github.com/docker/docker/... \
	github.com/docker/libnetwork/... github.com/docker/swarmkit/...\
	" dh_golang -O--buildsystem=golang -O--builddirectory=_build

override_dh_clean:
	dh_clean
	$(RM) -v -r autogen bundles man/man*/
#	-find . -type f -name '*.pb.go' -delete -printf 'removed %p\n'            ## delete generated .pb.go files
	## Remove Files-Excluded (when built from checkout or non-DFSG tarball):
	$(RM) -rv `perl -0nE 'say grep { $$_=q{ */}.$$_ } split(/\s+/,$$1) if m{^Files\-Excluded:\s*(.*?)(?:\n\n|^Files|^Comment:)}sm;' debian/copyright`
	-find */vendor -mindepth 1 -type d -empty -delete -printf 'removed %p\n'

override_dh_auto_configure:
## dh_auto_configure symlinks dirs from "/usr/share/gocode/src" into
## $(GOPATH) unless folders already exist there in which case files
## are copied. Pre-making directories allows us to use debhelper to
## create proper GOPATH source tree yet have certain directories writable
## even if they are provided by already installed package.
	mkdir -pv $(GOPATH)/src/github.com/docker/cli
	mkdir -pv $(GOPATH)/src/github.com/docker/docker
	mkdir -pv $(GOPATH)/src/github.com/docker/libnetwork
	mkdir -pv $(GOPATH)/src/github.com/docker/swarmkit

## Prepare builddirectory but throw away sourcedirectory.
	DH_GOPKG="__IGNORE__" dh_auto_configure

## Each package must be removed from working tree first, in case some
## of the packages that we are going to build are already installed.
## This is also useful for building in unclean environment.
##
## This is _necessary_ to avoid merging packages that we build with
## pre-installed packages. See #827226 for details.
## Debhelper knows only about "main" DH_GOPKG package but we build several
## and we have to ensure that build tree is properly prepared.
	$(RM) -r              $(GOPATH)/src/github.com/docker/cli
	cp -ra  cli           $(GOPATH)/src/github.com/docker/cli

	$(RM) -r              $(GOPATH)/src/github.com/docker/docker
	cp -ra  engine        $(GOPATH)/src/github.com/docker/docker

	$(RM) -r              $(GOPATH)/src/github.com/docker/libnetwork
	cp -ra  libnetwork    $(GOPATH)/src/github.com/docker/libnetwork

	$(RM) -r              $(GOPATH)/src/github.com/docker/swarmkit
	cp -ra  swarmkit      $(GOPATH)/src/github.com/docker/swarmkit

override_dh_auto_build:
## Order is important:

	DH_GOPKG="github.com/docker/libnetwork"                dh_auto_build -v

##	## FIXME: DH_GOLANG_BUILDPKG --> DH_GOPKG
	DH_GOLANG_BUILDPKG="github.com/docker/swarmkit"        dh_auto_build -v

##	build ("cd" first to ensure we build from within GOPATH)
	cd '$(GOPATH)/src/github.com/docker/docker' \
        && KEEPBUNDLE=$(ENGINE_KEEPBUNDLE) VERSION=$(DOCKER_VERSION) DOCKER_BUILDTAGS='$(ENGINE_BUILDTAGS)' \
	   DOCKER_GITCOMMIT=$(ENGINE_GITCOMMIT) PRODUCT=docker ./hack/make.sh dynbinary
	
	cd '$(GOPATH)/src/github.com/docker/cli' \
        && LDFLAGS='' DISABLE_WARN_OUTSIDE_CONTAINER=1 BUILDTIME='$(CLI_BUILDTIME)' \
           $(MAKE) VERSION=$(DOCKER_VERSION) GITCOMMIT=$(CLI_GITCOMMIT) dynbinary manpages
	
override_dh_auto_test:
###     [[[ DO NOT DISABLE TESTS ]]]  [[[ DO *NOT* DISABLE TESTS ]]]
	DH_GOLANG_EXCLUDES="integration-cli integration test/integration" \
        dh_auto_test -v --max-parallel=4 -- -short
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))

	## Test engine:
	cd $(GOPATH)/src/github.com/docker/docker \
        && TESTFLAGS='$(ENGINE_TESTFLAGS)' ./hack/test/unit

	## Test CLI:
	cd $(GOPATH)/src/github.com/docker/cli \
        && DISABLE_WARN_OUTSIDE_CONTAINER=1 $(MAKE) test-unit

endif

override_dh_install:
	## Rename binaries
	cd $(GOPATH)/src/github.com/docker/docker/bundles/dynbinary-daemon \
	&& mv dockerd-$(DOCKER_VERSION) dockerd
	cd $(GOPATH)/src/github.com/docker/cli/build \
	&& mv docker-linux-$$(go env GOHOSTARCH) docker
	cd $(GOPATH)/bin \
	&& mv proxy docker-proxy
	## Do not install extra license files:
	dh_install -XLICENSE
	## Apparmor thing, not sure it's needed, we don't install a profile
	dh_apparmor --profile-name=docker.io -pdocker.io

override_dh_auto_install:
	## skip dh_auto_install to stop dh_golang trying to copy bits
	## for /usr/share/gocode (we use dh_install for that)

override_dh_installinit:
	dh_installinit -v --name=docker --no-stop-on-upgrade --no-restart-after-upgrade

override_dh_installsystemd:
	dh_installsystemd -v --name=docker --no-stop-on-upgrade --no-restart-after-upgrade

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

override_dh_gencontrol:
	echo 'libc:Built-Using=$(BUILT_LIBC)' >> debian/docker.io.substvars
	dh_gencontrol
