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

# Tell dh-golang where this package lives upstream
export DH_GOPKG := github.com/docker/docker
# Tell dh-golang that we DO need subpackages
export DH_GOLANG_INSTALL_ALL := 1

LIBCONTAINER_GOPKG = github.com/docker/libcontainer

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

# a few helpful variables for deduplication
INITDIR = /usr/lib/docker.io
INITPATH = ${INITDIR}/dockerinit
DOCKER_VERSION = $(shell cat VERSION)

export DOCKER_GITCOMMIT := $(shell ./debian/helpers/gitcommit.sh $(DOCKER_VERSION))
export DOCKER_INITPATH := ${INITPATH}

# good old Ubuntu needs AppArmor
export DOCKER_BUILDTAGS := apparmor


APPARMOR_RECOMMENDS = $(shell dpkg-vendor --is Ubuntu && echo apparmor)
override_dh_gencontrol:
	echo 'apparmor:Recommends=$(APPARMOR_RECOMMENDS)' >> debian/docker.io.substvars
	dh_gencontrol


override_dh_auto_build:
	@bash -c '{ [ "$$DOCKER_GITCOMMIT" ]; } || { echo; echo "error: missing DOCKER_GITCOMMIT - see debian/upstream-version-gitcommits"; echo; exit 2; } >&2'
	
	@# this is especially for easier build-testing of nightlies
	@[ -d libcontainer ] || { [ -d vendor/src/$(LIBCONTAINER_GOPKG) ] && ln -sf vendor/src/$(LIBCONTAINER_GOPKG) libcontainer; }
	
	@# we need to make sure our multitarball libcontainer is in our GOPATH
	@mkdir -p "$$GOPATH/src/$(dir $(LIBCONTAINER_GOPKG))"
	ln -sf "$$(readlink -f libcontainer)" "$(GOPATH)/src/$(dir $(LIBCONTAINER_GOPKG))"
	
	./hack/make.sh dynbinary
	
	# compile man pages
	./docs/man/md2man-all.sh


override_dh_auto_install:
	dh_auto_install
	
	# install docker binary
	mkdir -p debian/docker.io/usr/bin
	mv bundles/${DOCKER_VERSION}/dynbinary/docker-${DOCKER_VERSION} debian/docker.io/usr/bin/docker
	# install docker.io symlink for easier transition
	ln -s docker debian/docker.io/usr/bin/docker.io
	
	# install dockerinit binary
	mkdir -p debian/docker.io/${INITDIR}
	mv bundles/${DOCKER_VERSION}/dynbinary/dockerinit-${DOCKER_VERSION} debian/docker.io/${INITPATH}
	
	# Most of the source of docker does not make a library,
	#   so only ship the reusable parts (and in a separate package).
	# THIS WILL BE AN ACTUAL PACKAGE IN THE _NEXT_ VERSION.
	mkdir -p debian/golang-docker-dev/usr/share/gocode/src/${DH_GOPKG}
	mv -v \
		debian/tmp/usr/share/gocode/src/${DH_GOPKG}/pkg \
		debian/golang-docker-dev/usr/share/gocode/src/${DH_GOPKG}/
	mkdir -p debian/golang-docker-dev/usr/share/gocode/src/$(dir $(LIBCONTAINER_GOPKG))
	@# this is especially for easier build-testing of nightlies
	@[ -d debian/tmp/usr/share/gocode/src/${DH_GOPKG}/libcontainer ] || { [ -d debian/tmp/usr/share/gocode/src/${DH_GOPKG}/vendor/src/$(LIBCONTAINER_GOPKG) ] && mv debian/tmp/usr/share/gocode/src/${DH_GOPKG}/vendor/src/$(LIBCONTAINER_GOPKG) debian/tmp/usr/share/gocode/src/${DH_GOPKG}/libcontainer; }
	mv -v \
		debian/tmp/usr/share/gocode/src/${DH_GOPKG}/libcontainer \
		debian/golang-docker-dev/usr/share/gocode/src/$(dir $(LIBCONTAINER_GOPKG))
	rm -rf debian/tmp/usr/share/gocode


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


override_dh_auto_test:


override_dh_installinit:
	dh_installinit --name=docker


override_dh_installchangelogs:
	dh_installchangelogs CHANGELOG.md


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


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