#!/usr/bin/make -f

# DH_VERBOSE := 1
export LC_ALL=C.UTF-8
export PYBUILD_TEST_ARGS={dir}/bmtk/tests/

include /usr/share/dpkg/default.mk

# for hardening you might like to uncomment this:
# export DEB_BUILD_MAINT_OPTIONS=hardening=+all

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

override_dh_auto_build: export http_proxy=127.0.0.1:9
override_dh_auto_build: export https_proxy=127.0.0.1:9
override_dh_auto_build:
	dh_auto_build
	sphinx-apidoc -f -o docs/autodocs/source/bmtk/ bmtk
	PYTHONPATH=. python3 -m sphinx -N -bhtml docs/autodocs/source/ build/html # HTML generator

override_dh_install:
	find . -name "*" -size 0 -print0 -exec rm {\} \;
	find . -name "*.json" -exec chmod -x {\} \;
	find . -name "*.nml" -exec chmod -x {\} \;
	find . -name "*.swc" -exec chmod -x {\} \;
	find . -name "*.csv" -exec chmod -x {\} \;
	find . -name "*.h5" -exec chmod -x {\} \;
	find . -name tutorial_filter_models.html | xargs sed -i '/<img[[:space:]]/ s/alt\=\"[^"]*"//g'
	find . -name tutorial_pointnet_modeling.html | xargs sed -i '/<img[[:space:]]/ s/alt\=\"[^"]*"//g'
	dh_install

EXAMPLES_ROOT = $(CURDIR)/debian/python3-bmtk-examples
EXAMPLES_DIR = $(EXAMPLES_ROOT)/usr/share/doc/python3-bmtk-examples/examples
execute_after_dh_installexamples-indep:
	@ echo "I: Fix examples headers for running as python3 scripts."
	find $(EXAMPLES_DIR)/ \
		-name "*.py" \
		-exec sed -i '1i\#!/usr/bin/python3' {\} \;

# For some reason, __init__.py and other empty files get removed at some point.
# Restoring them fixes attempts to run the test suite when running multiple
# binary builds in a row.
RESTORE_EMPTY_FILES = \
	bmtk/tests/__init__.py \
	bmtk/tests/utils/reports/spike_trains/__init__.py \
	bmtk/tests/utils/reports/compartment/test_compartment_report.py \
	bmtk/tests/utils/reports/compartment/__init__.py \
	bmtk/tests/simulator/__init__.py \
	bmtk/tests/simulator/popnet/__init__.py \
	bmtk/tests/simulator/pointnet/__init__.py \
	bmtk/tests/simulator/bionet/__init__.py \
	bmtk/simulator/core/__init__.py \
	bmtk/simulator/core/modules/__init__.py
execute_after_dh_auto_clean:
	touch $(RESTORE_EMPTY_FILES)
