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

#export DH_VERBOSE=1

GODIR=go

export DH_GOPKG:=github.com/juju/juju
export BUILDPATH := obj-$(DEB_BUILD_GNU_TYPE)
export GOPATH := $(CURDIR)/$(BUILDPATH)
export PATH:=$(CURDIR)/$(GODIR)/bin:$(GOPATH)/bin:$(PATH)

PKGDIR:=debian/juju
VERSION:=$(shell sed -n 's/^const version = "\(2\.[0-9]\+\)[\.-].*"/\1/p' $(CURDIR)/src/github.com/juju/juju/version/version.go)
ifeq ($(VERSION),)
$(error Invalid version constant in version.go)
endif

DB_DEP = -Vdist:Depends="juju-mongodb (>= 2.4.6)"

%:
	dh $@ --with=golang --buildsystem=golang

COMMON_FLAGS:= -v
supported_archs:= amd64 s390x ppc64el arm64

build_go:
ifneq (,$(filter $(DEB_HOST_ARCH), $(supported_archs)))
	cd $(GODIR)/src && \
	GOROOT_BOOTSTRAP=$$(/usr/bin/go env GOROOT) ./make.bash
else
	:
endif

# Ugly workaround for bundled dependencies
# This is inspiried by LXD's packaging
override_dh_auto_configure: build_go
	dh_auto_configure

	# dh-golang's configure has copied the source tree into GOPATH. But
	# because juju gets some dependencies from the archive and some from
	# the copies bundled in dist, we have to unpick a bunch of what it has
	# done and set it up again.

	# Remove the extra copy of src dh-golang has copied onto GOPATH (or
	# when dh-golang tries to run go install github.com/juju/juju/... things
	# get very confused).

	# Move the juju source aside while we do this.
	mv ${GOPATH}/src/github.com/juju/juju/src/github.com/juju/juju ${GOPATH}/juju

	# Clean GOPATH.
	rm -Rf ${GOPATH}/src

	# copy dependencies from GOPATH.
	cp -R src ${GOPATH}

	# But not the symlink for juju.
	rm -rf ${GOPATH}/src/github.com/juju/juju

	# Link depedencies from where the distro package
	# has installed it, as appropriate.
	# Packaged dependencies
	debian/helpers/link-from-installed github.com/bmizerany/pat
	debian/helpers/link-from-installed launchpad.net/go-dbus
	debian/helpers/link-from-installed gopkg.in/tomb.v2

	# And put the juju source back again.
	mkdir -p ${GOPATH}/src/github.com/juju/
	mv ${GOPATH}/juju/ ${GOPATH}/src/github.com/juju/juju/

override_dh_auto_build:
ifneq (,$(filter $(DEB_HOST_ARCH), $(supported_archs)))
		dh_auto_build -- $(COMMON_FLAGS)
endif

# Don't run the tests -- the juju unit tests are too heavyweight to run during
# package build.
override_dh_auto_test:
	:

override_dh_auto_install:
ifneq (,$(filter $(DEB_HOST_ARCH), $(supported_archs)))
	echo '#!/bin/sh\nexport PATH=/usr/lib/juju-$(VERSION)/bin:"$$PATH"\nexec juju "$$@"' > ${BUILDPATH}/bin/juju-$(VERSION)
	chmod 755 ${BUILDPATH}/bin/juju-$(VERSION)
	mkdir -p ${BUILDPATH}/home
	HOME=${BUILDPATH}/home $(CURDIR)/src/github.com/juju/juju/scripts/generate-docs.py man -o ${BUILDPATH}/juju.1
	dh_install ${BUILDPATH}/bin/juju usr/lib/juju-$(VERSION)/bin
	dh_install ${BUILDPATH}/bin/juju-metadata usr/lib/juju-$(VERSION)/bin
	dh_install ${BUILDPATH}/bin/jujud usr/lib/juju-$(VERSION)/bin
	dh_install ${BUILDPATH}/bin/juju-$(VERSION) usr/bin
	dh_install ${BUILDPATH}/juju.1 usr/lib/juju-$(VERSION)/man/man1
	# Install bash completion scripts
	dh_install src/github.com/juju/juju/etc/bash_completion.d/juju-version \
	           usr/share/bash-completion/completions
	dh_install src/github.com/juju/juju/etc/bash_completion.d/juju \
	           usr/share/bash-completion/completions
	dh_install debian/juju-2.conf usr/lib/sysctl.d
endif

override_dh_link:
	dh_link -pjuju-2.0 usr/bin/juju-$(VERSION) usr/bin/juju
	dh_link -pjuju-2.0 usr/lib/juju-$(VERSION)/man/man1/juju.1.gz usr/share/man/man1/juju.1.gz
	dh_link -pjuju-2.0 usr/lib/juju-$(VERSION)/man/man1/juju.1.gz usr/share/man/man1/juju-$(VERSION).1.gz
	dh_link

override_dh_compress:
ifneq (,$(filter $(DEB_HOST_ARCH), $(supported_archs)))
	dh_compress usr/lib/juju-$(VERSION)/man/man1/juju.1
endif
	dh_compress

override_dh_auto_clean:
	find . -name "*.pyc" -delete || :
	dh_auto_clean

override_dh_builddeb:
	dh_builddeb -- -Zxz

override_dh_gencontrol:
	dh_gencontrol -- $(DB_DEP)
