#!/usr/bin/make -f

DEB_HOST_MULTIARCH = $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
export ORIGLIBDIRS := /lib:/lib/$(DEB_HOST_MULTIARCH)

export DEB_HOST_ARCH_OS := $(shell dpkg-architecture -qDEB_HOST_ARCH_OS)

DEB_UPSTREAM_VERSION=$(shell dpkg-parsechangelog | \
                       sed -rne 's,^Version: ([^+]+).*,\1,p')

PYVERS=$(shell pyversions -r debian/control)

# We include all the documentation except:
# util (which is for building the docs from the source)
# fullinstall.txt (which is installation instructions)
# LGPL (which is in /usr/share/common-licenses/LGPL-2.1 under Debian)
doclist = $(filter-out docs/util docs/fullinstall.txt docs/LGPL,$(wildcard docs/*))

%:
	dh $@ --with python2

override_dh_clean:
	rm -f *.pyc Setup
	rm -rf build

	dh_clean

override_dh_installdocs:
	dh_installdocs -a $(doclist)

override_dh_installchangelogs:
	dh_installchangelogs -a WHATSNEW

override_dh_link:
	# Replace embedded font with system copy
	ln -sf /usr/share/fonts/truetype/freefont/FreeSansBold.ttf \
	$(CURDIR)/debian/python-pygame/usr/share/pyshared/pygame/freesansbold.ttf
	for python in $(PYVERS); \
		do ln -sf /usr/share/fonts/truetype/freefont/FreeSansBold.ttf \
			$(CURDIR)/debian/python-pygame/usr/lib/$$python/dist-packages/pygame/freesansbold.ttf; \
	done
	
	dh_link -a

get-orig-source:
	# Remove existing pygame directory and tarball, if existing
	rm -rf pygame-$(DEB_UPSTREAM_VERSION)
	rm -f pygame_$(DEB_UPSTREAM_VERSION)+dfsg.orig.tar.gz
	# Fetch latest upstream tarball
	uscan --noconf --force-download --download-current-version \
	--destdir=.
	# Unpack and remove tarball
	tar -xzf pygame-$(DEB_UPSTREAM_VERSION).tar.gz
	rm -f pygame-$(DEB_UPSTREAM_VERSION).tar.gz
	# Remove embedded freesansbold.ttf copy; it comes without the FontForge
	# source file (.sfd), so it's technically a GPL violation
	rm -f pygame-$(DEB_UPSTREAM_VERSION)/lib/freesansbold.ttf
	# Repack tarball
	tar -czf pygame_$(DEB_UPSTREAM_VERSION)+dfsg.orig.tar.gz pygame-\
	$(DEB_UPSTREAM_VERSION)/
