#!/usr/bin/make -f

# Use the debhelper command sequencer.
%:
	dh $@ --with python2

############################# Variable definitions #############################

# Some targets (e.g. get-orig-source) are not required to be started from the
# package's top level directory.
# Nevertheless they need to know the package's version.
# The version gets determined at runtime by dpkg-parsechangelog using the
# package's changefile.
# The changefile is located in this file's directory.
# Thus this file's directory needs to be determined, which will only work
# reliably when the make variable MAKEFILE_LIST contains exactly one value.
ifneq ($(words $(MAKEFILE_LIST)),1)
    $(error Unable to determine location of rules)
endif

debiandir := $(realpath $(dir $(strip $(MAKEFILE_LIST))))

PKG_VERS     := $(shell dpkg-parsechangelog -l$(debiandir)/changelog \
                | grep ^Version: | cut -d' ' -f2 | cut -d- -f1)
PKG_UPSTREAM := dblatex-$(PKG_VERS).tar.bz2
PKG_DEBIAN   := dblatex_$(PKG_VERS).orig.tar.bz2
DOWNLOAD_URL := http://prdownloads.sourceforge.net/dblatex

################################# dh overrides #################################

override_dh_clean:
	rm --force --recursive docs/examples/
	rm --force --recursive dblatex.egg-info/
	dh_clean

override_dh_auto_build:
        # - Nothing to be compiled
        # - Don't call build_doc, as it's superfluous and will run into
        #   bootstrap problems.

override_dh_installchangelogs:
	dh_installchangelogs docs/changes/changes.txt

override_dh_installdocs:
        # Prepare installation of dblatex examples.
	tar --directory=docs --extract --bzip2 \
            --file=examples/examples-src.tar.bz2
	rm --force docs/examples/COPYRIGHT
        # Finally install all documentation.
	dh_installdocs docs/manual.pdf docs/release-notes.pdf \
                       docs/xhtml/ docs/examples/
        # Don't follow upstream directory name change from docs/html/ to
        # docs/xhtml/, but keep well known name /usr/share/doc/dblatex/html/,
        # that is don't break existing bookmarks.
	mv debian/dblatex/usr/share/doc/dblatex/xhtml debian/dblatex/usr/share/doc/dblatex/html

############################## Additional targets ##############################

get-orig-source:
	wget $(DOWNLOAD_URL)/$(PKG_UPSTREAM)?download \
             --output-document=$(PKG_DEBIAN)
	wget $(DOWNLOAD_URL)/dblatex-examples.tar.bz2 \
             --output-document=dblatex_$(PKG_VERS).orig-examples.tar.bz2

.PHONY: get-orig-source
