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

include /usr/share/dpkg/pkg-info.mk

export BUILD_DATE=$(shell LC_ALL=C date -u "+%B %d, %Y" -d @"$(SOURCE_DATE_EPOCH)")

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

export PYBUILD_DISABLE=test/python3

PYTHONDIR=/usr/lib/python3/dist-packages/

PYTHON:=$(shell py3versions -r)
install_dir=$(call py_libdir, $(PYTHON))

%:
	dh $@ --with sphinxdoc,python3 --buildsystem=pybuild

override_dh_python3:
	./share/requires.py --distribution debian --suite $(DEB_DISTRIBUTION) --package lava-dispatcher > dispatcher-requirements.txt
	./share/requires.py --distribution debian --suite $(DEB_DISTRIBUTION) --package lava-server > server-requirements.txt
	./share/requires.py --distribution debian --suite $(DEB_DISTRIBUTION) --package lava-common > common-requirements.txt
	dh_python3 -p lava-dispatcher --shebang=/usr/bin/python3 --requires=dispatcher-requirements.txt
	dh_python3 -p lava-server --shebang=/usr/bin/python3 --requires=server-requirements.txt
	dh_python3 -p lava-common --shebang=/usr/bin/python3 --requires=common-requirements.txt

override_dh_auto_build:
	make -C man/ man SPHINXBUILD="/usr/share/sphinx/scripts/python3/sphinx-build" SPHINXOPTS="-D today=\"$(BUILD_DATE)\""
	make -C doc/v2/ html SPHINXBUILD="/usr/share/sphinx/scripts/python3/sphinx-build" SPHINXOPTS="-j 4 -D today=\"$(BUILD_DATE)\""
	$(RM) doc/v2/_build/html/objects.inv
	make -C man/ man SPHINXBUILD="/usr/share/sphinx/scripts/python3/sphinx-build" SPHINXOPTS="-j 4 -D today=\"$(BUILD_DATE)\""

override_dh_auto_install:
	find . -path '*/__pycache__/*' -delete
	find . -type d -name '__pycache__' -empty -delete
	# handle man pages
	dh_installman -plava-dispatcher man/_build/man/lava-run.1
	dh_installman -plava-dispatcher man/_build/man/lava-worker.8
	dh_installman -plava-lxc-mocker man/_build/man/lava-lxc-mocker.7
	dh_installman -plava-lxc-mocker man/_build/man/lxc-attach.1
	dh_installman -plava-lxc-mocker man/_build/man/lxc-create.1
	dh_installman -plava-lxc-mocker man/_build/man/lxc-destroy.1
	dh_installman -plava-lxc-mocker man/_build/man/lxc-device.1
	dh_installman -plava-lxc-mocker man/_build/man/lxc-info.1
	dh_installman -plava-lxc-mocker man/_build/man/lxc-start.1
	dh_installman -plava-lxc-mocker man/_build/man/lxc-stop.1
	dh_installman -plava-server man/_build/man/lava-server.1
	# use setup.py
	python3 setup.py install --root=$(CURDIR)/debian/lava-common/ --no-compile --install-layout=deb lava-common
	rm -rf $(CURDIR)/build
	python3 setup.py install --root=$(CURDIR)/debian/lava-coordinator/ --no-compile --install-layout=deb lava-coordinator
	rm -rf $(CURDIR)/build
	python3 setup.py install --root=$(CURDIR)/debian/lava-dispatcher/ --no-compile --install-layout=deb lava-dispatcher
	rm -rf $(CURDIR)/build
	python3 setup.py install --root=$(CURDIR)/debian/lava-dispatcher-host/ --no-compile --install-layout=deb lava-dispatcher-host
	rm -rf $(CURDIR)/build
	python3 setup.py install --root=$(CURDIR)/debian/lava-server/ --no-compile --install-layout=deb lava-server
	rm -rf $(CURDIR)/build
	# handle embedded JS
	python3 ./share/javascript.py -f ./share/javascript.yaml --remove
	$(RM) $(CURDIR)/debian/lava-server/usr/lib/python3/dist-packages/lava_results_app/static/lava_results_app/js/jquery.flot.canvas.js

override_dh_fixperms-arch:
	dh_fixperms -X debian/lava-dispatcher/usr/lib/python3/dist-packages/lava_dispatcher/dynamic_vm_keys/lava
	# ssh connections use a shared private key - use the same location for debian & ubuntu
	chmod 0600 debian/lava-dispatcher/usr/lib/python3/dist-packages/lava_dispatcher/dynamic_vm_keys/lava
	chmod 0644 debian/lava-dispatcher/usr/lib/python3/dist-packages/lava_dispatcher/dynamic_vm_keys/lava.pub

override_dh_installinit:
	dh_installinit -Nlava-server

override_dh_installsystemd:
	dh_installsystemd -Xlava-celery-worker.service -Xlava-docker-worker.service

override_dh_compress:
	dh_compress -X.yaml
	$(RM) -r debian/lava/usr/lib
	$(RM) -r debian/lava/usr/bin

override_dh_auto_clean:
	python3 setup.py clean -a
	find . -path '*/__pycache__/*' -delete
	$(RM) -r lava.egg-info .idea/ build/
	$(RM) -r .eggs/ versiontools-*.egg/ doc/build/ doc/html/
	find . -type d -name '__pycache__' -empty -delete
	$(RM) -r dist/
	$(RM) debian/*.service
	$(RM) common-requirements.txt dispatcher-requirements.txt server-requirements.txt
	[ ! -f doc/v2/Makefile ] || make -C doc/v2/ clean
	[ ! -f man/Makefile ] || make -C man/ clean