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

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

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

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

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

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

%:
	dh $@ --with sphinxdoc,systemd,python3

override_dh_python3:
	dh_python3 --shebang=/usr/bin/python3

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

override_dh_auto_install:
	python3 setup.py install --root=$(CURDIR)/debian/lava-server/ --install-layout=deb
	[ -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
	find . -path '*/__pycache__/*' -delete
	find . -type d -name '__pycache__' -empty -delete
	dh_installman -plava-server man/_build/man/lava-server.1
	# skip unwanted licence file specified in copyright
	$(RM) debian/lava-server/$(PYTHONDIR)/google_analytics/LICENSE
	# handle embedded JS
	python3 ./share/javascript.py -f ./share/javascript.yaml --remove
	# handle systemd services
	cp ./etc/*.service debian/
	dh_systemd_enable --name lava-server-gunicorn
	dh_systemd_enable --name lava-logs
	dh_systemd_enable --name lava-server
	dh_systemd_enable --name lava-publisher
	dh_systemd_enable --name lava-master
	dh_systemd_start --name lava-server-gunicorn
	dh_systemd_start --name lava-logs
	dh_systemd_start --name lava-server
	dh_systemd_start --name lava-publisher
	dh_systemd_start --name lava-master

override_dh_installinit:
	dh_installinit -Nlava-server

override_dh_compress:
	dh_compress -X.yaml

override_dh_auto_clean:
	python3 setup.py clean -a
	find . -path '*/__pycache__/*' -delete
	find . -type d -name '__pycache__' -empty -delete
	$(RM) -r lava_server.egg-info doc/_build dist/ .idea/ jquery.cookie.js
	$(RM) -r versiontools-*.egg/ build/
	$(RM) debian/*.service
	[ ! -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

