#!/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
#	-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_clean:
	dh_auto_clean
	
	# autogen is created by hack/make.sh
	# bundles is created by hack/make.sh
	# dockerversion/version_autogen.go is created by hack/make.sh
	# man/man*/ is created by man/md2man-all.sh
	$(RM) -v -r autogen bundles dockerversion/version_autogen.go man/man*/

override_dh_auto_configure:
	mkdir -pv $(OUR_GOPATH)/src/github.com/docker/cli             && cp -r cli/*           $(OUR_GOPATH)/src/github.com/docker/cli/
	mkdir -pv $(OUR_GOPATH)/src/github.com/docker/docker          && cp -r engine/*        $(OUR_GOPATH)/src/github.com/docker/docker/
	mkdir -pv $(OUR_GOPATH)/src/github.com/docker/libnetwork      && cp -r libnetwork/*    $(OUR_GOPATH)/src/github.com/docker/libnetwork/
	mkdir -pv $(OUR_GOPATH)/src/github.com/docker/swarmkit        && cp -r swarmkit/*      $(OUR_GOPATH)/src/github.com/docker/swarmkit/
	mkdir -pv $(OUR_GOPATH)/src/github.com/docker/distribution    && cp -r distribution/*  $(OUR_GOPATH)/src/github.com/docker/distribution/
	mkdir -pv $(OUR_GOPATH)/src/github.com/docker/go-events       && cp -r go-events/*     $(OUR_GOPATH)/src/github.com/docker/go-events/
	mkdir -pv $(OUR_GOPATH)/src/github.com/docker/go-metrics      && cp -r go-metrics/*    $(OUR_GOPATH)/src/github.com/docker/go-metrics/
	mkdir -pv $(OUR_GOPATH)/src/github.com/containerd/containerd  && cp -r containerd/*    $(OUR_GOPATH)/src/github.com/containerd/containerd/
	if [ -d "vendor" ]; then cp -r vendor/*  $(OUR_GOPATH)/src/ ;fi
#	    cp -r containerd/vendor/github.com/containerd/continuity    $(OUR_GOPATH)/src/github.com/containerd/
#	    cp -r engine/vendor/github.com/docker/distribution          $(OUR_GOPATH)/src/github.com/docker/
	DH_GOPKG="github.com/docker/docker0none0" dh_auto_configure     # Prepare builddirectory but throw away sourcedirectory.
	
#	# make sure the bits modified at build-time are properly in our GOPATH
#	mkdir -p '$(OUR_GOPATH)/src/$(DH_GOPKG)'
#	#   dockerversion
#	rm -rf '$(OUR_GOPATH)/src/$(DH_GOPKG)/dockerversion'
#	ln -sfT '$(CURDIR)/dockerversion' '$(OUR_GOPATH)/src/$(DH_GOPKG)/dockerversion'
#	#   bundles
#	mkdir -p bundles
#	rm -rf '$(OUR_GOPATH)/src/$(DH_GOPKG)/bundles'
#	cp -r '$(CURDIR)/bundles' '$(OUR_GOPATH)/src/$(DH_GOPKG)/'

override_dh_auto_build-indep:
	# noop

$(OUR_GOPATH)/tini-static:
	dh_auto_configure -v --buildsystem=cmake --sourcedirectory=tini
	dh_auto_build -v --buildsystem=cmake --sourcedirectory=tini

override_dh_auto_build-arch: $(OUR_GOPATH)/tini-static
	## 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' \
        && ./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:
###     [[[ 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)))
	.gopath/bin/containerd -version

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

override_dh_installsystemd:
	dh_installsystemd -v --name=docker

override_dh_installudev:
	# use priority z80 to match the upstream priority of 80
	dh_installudev --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
