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

#export DH_VERBOSE=1

export DH_GOPKG:=github.com/juju/juju
export GOPATH:=$(CURDIR)/_build
export PATH:=$(CURDIR)/_build/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

ifeq ($(shell lsb_release -cs | sed -r 's/saucy|precise/old/'),old)
	DB_DEP = -Vdist:Depends="mongodb-server (>= 2.4.6)"
else
	DB_DEP = -Vdist:Depends="juju-mongodb (>= 2.4.6)"
endif

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

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

# NOTE: ensure /usr/share/gocode is use in preference to any
#       embedded source for dependencies.
override_dh_auto_configure:
	./debian/helpers/setup-build-directory.py

override_dh_auto_build:
ifneq (,$(filter $(DEB_HOST_ARCH), $(supported_archs)))
		dh_auto_build -- $(COMMON_FLAGS)
		rm _build/bin/jujud
		# re-link jujud with specific options
		go install $(COMMON_FLAGS) $(JUJUD_FLAGS) github.com/juju/juju/cmd/jujud
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 "$$@"' > _build/bin/juju-$(VERSION)
	chmod 755 _build/bin/juju-$(VERSION)
	mkdir -p _build/home
	HOME=_build/home $(CURDIR)/src/github.com/juju/juju/scripts/generate-docs.py man -o _build/juju.1
	dh_install _build/bin/juju usr/lib/juju-$(VERSION)/bin
	dh_install _build/bin/juju-metadata usr/lib/juju-$(VERSION)/bin
	dh_install _build/bin/juju-upgrade-mongo usr/lib/juju-$(VERSION)/bin
	dh_install _build/bin/jujud usr/lib/juju-$(VERSION)/bin
	dh_install _build/bin/juju-$(VERSION) usr/bin
	dh_install _build/juju.1 usr/lib/juju-$(VERSION)/man/man1
	# Install version switching bash completion script under 'juju' so first load picks
	# it up, and we have already made sure we remove earlier clashing 1.X packages.
	cp src/github.com/juju/juju/etc/bash_completion.d/juju-version _build/juju
	dh_install _build/juju src/github.com/juju/juju/etc/bash_completion.d/juju-2.0 \
	           usr/share/bash-completion/completions
	dh_install debian/juju-2.0.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-2.0.1.gz
	dh_link

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

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

override_dh_builddeb:
	dh_builddeb -- -Zxz

override_dh_gencontrol:
	dh_gencontrol -- $(DB_DEP)
