#!/usr/bin/make -f

srcpkg = $(shell LC_ALL=C dpkg-parsechangelog | grep '^Source:' | cut -d ' ' -f 2,2)
upstreamver = $(shell LC_ALL=C dpkg-parsechangelog | grep '^Version:' | cut -d ' ' -f 2,2 | cut -d '-' -f 1,1)
upstreamtarball = tutorial.zip
upstreamurl = http://www.cabiatl.com/Resources/Course/$(upstreamtarball)

# one ring to rule them all ...
%:
	dh $@

override_dh_compress:
	# this is all about documentation -- we want it readily usable, since this
	# is the sole purpose of this package
	dh_compress -X.ppt -X.txt -X.fsf -X.mat -Xfeat -X.par -X.trg

#
# everything below are maintainer targets for source package handling/processing
#
$(upstreamtarball):
	[ -f ../$@ ] || curl -o ../$@ $(upstreamurl)

maint-clean:
	find . -maxdepth 1 -type f ! -wholename './.git*' -delete
	rm -rf html

maint-update-md5sum:
	find . -type f ! -wholename './debian*' ! -wholename './.git*' -print0 |\
		xargs -0 md5sum > debian/origdata.md5sum

maint-check-md5sum:
	# if the check fails it means upstream changed something in the
	# zip file -- in that case check the package, figure out what changed,
	# update the version and use it as a new upstream version (simply update
	# Debian changelog)
	md5sum -c debian/origdata.md5sum

maint-populate-srcpkg:
	tar --wildcards --strip 1 -xvjf ../$(srcpkg)_$(upstreamver).orig.tar.bz2

get-orig-source: $(upstreamtarball)
	# extract orig archiv and strip trash
	unzip ../tutorial.zip -x '*Thumbs.db' '*.DS_Store' '*._*' -d .
	# also extract embedded archive
	unzip peri.zip -d .
	# delete embedded archive itself -- redundant
	rm peri.zip
	# make orig tarball with version that reflects the last modified date of
	# any file in the sources
	PKGORIGVERSION="0.$$(LC_ALL=C find . -type f ! -wholename './debian*' ! -wholename './.git*' \
                                      -exec ls -og --time-style=long-iso {} \; \
                      |sort --key 4 |tail -n1 | cut -d ' ' -f 4,4 | tr -d '-')~dfsg.1"; \
        tar --exclude='.git*' --exclude=debian --transform "s,^./,$(srcpkg)-$${PKGORIGVERSION}/,"\
            -cjf ../$(srcpkg)_$${PKGORIGVERSION}.orig.tar.bz2 .
	# check integrity to notify a version change
	md5sum -c debian/origdata.md5sum
