#!/usr/bin/make -f

export PYBUILD_NAME = pygame

# Replace embedded font with system copy
export PYBUILD_AFTER_INSTALL = ln -sf /usr/share/fonts/truetype/freefont/FreeSansBold.ttf \
                                      {destdir}/{install_dir}/pygame/freesansbold.ttf

# setup.py only installs data during install, not build
export PYBUILD_BEFORE_TEST = cp {dir}/lib/pygame_icon.bmp {dir}/lib/freesansbold.ttf \
                                {build_dir}/pygame

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')

HG_REPO ?= https://bitbucket.org/pygame/pygame
HG_SNAPSHOT_REV=3449

# 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,python3 --buildsystem=pybuild

override_dh_auto_test:
	# buildds don't provide graphical interfaces or audio/video devices
	# to test with, hence some tests won't work properly without:
	SDL_VIDEODRIVER=dummy SDL_AUDIODRIVER=disk LC_ALL=C.UTF-8 \
		dh_auto_test -- --system=custom --test-args \
		'{interpreter} run_tests.py --exclude opengl,display' || true

override_dh_install:
	dh_install
	dh_numpy
	dh_numpy3

override_dh_installdocs:
	dh_installdocs -a $(doclist)

override_dh_installchangelogs:
	dh_installchangelogs -a WHATSNEW

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)/

get-orig-source-hg:
	@echo "You can build this from a local repo by supplying the HG_REPO variable"
	# Requires mercurial
	hg clone -r$(HG_SNAPSHOT_REV) $(HG_REPO)
	# Remove unwanted cruft
	rm -rf pygame/.hg pygame/.hgtags pygame/.hgignore pygame/obj
	# Repack tarball
	tar -cJf pygame_1.9.2~pre~r$(HG_SNAPSHOT_REV).orig.tar.xz pygame
	rm -rf pygame
