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

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

PYVER = $(shell pyversions -vd)

export DEBPYTHON_SUPPORTED=$(PYVER)
export HOME=$(CURDIR)/build
DEB_DESTDIR=$(CURDIR)/debian/psychopy

%:
	dh $@ --with python2 --buildsystem python_distutils

override_dh_auto_install:
	dh_auto_install
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	echo "I: Running tests using default Python"
	cd build \
		&& USER=nobody TRAVIS=true \
		xvfb-run -s "-screen 0 1024x768x24 -ac +extension GLX +render -noreset" \
		python ../psychopy/tests/runPytest.py --tb=line -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 -r chmod a-x
## # Correct interpreter lines
## was pushed upstream, but snippet is left in for possible uses
## 	find $(DEB_DESTDIR) -name \*.py \
##     | xargs -r 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

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

override_dh_link:
	dh_link -ppsychopy /usr/lib/python$(PYVER)/dist-packages/psychopy/demos usr/share/doc/psychopy/examples
	dh_link

override_dh_installman:  debian/psychopy.1
	dh_installman

override_dh_installchangelogs:
	dh_installchangelogs psychopy/CHANGELOG.txt


# 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"

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

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

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

