#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper.
#
# This file was originally written by Joey Hess and Craig Small.
# As a special exception, when this file is copied by dh-make into a
# dh-make output file, you may use that output file without restriction.
# This special exception was added by Craig Small in version 0.37 of dh-make.
#
# Modified to make a template file for a multi-binary package with separated
# build-arch and build-indep targets  by Bill Allombert 2001

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

# Prevent setuptools/distribute from accessing the internet.
export http_proxy = http://127.0.9.1:9

export LAST_CHANGE=$(shell dpkg-parsechangelog -S Date)
export BUILD_DATE=$(shell LC_ALL=C date -u "+%B %d, %Y" -d "$(LAST_CHANGE)")

# This has to be exported to make some magic below work.
export DH_OPTIONS

%:
	dh $@ --with python2

override_dh_auto_build:
	[ ! -f etc/lava-slave.init ] || cp etc/lava-slave.init debian/lava-dispatcher.lava-slave.init
	dh_auto_build
	python setup.py build
	make -C man/ man SPHINXOPTS="-D today=\"$(BUILD_DATE)\""

override_dh_auto_install:
	dh_auto_install
	dh_installman -plava-dispatcher man/_build/man/lava-dispatch.1
	# support the optional slave init daemon
	[ ! -f debian/lava-dispatcher.lava-slave.init ] || dh_installinit --name lava-slave
	python setup.py install --root=$(CURDIR)/debian/lava-dispatcher/ --install-layout=deb
	$(RM) debian/lava-dispatcher/etc/init.d/lava-slave.init
	mv -v debian/lava-dispatcher/usr/bin/lava-dispatch debian/lava-dispatcher/usr/sbin/

override_dh_fixperms:
	dh_fixperms -X debian/lava-dispatcher/usr/lib/python2.7/dist-packages/lava_dispatcher/device/dynamic_vm_keys/lava
	# vm_groups use a shared private key - use the same location for debian & ubuntu
	chmod 0600 debian/lava-dispatcher/usr/lib/python2.7/dist-packages/lava_dispatcher/device/dynamic_vm_keys/lava

override_dh_auto_clean:
	dh_auto_clean
	$(RM) -r lava_dispatcher.egg-info setup.cfg .idea/
	$(RM) -r versiontools-*.egg/ doc/build/ doc/html/
	[ ! -f man/Makefile ] || make -C man/ clean

# get upstream version for get-orig-source target
VERSION:= $(shell dpkg-parsechangelog | grep '^Version: ' | sed -e 's/^Version: //' | sed -e 's/\-.*$$//')

sdist-orig:
	python setup.py sdist
	mv -v dist/lava-dispatcher-$(VERSION)*.tar.gz ../lava-dispatcher_$(VERSION).orig.tar.gz
	$(RM) -r versiontools-*.egg/
