#!/usr/bin/make -f
# -*- makefile -*-
# debian/rules for package 'dblatex' that uses debhelper.
# The underlying template was originally written by Joey Hess and Craig Small.
# As a special exception, when the template is copied by dh-make into a
# dh-make output file, you may use that output file without restriction.
# This special exception was added by Craig Small in version 0.37 of dh-make.

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

# 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
SHARE_DIR   := $(prefix)/usr/share/dblatex
CONTRIB_DIR := $(SHARE_DIR)/lib/contrib
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

build: build-stamp

build-stamp:
	dh_testdir
        # As the upstream manual page is not necessarily compressed with
        # maximum compression, let compression be done by dh_installman.
	gunzip --to-stdout docs/manpage/dblatex.1.gz > dblatex.1
	cd docs/ \
        && tar --extract --bzip2 --file=../examples/examples-src.tar.bz2
	rm --force docs/examples/COPYRIGHT
	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm --force build-stamp
	rm --force dblatex.1
	rm --force --recursive docs/examples/
	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	mkdir --parents $(BIN_DIR)
	cp --archive lib/contrib/debian/dblatex $(BIN_DIR)

	mkdir --parents $(TEX_DIR)
	cp --archive latex/contrib latex/misc latex/style $(TEX_DIR)
	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

	mkdir --parents $(XSL_DIR)
	cp --archive xsl $(XSL_DIR)

	mkdir --parents $(SHARE_DIR)/lib
	cp --archive lib/dbtexmf $(SHARE_DIR)/lib
	mkdir --parents $(CONTRIB_DIR)/debian
	cp --archive lib/contrib/debian/errorhandler.py $(CONTRIB_DIR)/debian
	touch $(CONTRIB_DIR)/__init__.py $(CONTRIB_DIR)/debian/__init__.py
	mkdir --parents $(SHARE_DIR)/latex
	cp --archive latex/graphics latex/scripts latex/specs \
                     $(SHARE_DIR)/latex
	ln --symbolic ../../texmf/tex/latex/dblatex/contrib \
                      $(SHARE_DIR)/latex/contrib
	ln --symbolic ../xml/docbook/stylesheet/dblatex/xsl $(SHARE_DIR)/xsl

# Build architecture-independent files here.
binary-indep: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs
	dh_installdocs docs/manual.pdf docs/release-notes.pdf \
                       docs/html/ docs/examples/
	dh_installman dblatex.1
	dh_pysupport
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_gencontrol
	dh_md5sums
	dh_builddeb

# Build architecture-dependent files here.
binary-arch: build install
# We have nothing to do by default.

binary: binary-indep binary-arch

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

.PHONY: build clean install binary-indep binary-arch binary get-orig-source
