#!/usr/bin/make -f

export PYBUILD_NAME=django

PREFIX = debian/python-django
PREFIX3 = debian/python3-django
PREFIXC = debian/python-django-common

MANAGE_PY_TPL = django/conf/project_template/manage.py-tpl

%:
	dh $@ --with sphinxdoc,python2,python3 --buildsystem=pybuild

override_dh_auto_clean:
	dh_auto_clean
	make -C docs clean

override_dh_auto_build:
	dh_auto_build
	make -C docs html

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	set -e; cd tests && for python in $$(pyversions -s) $$(py3versions -s); do \
	    	echo "——— Running tests with $$python ———"; \
		LC_ALL=C.UTF-8 PYTHONPATH=.. $$python ./runtests.py --verbosity=2; \
	done
endif

override_dh_clean:
	# backup~ is used in tests
	dh_clean -Xbackup~

override_dh_auto_install:
	dh_auto_install

	# Drop extra license files
	find $(PREFIX) -name LICENSE -o -name license.txt -o -name license.python.txt \
	    -o -name LICENSE.txt | xargs --no-run-if-empty rm
	find $(PREFIX3) -name LICENSE -o -name license.txt -o -name license.python.txt \
	    -o -name LICENSE.txt | xargs --no-run-if-empty rm

	# Drop django-admin{,.py} which is in python-django-common and
	# clean up empty parent directories
	rm $(PREFIX)/usr/bin/django-admin*
	rm $(PREFIX3)/usr/bin/django-admin*
	rmdir --ignore-fail-on-non-empty --parents $(PREFIX)/usr/bin $(PREFIX3)/usr/bin

	# Fix the shebang lines to use python3 in python3-django
	find $(PREFIX3) -name django-admin.py -or -name manage.py | \
	    xargs --no-run-if-empty sed -i -e '1s/python$$/python3/'

	# Remove embedded Javascript libraries
	set -e; for FILENAME in jquery.js jquery.min.js; do \
		find $(PREFIX) -name $$FILENAME -exec \
			ln -sf /usr/share/javascript/jquery/$$FILENAME {} \;; \
		find $(PREFIX3) -name $$FILENAME -exec \
			ln -sf /usr/share/javascript/jquery/$$FILENAME {} \;; \
	done

override_dh_install:
	dh_install
	
	# Fix permissions
	chmod 644 $(PREFIXC)/usr/share/bash-completion/completions/django_bash_completion
	chmod 755 $(PREFIXC)/usr/bin/django-admin

override_dh_python3:
	dh_python3
	find debian/python3-django/usr/lib/python3/dist-packages/ -type f -not -name '*.py' | \
	(while read file; do \
		relname=$${file##debian/python3-django/usr/lib/python3/dist-packages/}; \
		reldirname=$$(dirname $$relname); \
		mkdir -p debian/python-django-common/usr/share/python-django-common/$$reldirname; \
		mv $$file debian/python-django-common/usr/share/python-django-common/$$reldirname/; \
		ln -sf /usr/share/python-django-common/$$relname $$file; \
		if test -e debian/python-django/usr/share/pyshared/$$relname; then \
		    rm debian/python-django/usr/share/pyshared/$$relname; \
		    ln -sf /usr/share/python-django-common/$$relname debian/python-django/usr/share/pyshared/$$relname; \
		else \
		    rm debian/python-django/usr/lib/python2.7/dist-packages/$$relname; \
		    ln -sf /usr/share/python-django-common/$$relname debian/python-django/usr/lib/python2.7/dist-packages/$$relname; \
		fi \
	done)
	
	# Ensure that "django-admin startproject foo" using python3-django
	# emits the corrent shebang (#833275)
	rm $(PREFIX3)/usr/lib/python3/dist-packages/$(MANAGE_PY_TPL)
	sed -e '1s/python$$/python3/' $(PREFIXC)/usr/share/python-django-common/$(MANAGE_PY_TPL) > \
		$(PREFIX3)/usr/lib/python3/dist-packages/$(MANAGE_PY_TPL)

override_dh_compress:
	dh_compress -X.js -Xobjects.inv
