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

DOCKER_GOPKG := github.com/docker/docker

# MUST STAY IN SYNC WITH LIST IN "debian/helpers/download-libcontainer"
MULTI_ORIG_GOPKGS := \
	github.com/docker/libnetwork

# the list of Go packages to include in golang-github-docker-docker-dev
DEV_GOPKGS := \
	github.com/docker/docker/pkg

# temporary build path (see http://golang.org/doc/code.html#GOPATH)
OUR_GOPATH := $(CURDIR)/.gopath
GOPATH_PACKAGED := /usr/share/gocode
EXTRA_GOPATH := $(GOPATH_PACKAGED)

# if we've got the vendored source not purged, let's use it (normally purged via debian/repack.sh)
ifneq (, $(wildcard $(CURDIR)/vendor/src))
	EXTRA_GOPATH := $(CURDIR)/vendor
endif

export GOPATH := $(OUR_GOPATH):$(EXTRA_GOPATH)

# a few helpful variables for deduplication
DOCKER_BINPATH := /usr/bin/docker
DOCKER_VERSION := $(shell cat VERSION)

export DOCKER_GITCOMMIT := $(shell ./debian/helpers/gitcommit.sh $(DOCKER_VERSION))
export BUILDTIME := $(shell dpkg-parsechangelog -SDate)
export DOCKER_INITPATH := /usr/lib/docker.io/dockerinit

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

# since "gccgo" doesn't ship the "dist" go tool (and isn't likely to start), let's use that for now to detect gc vs gccgo
DOCKER_BUILD_TARGET := $(shell go tool dist env > /dev/null 2>&1 && echo dynbinary || echo dyngccgo)


APPARMOR_RECOMMENDS := $(shell dpkg-vendor --is Ubuntu && echo apparmor)
BUILT_LIBC := $(shell dpkg-query -f '$${source:Package} (= $${source:Version})' -W libc-dev-bin)
BUILT_GOLANG := $(shell dpkg-query -f '$${source:Package} (= $${source:Version})' -W $$(dpkg --search $$(readlink -f /usr/bin/go) | cut -d: -f1))

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


setup-gopath:
	# we need to make sure all deps are in our GOPATH
	mkdir -p "$(OUR_GOPATH)"
	@set -ex; \
		for package in $(DOCKER_GOPKG) $(MULTI_ORIG_GOPKGS); do \
			for gopath in $(subst :, ,$(GOPATH)); do \
				if [ -d "$$gopath/src/$$package" ]; then \
					continue 2; \
				fi; \
			done; \
			[ $$package = $(DOCKER_GOPKG) ] \
				&& src=. \
				|| src=$$(basename $$package); \
			mkdir -p "$(OUR_GOPATH)/src/$$(dirname $$package)"; \
			ln -sfT "$$(readlink -f "$$src")" "$(OUR_GOPATH)/src/$$package"; \
		done

override_dh_auto_build:
	@bash -c '{ [ "$$DOCKER_GITCOMMIT" ]; } || { echo; echo "error: missing DOCKER_GITCOMMIT - see debian/upstream-version-gitcommits"; echo; exit 2; } >&2'
	
	./debian/rules setup-gopath
	
	# build "docker" and "dockerinit"
	./hack/make.sh $(DOCKER_BUILD_TARGET)
	
	# compile man pages
	./man/md2man-all.sh -q


override_dh_auto_install:
	# install docker binary
	mkdir -p debian/docker.io/$(dir $(DOCKER_BINPATH))
	cp -aT bundles/$(DOCKER_VERSION)/$(DOCKER_BUILD_TARGET)/docker-$(DOCKER_VERSION) debian/docker.io/$(DOCKER_BINPATH)
	
	# install dockerinit binary
	mkdir -p debian/docker.io/$(dir $(DOCKER_INITPATH))
	cp -aT bundles/$(DOCKER_VERSION)/$(DOCKER_BUILD_TARGET)/dockerinit-$(DOCKER_VERSION) debian/docker.io/$(DOCKER_INITPATH)
	
	# Most of the source of docker does not make a library,
	#   so only ship the reusable parts (and in a separate package).
	@set -ex; \
		for package in $(DEV_GOPKGS); do \
			mkdir -p "debian/golang-github-docker-docker-dev/$(GOPATH_PACKAGED)/src/$$package"; \
			cp -aT "$$(readlink -f "$(OUR_GOPATH)/src/$$package")" "debian/golang-github-docker-docker-dev/$(GOPATH_PACKAGED)/src/$$package"; \
		done


override_dh_strip:
	# the SHA1 of dockerinit is important: don't strip it
	# also, Go has lots of problems with stripping, so just don't


override_dh_auto_test:


override_dh_installinit:
	dh_installinit --name=docker --no-restart-on-upgrade


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


override_dh_auto_clean:
	dh_auto_clean
	
	# OUR_GOPATH is created by us
	rm -rf "$(OUR_GOPATH)"
	
	# 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 -rf autogen bundles dockerversion/version_autogen.go man/man*/


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