#!/usr/bin/make -f
# -*- makefile -*-

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1


PKGDIR = $(dir $(firstword $(MAKEFILE_LIST)))..
UPVER = $(shell cd $(PKGDIR) && dpkg-parsechangelog | sed -rne 's,^Version: ([^-]+).*,\1,p')


%:
	dh $@ 

# Use package libjs-jquery; mangle URLs
override_dh_install:
	dh_install -Xjquery.js

	# Point to local Python3 docs instead of official at docs.python.org
	sed -i -e \
		's|http://docs\.python\.org/3\..|file:///usr/share/doc/python3/html|' \
		debian/diveintopython3/usr/share/doc/diveintopython3/html/*.html

	# Make download URLs absolute
	sed -i -e \
		's|href=d/|href=http://www.diveintopython3.org/d/|g' \
		debian/diveintopython3/usr/share/doc/diveintopython3/html/index.html

	# Remove link to cover image missing from ZIP file
	sed -i -e \
		's|<br><img src="i/cover.jpg"[^<]*||' \
		debian/diveintopython3/usr/share/doc/diveintopython3/html/index.html


# Fix script perms
override_dh_installexamples:
	dh_installexamples
	chmod 644 debian/diveintopython3/usr/share/doc/diveintopython3/examples/*


# Don't compress anything in examples, and exclude some javascript files
override_dh_compress:
	dh_compress -Xexamples -X.js


get-orig-source:
	which unzip >/dev/null || { echo "Package 'unzip' required"; exit 1; }
	
	uscan --noconf --force-download --download-current-version \
		--check-dirname-level=0 --destdir=$(CURDIR) $(PKGDIR)
	
	unzip diveintopython3-html-r*.zip
	rm  -f diveintopython3-html-r*.zip
	
	mv diveintopython3-r*-*-*-* diveintopython3-$(UPVER).orig
	find diveintopython3-$(UPVER).orig -type f -print0 | xargs -0 chmod 644
	GZIP="-9" tar --owner=root --group=root --mode=a+rX \
			-czf diveintopython3_$(UPVER).orig.tar.gz \
			diveintopython3-$(UPVER).orig
	
	rm -rf diveintopython3-$(UPVER).orig 
