#!/usr/bin/make -f
# -*- mode: makefile; coding: utf-8 -*-

export DH_VERBOSE=1
# see EXAMPLES in dpkg-buildflags(1) and read /usr/share/dpkg/*
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/default.mk

export PYBUILD_NAME = nilearn
export PYBUILD_TEST_NOSE = 1
export PYBUILD_BEFORE_TEST = cp -v {dir}/setup.cfg {build_dir}

# french names make setup.py unhappy on non-utf systems
export LC_ALL = C.UTF-8

srcpkg = $(shell LC_ALL=C dpkg-parsechangelog | grep '^Source:' | cut -d ' ' -f 2,2)
debver = $(shell LC_ALL=C dpkg-parsechangelog | grep '^Version:' | cut -d ' ' -f 2,2 )
upstreamver = $(shell echo $(debver) | cut -d '-' -f 1,1 )

# this figures out the last merge point from 'master' into the Debian branch and
# then described this commit relative to the last release tag
# For this to make any sense the local master branch must track upstream's
# master or whatever other source branch.
gitver = $(shell [ -x /usr/bin/git ] && git describe --tags --match '[0-9]\.[0-9a-z]*' $$(git merge-base -a HEAD master) | sed -e 's/-/+git/')~dfsg.1

PYVER = $(shell pyversions -vd)

export  http_proxy=http://127.0.0.1:9/
export  https_proxy=http://127.0.0.1:9/

# Mega rule
%:
	dh $@  --with python2,python3 --buildsystem=pybuild

override_dh_install:
	rm -f .pybuild/*/build/.coverage
	if [ -x /usr/bin/dh_numpy ]; then dh_numpy;	fi
	dh_install

# immediately useable documentation and exemplar scripts/data
# this anticipates the documentation actually being built at some
# point
override_dh_compress:
	: # I: Avoiding compression of what should not be compressed
	dh_compress -X.py -X.html -X.pdf -X.css -X.jpg -X.txt -X.js -X.json -X.rtc -Xobjects.inv

override_dh_clean:
	# I: Custom cleaning
	rm -f .coverage
	rm -rf nilearn.egg-info build
	-find . -name '*.pyc' -delete
	dh_clean

# run this target manually whenever a merge from upstream's master into the
# Debian branch is done.
dfsg-source-tree:
	-quilt pop -a
	@echo "Testing for uncommited changes"
	@git diff --quiet HEAD
# binaries without sources
	rm -f doc/themes/nilearn/static/jquery.js doc/themes/nilearn/static/jquery.jcarousel.min.js doc/themes/nilearn/static/doctools.js
# commit any cleanup results
	@if ! git diff --quiet HEAD; then \
		git commit -e -a -m "Remove unwanted (e.g. non-DFSG-compliant) content"; \
	fi

# make orig tarball from repository content
get-orig-source: dfsg-source-tree
	# orig tarball, turn directory into something nicer
	git archive --format=tar --prefix=$(srcpkg)-$(gitver)/ HEAD | \
		gzip -9 > $(srcpkg)_$(gitver).orig.tar.gz

# check that DSC patches for backports still apply
maint-check-dsc-patches:
	@for p in debian/patches/*-dsc-patch; \
		do echo "check $$p"; \
		patch -p1 --dry-run < $$p || exit 1 ; \
	done

