#!/usr/bin/make -f

PKD := $(abspath $(dir $(MAKEFILE_LIST)))
PKG := $(shell python -c "from debian.changelog import Changelog; print(Changelog(open(\"$(PKD)/changelog\",'r')).package)")
UVER := $(shell python -c "from debian.changelog import Changelog; print(Changelog(open('$(PKD)/changelog','r')).upstream_version)")
DTYPE = +repack
VER ?= $(subst $(DTYPE),,$(UVER))

PYTHON2=$(shell pyversions -vr)

# see https://wiki.debian.org/Python/LibraryStyleGuide
export http_proxy=http://127.0.9.1:9

%:
	dh $@ --with python2,sphinxdoc

ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
test-python%:
	python$* test/test.py
	python$* test/tss_test.py

override_dh_auto_test: $(PYTHON2:%=test-python%)
endif

override_dh_auto_build:
	./build_it
	# build the man pages for the binary package
	python setup.py build_sphinx -b man

override_dh_auto_install-arch:
	dh_numpy
	dh_auto_install

override_dh_auto_install-indep:
	# build the html pages for the -doc package
	python setup.py build_sphinx -b html

override_dh_installchangelogs:
	dh_installchangelogs -k doc/history.rst

override_dh_sphinxdoc-arch:
	# dh_sphinxdoc doesn't fail silently when there are no docs

override_dh_auto_clean:
	./clean
	-rm -rf *.egg-info HTSeq/_version.py
	-rm example_data/minus.wig example_data/plus.wig
	-rm example_data/test.fa example_data/test.fq
	dh_auto_clean

# need an explicit clean target to deal with the file named clean
clean:
	dh $@ --with python2,sphinxdoc

get-orig-source: $(PKG)_$(VER)$(DTYPE).orig.tar.xz

UURL = http://svn.code.sf.net/p/htseq/code/tags/
$(PKG)_$(VER)$(DTYPE).orig.tar.xz: $(info I: VER=$(VER))
	$(if $(wildcard $(PKG)-$(VER)),$(error $(PKG)-$(VER) exist, aborting..))
	svn checkout --config-option config:miscellany:use-commit-times=yes \
	             $(UURL)/release_$(UVER) $(PKG)-$(VER) \
	|| $(RM) -r $(PKG)-$(VER)
	@echo "Clean-up..."
	cd $(PKG)-$(VER) \
	&& find . -depth -name ".svn" -exec $(RM) -r '{}' \;
	@echo "# Packing..."
	find -L $(PKG)-$(VER) -xdev -type f -print | sort | \
	XZ_OPT="-6v" tar caf "$(PKG)_$(VER)$(DTYPE).orig.tar.xz" -T- --owner=root --group=root --mode=a+rX \
	&& $(RM) -r "$(PKG)-$(VER)"

.PHONY: clean get-orig-source
