#!/usr/bin/make -f

export DH_VERBOSE=1

%:
	#dh $@ --with python2,python3
	dh $@ --with python2

override_dh_auto_install: 
	set -e && for pyvers in $(shell pyversions -vr); do \
		python$$pyvers setup.py install --install-layout=deb \
			--root $(CURDIR)/debian/python-pattern; \
	done

	#set -e && for py3vers in $(shell py3versions -sv); do \
	#	python$$py3vers setup.py install --install-layout=deb \
	#		--root $(CURDIR)/debian/python3-pattern; \
	#done

	find debian/python*-pattern/ -type f -name "LICENSE.txt" -exec rm -fv {} \;
	find debian/python*-pattern/ -type f -name "LICENSE" -exec rm -fv {} \;

override_dh_install:
	dh_install

	find "debian/python-pattern/usr/share/doc/python-pattern/test" -type f -name "*.py" | \
		while read -r F; do \
			echo "Fixing $$F"; \
			sed -i.bak 's/import os, sys; sys.path.insert(.*)/import os, sys/g' "$$F"; \
		done
	find "debian/python-pattern/usr/share/doc/python-pattern/test" -type f -name "*.py.bak" -delete


override_dh_installexamples:
	dh_installexamples

	find "debian/python-pattern/usr/share/doc/python-pattern/examples" -type f -name "*.py" | \
		while read -r F; do \
			echo "Fixing $$F"; \
			sed -i.bak 's/import os, sys; sys.path.insert(.*)/import os, sys/g' "$$F"; \
		done
	find "debian/python-pattern/usr/share/doc/python-pattern/examples" -type f -name "*.py.bak" -delete


override_dh_auto_clean:
	rm -rfv pattern/web/feed/
	rm -rfv pattern/web/json/
	rm -rfv pattern/web/soup/
	rm -rfv pattern/vector/svm/liblinear-*/
	rm -fv  pattern/vector/svm/liblinear*.py
	rm -rfv pattern/vector/svm/libsvm-*/
	rm -fv  pattern/vector/svm/libsvm.py

	find pattern/text/en/wordnet/dict/ ! -name 'index.32' -type f -exec rm -fv {} +

	set -e && for pyvers in $(shell pyversions -vr); do \
		python$$pyvers setup.py clean -a; \
	done

	#set -e && for py3vers in $(shell py3versions -sv); do \
	#	python$$py3vers setup.py clean -a; \
	#done

	find . -name "*.pyc" -delete
	rm -rfv build Pattern.egg-info
	dh_clean


override_dh_compress:
	dh_compress \
		--exclude=.py \
		--exclude=.txt \
		--exclude=.csv \
		--exclude=.docx \
		--exclude=.pdf \
		--exclude=.db \
		--exclude=.html \
		--exclude=.js \
		--exclude=.gif \
		--exclude=.jpg \
		--exclude=.png


run-tests:
	dh_testdir
	# Start tests
	LC_ALL=C LANG=C python test/test_metrics.py
	# pattern.web tests require a working internet connection
	#LC_ALL=C LANG=C python test/test_web.py
	# pattern.db tests require a valid username and password for MySQL
	#LC_ALL=C LANG=C python test/test_db.py
	LC_ALL=C LANG=C python test/test_de.py
	LC_ALL=C LANG=C python test/test_en.py
	LC_ALL=C LANG=C python test/test_es.py
	LC_ALL=C LANG=C python test/test_fr.py
	LC_ALL=C LANG=C python test/test_it.py 
	LC_ALL=C LANG=C python test/test_nl.py
	LC_ALL=C LANG=C python test/test_text.py
	LC_ALL=C LANG=C python test/test_search.py
	LC_ALL=C LANG=C python test/test_vector.py
	LC_ALL=C LANG=C python test/test_graph.py
	# Clean everything
	find . -name "*.pyc" -delete


get-orig-source: clean
	dh_testdir
	dh_testroot
	# Create temporary directory (removing it if it previously existed)
	rm -rf debian/tmp
	mkdir -p debian/tmp
	# Download software
	wget \
		--output-document="debian/tmp/pattern-2.6+git820cccf.zip" \
		https://codeload.github.com/clips/pattern/zip/820cccf33c6ac4a4f1564a273137171cfa6ab7cb
	cd debian/tmp && unzip pattern-*.zip && rm -fv pattern-*.zip
	# Remove unwanted pre-compiled files
	rm -rfv debian/tmp/pattern-*/pattern/vector/svm/liblinear-*/
	rm -rfv debian/tmp/pattern-*/pattern/vector/svm/libsvm-*/
	# Fix permissions
	find debian/tmp/pattern-*/. -type f -name "*.txt"    -exec chmod 0644 {} \;
	find debian/tmp/pattern-*/. -type f -name "*.rst"    -exec chmod 0644 {} \;
	find debian/tmp/pattern-*/. -type f -name "PKG-INFO" -exec chmod 0644 {} \;
	find debian/tmp/pattern-*/. -type f -name "*.py"     -exec chmod 0644 {} \;
	find debian/tmp/pattern-*/. -type f -name "*.pyc"    -exec rm -fv {} \;
	chmod +x debian/tmp/pattern-*/pattern/server/cherrypy/cherrypy/cherryd
	# Replace shCore.js with the original source
	patch debian/tmp/pattern-*/docs/js/shCore.js < debian/patches/replace-shCore-source.patch
	# Create tarball
	cd debian/tmp && tar cvfJ python-pattern_2.6+git20150109.orig.tar.xz pattern-*/
	mv debian/tmp/python-pattern_*.orig.tar.xz ..
	rm -rf debian/tmp
