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

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

## Disable compiler cache (`go env GOCACHE`, `go help cache`).
export GOCACHE=off

export HOME=$(CURDIR)/debian/tmp

#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

# temporary build path (see http://golang.org/doc/code.html#GOPATH)
OUR_GOPATH := $(CURDIR)/.gopath
export GOPATH := $(OUR_GOPATH)

export DOCKER_VERSION := $(shell cat VERSION)
export DOCKER_GITCOMMIT := $(shell ./debian/helpers/gitcommit.sh $(DOCKER_VERSION))
ifndef DOCKER_GITCOMMIT
  $(error Missing DOCKER_GITCOMMIT - see debian/upstream-version-gitcommits)
endif

export BUILDTIME := $(shell dpkg-parsechangelog -SDate)

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

## 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 ambient

## prefer Go 1.7 explicitly if it's available (golang-1.7-go)
#export PATH := /usr/lib/go-1.7/bin:$(PATH)

APPARMOR_RECOMMENDS := $(shell dpkg-vendor --is Ubuntu && echo apparmor)
BUILT_LIBC := $(shell dpkg-query -f '$${source:Package} (= $${source:Version})' -W libc-dev-bin)

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

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
## $(OUR_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 $(OUR_GOPATH)/src/github.com/docker/cli
	mkdir -pv $(OUR_GOPATH)/src/github.com/docker/docker
	mkdir -pv $(OUR_GOPATH)/src/github.com/docker/libnetwork
	mkdir -pv $(OUR_GOPATH)/src/github.com/docker/swarmkit
	mkdir -pv $(OUR_GOPATH)/src/github.com/docker/distribution
	mkdir -pv $(OUR_GOPATH)/src/github.com/docker/go-events
	mkdir -pv $(OUR_GOPATH)/src/github.com/docker/go-metrics
	mkdir -pv $(OUR_GOPATH)/src/github.com/containerd/containerd

#	## 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              $(OUR_GOPATH)/src/github.com/docker/cli
	cp -ra  cli           $(OUR_GOPATH)/src/github.com/docker/cli

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

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

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

	$(RM) -r              $(OUR_GOPATH)/src/github.com/docker/distribution
	cp -ra  distribution  $(OUR_GOPATH)/src/github.com/docker/distribution

	$(RM) -r              $(OUR_GOPATH)/src/github.com/docker/go-events
	cp -ra  go-events     $(OUR_GOPATH)/src/github.com/docker/go-events

	$(RM) -r              $(OUR_GOPATH)/src/github.com/docker/go-metrics
	cp -ra  go-metrics    $(OUR_GOPATH)/src/github.com/docker/go-metrics

	$(RM) -r              $(OUR_GOPATH)/src/github.com/containerd/containerd
	cp -ra  containerd    $(OUR_GOPATH)/src/github.com/containerd/containerd

#	cp -ra  containerd/vendor/github.com/containerd/continuity    $(OUR_GOPATH)/src/github.com/containerd/
#	cp -ra  engine/vendor/github.com/docker/distribution          $(OUR_GOPATH)/src/github.com/docker/

override_dh_auto_build-indep:
	# noop

override_dh_auto_build-arch:
## order is important:

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

	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

	export DH_GOPKG="github.com/containerd/containerd" ;\
        export REV="`grep $${DH_GOPKG} engine/vendor.conf | head -1 | awk '{print $$2}'`" ;\
        dh_auto_build -v -- -tags "$(DOCKER_BUILDTAGS)" \
          -ldflags "-X $${DH_GOPKG}/version.Version=$(DOCKER_VERSION) -X $${DH_GOPKG}/version.Revision=$${REV}"

	DH_GOPKG="github.com/docker/go-events"                 dh_auto_build -v
	DH_GOPKG="github.com/docker/go-metrics"                dh_auto_build -v
	
##	build ("cd" first to ensure we build from within GOPATH)
	cd '$(OUR_GOPATH)/src/github.com/docker/docker' \
        && VERSION=$(DOCKER_VERSION) ./hack/make.sh dynbinary
	
	cd '$(OUR_GOPATH)/src/github.com/docker/cli' \
        && DISABLE_WARN_OUTSIDE_CONTAINER=1 LDFLAGS='' \
           $(MAKE) VERSION=$(DOCKER_VERSION) GITCOMMIT=$(DOCKER_GITCOMMIT) dynbinary manpages
	
#	# compile man pages
#	./man/md2man-all.sh -q

override_dh_auto_test-indep:
	DH_GOLANG_EXCLUDES="integration-cli integration test/integration" \
        dh_auto_test -v --max-parallel=4 -- -short

override_dh_auto_test-arch:
###     [[[ 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 '$(OUR_GOPATH)/src/$(DH_GOPKG)' \
#        && TESTFLAGS='-test.short' ./hack/make.sh test-unit
	cd $(OUR_GOPATH)/src/github.com/docker/docker \
        && TESTFLAGS='-test.short' ./hack/test/unit

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

	.gopath/bin/containerd -version
endif

override_dh_install:
	## Do not install extra license files:
	dh_install -XLICENSE

override_dh_auto_install:
	## skip dh_auto_install to stop dh_golang trying to copy bits
	## for /usr/share/gocode (we use dh_install/dh-exec 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 'apparmor:Recommends=$(APPARMOR_RECOMMENDS)' >> debian/docker.io.substvars
	echo 'libc:Built-Using=$(BUILT_LIBC)' >> debian/docker.io.substvars
	dh_gencontrol
