#!/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))))
prefix    := $(debiandir)/dblatex

BIN_DIR     := $(prefix)/usr/bin
DBLATEX_DIR := $(prefix)/usr/share/dblatex
PYTHON_DIR  := $(prefix)/usr/lib/$(shell pyversions --default)/dist-packages
TEX_DIR     := $(prefix)/usr/share/texmf/tex/latex/dblatex
XSL_DIR     := $(prefix)/usr/share/xml/docbook/stylesheet/dblatex

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/
	if [ -d docs/html ]; then mv docs/html docs/xhtml; fi
	dh_clean

override_dh_auto_build:
        # Nothing to be compiled

override_dh_auto_install:
        # Call upstream setup.py, but omit dependency checking as it only
        # applies to the install host, but not to the build host.
	dh_auto_install -- --nodeps

        # As upstream installation doesn't match Debian packaging requirements,
        # its results needs to be tweaked:

        # Remove documentation installed by upstream as dh_installdocs will do
        # a better job.
	rm --force --recursive $(prefix)/usr/share/doc/dblatex/

        # Add Debian specific main script as it doesn't get installed by
        # upstream.
	mkdir --parents $(BIN_DIR)
	cp --archive lib/contrib/debian/dblatex $(BIN_DIR)/

        # Add Debian error handler as it doesn't get installed by upstream.
	mkdir --parents $(PYTHON_DIR)/dbtexmf/contrib/debian
	cp --archive lib/contrib/debian/errorhandler.py \
                     $(PYTHON_DIR)/dbtexmf/contrib/debian/
	touch $(PYTHON_DIR)/dbtexmf/contrib/__init__.py \
              $(PYTHON_DIR)/dbtexmf/contrib/debian/__init__.py

        # Move XSLT files from upstream to Debian specific location.
	mkdir --parents $(XSL_DIR)
	mv $(DBLATEX_DIR)/xsl $(XSL_DIR)/
	ln --symbolic ../xml/docbook/stylesheet/dblatex/xsl $(DBLATEX_DIR)/xsl

        # Move TeX files from upstream to Debian specific location.
	mkdir --parents $(TEX_DIR)
	mv $(DBLATEX_DIR)/latex/contrib $(TEX_DIR)/
	mv $(DBLATEX_DIR)/latex/misc    $(TEX_DIR)/
	mv $(DBLATEX_DIR)/latex/style   $(TEX_DIR)/
	ln --symbolic ../../texmf/tex/latex/dblatex/contrib \
                      $(DBLATEX_DIR)/latex/contrib

        # Remove upstream files that are already included in other Debian
        # packages.
	rm $(TEX_DIR)/misc/passivetex/LICENSE \
           $(TEX_DIR)/misc/attachfile.sty \
           $(TEX_DIR)/misc/bibtopic.sty \
           $(TEX_DIR)/misc/enumitem.sty \
           $(TEX_DIR)/misc/lastpage.sty \
           $(TEX_DIR)/misc/ragged2e.sty

override_dh_installchangelogs:
	dh_installchangelogs docs/changes/changes.txt

override_dh_installdocs:
        # 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.
	if [ -d docs/xhtml ]; then mv docs/xhtml docs/html; fi
        # 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/html/ docs/examples/

############################## 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
