#!/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

# only python2.7 supported currently
PYTHONDIR=/usr/lib/python2.7/dist-packages/

PYTHON2:=$(shell pyversions -r)
install_dir=$(call py_libdir, $(PYTHON2))

%:
	dh $@ --with sphinxdoc,python2

override_dh_auto_build:
	dh_auto_build
	cp etc/lava-server.init debian/
	cp etc/lava-server-gunicorn.init debian/
	cp etc/lava-publisher.init debian/
	[ ! -f etc/lava-master.init ] || cp etc/lava-master.init debian/
	python setup.py build
	make -C doc/ html SPHINXOPTS="-j 4 -D today=\"$(BUILD_DATE)\""
	make -C doc/v1/ html SPHINXOPTS="-j 4 -D today=\"$(BUILD_DATE)\""
	make -C doc/v2/ html SPHINXOPTS="-j 4 -D today=\"$(BUILD_DATE)\""
	make -C man/ man SPHINXOPTS="-j 4 -D today=\"$(BUILD_DATE)\""

override_dh_auto_install:
	dh_auto_install
	python setup.py install --root=$(CURDIR)/debian/lava-server/ --install-layout=deb
	# dispatcher-master zeromq support, lava-server.init handled by auto_install.
	dh_installinit --name lava-master
	dh_installinit --name lava-server-gunicorn
	dh_installinit --name lava-publisher
	dh_installman -plava-server man/_build/man/lava-server.1
	dh_installman -plava-server man/_build/man/lava-daemon.1
	dh_installman -plava-server man/_build/man/lava-master.1
	[ -f debian/lava-server/usr/bin/lava-server ] || cp debian/lava-server/$(PYTHONDIR)/lava_server/manage.py debian/lava-server/usr/bin/lava-server; chmod a+x debian/lava-server/usr/bin/lava-server
	# move ssfs script to /usr/sbin
	mkdir debian/lava-server/usr/sbin/
	mv debian/lava-server/usr/bin/lava-mount-masterfs debian/lava-server/usr/sbin/
	# skip unwanted licence file specified in copyright
	$(RM) debian/lava-server/$(PYTHONDIR)/google_analytics/LICENSE
	# handle embedded JS
	python ./share/javascript.py -f ./share/javascript.yaml --remove

override_dh_compress:
	dh_compress -X.yaml

override_dh_auto_clean:
	dh_auto_clean
	$(RM) -r lava_server.egg-info doc/_build dist/ .idea/ jquery.cookie.js
	$(RM) -r versiontools-*.egg/
	[ ! -f doc/Makefile ] || make -C doc/ clean
	[ ! -f doc/v1/Makefile ] || make -C doc/v1/ clean
	[ ! -f doc/v2/Makefile ] || make -C doc/v2/ clean
	[ ! -f man/Makefile ] || make -C man/ clean
	debconf-updatepo

# 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-server-$(VERSION).tar.gz ../lava-server_$(VERSION).orig.tar.gz
	$(RM) -r versiontools-*.egg/
