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

include /usr/share/cdbs/1/rules/debhelper.mk
DEB_PYTHON_SYSTEM = pysupport
DEB_PYTHON_MODULE_PACKAGE = psychopy
DEB_PYTHON_MODULE_PACKAGES = psychopy
include /usr/share/cdbs/1/class/python-distutils.mk

# We will disallow to fail on unstable/sid, but let them pass otherwise
failed_tests_cmd = $(shell dpkg-parsechangelog --count 1 --format rfc822 | awk '/^Distribution:/{print $$2;}' | grep -q -e '\(sid\|unstable\)' && echo "exit 1" || echo ":")

debian/psychopy.1: debian/psychopy.sh
	chmod +x $^
	PYTHONPATH=. help2man -N -n "Creating psychology stimuli in Python" $^ > $@
## cleanup
	rm -f psychopy/__init__.pyc

install/psychopy:: debian/psychopy.1
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	echo "I: Running tests using default Python"
	cd build && xvfb-run -s "-screen 0 1024x768x24 -ac +extension GLX +render -noreset" \
		python ../psychopy/tests/runPytest.py -v -s -m "not needs_sound" || { echo "TESTS FAILED"; $(failed_tests_cmd); }
endif
## Fix up permissions on images -- pushed upstream, but left here
## just to make sure everything is ok
	find $(DEB_DESTDIR) -name \*.png -o -name \*.ico -o -name \*.jpg | \
		xargs chmod a-x
## # Correct interpreter lines
## was pushed upstream, but snippet is left in for possible uses
## 	find $(DEB_DESTDIR) -name \*.py \
##     | xargs sed -i -e '1 s,^#!.*python.*,#!/usr/bin/env python,g'

## convert and install icon's .xpm manually
	convert psychopy/app/Resources/psychopy.png -geometry 32x32 \
      $(DEB_DESTDIR)/usr/share/pixmaps/psychopy.xpm
## Rename upstream's runner
	mv $(DEB_DESTDIR)/usr/bin/psychopyApp.py $(DEB_DESTDIR)/usr/bin/psychopy

# Make tree dfsg-compliant
OREMOTE=
dfsg: dfsg-master
dfsgrelease-%: override OREMOTE=origin/release-
dfsgrelease-%: dfsg-%
	@echo "finished dfsgrelease-$*" # some command is necessary for poor make
dfsg-%:
	@git checkout dfsg
	branch=$(OREMOTE)$*; \
	descr=`git describe $$branch | sed -e 's,-,+git,'`; \
	dfsgtag=$${descr}.dfsg; \
	echo "Merging $${branch} into DFSG and cleaning"; \
	git merge --no-commit -X theirs $$branch || :; \
	git rm -f --ignore-unmatch PsychoPyBuilder/*bak \
	  psychopy/demos/*.ttf psychopy/demos/*.mpg \
	  psychopy/ext/*bits*.* \
	  docs/source/resources/*zip \
	  buildExeInstaller.nsi buildWinDistributions.bat \
	  googlecode_upload.py; \
	git rm -rf --ignore-unmatch iohub_docs psychopy/serial windlls; \
	find -type d -iname win32 -print0 | xargs -0 git rm -rf --ignore-unmatch; \
	diffstat="`git diff --cached $${branch} --stat`"; \
	echo -e "Merge $$branch ($$descr) into DFSG\n\n$$diffstat" | \
		git commit -F- -a && git tag -s -m "Autotagging DFSG version of $$descr" $$dfsgtag && \
	echo -e "Differences between dfsg and $${branch}:\n$$diffstat"; \
	git checkout debian && git merge $${dfsgtag} \
	&& dch -v "$${dfsgtag}-1" "New upstream release"

# immediately useable documentation
#DEB_COMPRESS_EXCLUDE := .py .pdf .html .css .jpg .txt
DEB_INSTALL_CHANGELOGS_ALL := psychopy/CHANGELOG.txt
DEB_INSTALL_DOCS_psychopy := psychopy/README.txt debian/README.Debian-source
DEB_INSTALL_MANPAGES_psychopy := debian/psychopy.1

test::
	LC_ALL=C.UTF-8 PYTHONPATH=$PWD xvfb-run -s "-screen 0 1280x1024x24 -noreset" nosetests -s -v psychopy/tests 2>&1

clean::
	rm -f debian/psychopy.1
	rm -rf build dist PsychoPy.egg-info

# do not actually install demos -- just symlink to them
#DEB_INSTALL_EXAMPLES_psychopy := psychopy/demos/*

