#! /usr/bin/make -f

PKG                   := tex-gyre
PKGFONTS              := fonts-texgyre
PKG_INSTALL_DIR       := debian/$(PKG)
PKGFONTS_INSTALL_DIR  := debian/$(PKGFONTS)

FAMILIES              := qag qbk qzc qcr qcs qhv qpl qtm
FAMILYNAMES           := Adventor Bonum Chorus Cursor Heros Pagella \
                         Schola Termes

INSTALL               := install
INSTALL_FILE          := $(INSTALL) -p -o root -g root -m 644


SHELL                 := /bin/bash

^build: build-arch build-indep
# We have nothing to do here but the Debian Policy says this target must
# exist.

build-arch:
# Nothing to do here

build-indep: build-indep-stamp

build-indep-stamp:
	dh_testdir fonts/type1/public/tex-gyre/qtmr.pfb tex/latex/tex-gyre/tgtermes.sty
	# Warn the person building the package if new files were added
	# to the .orig.tar.gz + .diff.gz that this script is not aware of.
	@newfiles=$$(find . -type f | grep -Ev \
		-e '^\./.git/' \
		-e '^\./.pc/' \
		-e '^\./debian/' \
		-e '^\./tlpkg/' \
		-e '^\./fonts/type1/public/tex-gyre/[^/]*\.pf[bm]$$' \
		-e '^\./fonts/afm/public/tex-gyre/[^/]*\.afm$$' \
		-e '^\./fonts/tfm/public/tex-gyre/[^/]*\.tfm$$' \
		-e '^\./fonts/map/dvips/tex-gyre/[^/]*\.map$$' \
		-e '^\./fonts/enc/dvips/tex-gyre/[^/]*\.enc$$' \
		-e '^\./fonts/opentype/public/tex-gyre/[^/]*\.otf$$' \
		-e '^\./fonts/opentype/public/tex-gyre-math/[^/]*\.otf$$' \
		-e '^\./tex/latex/tex-gyre/[^/]*\.(fd|sty)$$' \
		-e '^\./source/fonts/tex-gyre-math/texgyredejavu-math\.sfd$$' \
		-e '^\./doc/fonts/tex-gyre-math/[^/]*\.(tex|pdf|txt|fea|docx)$$' \
		-e '^\./doc/fonts/tex-gyre-math/README$$' \
		-e '^\./source/fonts/tex-gyre/[^/]*\.(fea|sfd)$$' \
		-e '^\./doc/fonts/tex-gyre/[^/]*\.(tex|pdf|txt|fea|nam)$$' || true) && \
	if [ -n "$$newfiles" ]; then \
		{ echo "New files were found in the patched tarball" \
			"(.orig.tar.gz + .diff.gz):"; \
			echo "$$newfiles" | sed 's/^\(.*\)$$/  \1/'; \
			echo; \
			echo "Please update the debian/rules file accordingly."; \
		} >&2; \
		exit 1; \
	fi

	# Upstream provides <texmf>/fonts/map/dvips/tex-gyre/q??.map that 
	# should be equivalent to all the encoding-specific map files in 
	# <texmf>/fonts/map/dvips/tex-gyre/ 
	# Let's check if this is really the case.
	@export LC_COLLATE=C && for fam in $(FAMILIES) ; do \
	   a=$$(find fonts/map/dvips/tex-gyre -name "$$fam-*.map" \
              -print0 | xargs -0r cat | grep -Ev -e '^(%|[[:space:]]*$$)' \
                      | sort) \
	&& b=$$(sort < fonts/map/dvips/tex-gyre/$$fam.map | grep -Ev -e \
                                                        '^(%|[[:space:]]*$$)') \
	&& if [ "$$a" != "$$b" ]; then \
             { echo; \
               echo "Warning: fonts/map/dvips/tex-gyre/$$fam.map does not appear to" \
                    "be equivalent to"; \
               echo "the concatenation of the other family's map files found in" \
                    "fonts/map/dvips/tex-gyre/."; \
               echo; \
             } >&2; \
           fi ; \
	done

	# Generate $(PKG).scale from $(PKG).defoma-hints (since it contains
	# nice hand-crafted XLFD names for the fonts).
	sed -nf debian/sed_scripts/gen-fonts.scale \
	  "debian/$(PKG).defoma-hints" > "debian/$(PKG).scale.tmp"
	{ wc -l < "debian/$(PKG).scale.tmp" && \
	  cat "debian/$(PKG).scale.tmp"; \
	} > "debian/$(PKG).scale"

	# The following generated list should be safe with respect to font
	# file names containing spaces (there is one name per line)...
	sed -ne 's/^[[:blank:]]*begin[[:blank:]]\{1,\}.*[/]\([^/]\{1,\}\)\.pfb[[:blank:]]*$$/\1/p' \
	  < "debian/$(PKG).defoma-hints" > "debian/$(PKG).fontlist-x11"
	
	touch build-indep-stamp

clean:
	dh_testdir fonts/type1/public/tex-gyre/qtmr.pfb tex/latex/tex-gyre/tgtermes.sty
	dh_testroot
	# debian/$(PKG).links is automatically generated from the list of
	# fonts to declare to X11 among other things, so we have to delete
	# it here.
	rm -f build-indep-stamp \
		"debian/$(PKG).scale.tmp" "debian/$(PKG).scale" \
		"debian/$(PKG).fontlist-x11" "debian/$(PKG).links"
	dh_clean

binary-indep: build-indep
	dh_testdir fonts/type1/public/tex-gyre/qtmr.pfb tex/latex/tex-gyre/tgtermes.sty
	dh_testroot
	dh_clean

	# We will call dh_link only once for the build, with all desired links
	# specified in $(PKG).links because it is much faster than spawning
	# a dh_link (actually, Perl) process for every symbolic link in this
	# package. So, $(PKG).links starts as an empty file and (target, link)
	# pairs will be added to it in the relevant places.
	: > "debian/$(PKG).links"


	# Setup symlinks so that X can see the AFM and PFB files as well.
	sed -nf debian/sed_scripts/gen-x-fonts-links-list \
	  "debian/$(PKG).fontlist-x11" >> "debian/$(PKG).links"
	
	dh_install
	dh_installxfonts
	dh_installtex --package=$(PKG) mapfile=debian/tex-gyre.cfg

	$(foreach PPP, $(PKG) $(PKGFONTS), \
	  dh_link -p $(PPP) \
            $(foreach FAM, $(FAMILYNAMES), \
              usr/share/texmf/doc/fonts/tex-gyre/README-TeX-Gyre-$(FAM).txt \
	      usr/share/doc/$(PPP)/README-TeX-Gyre-$(FAM).txt) \
            $(foreach FAM, $(FAMILIES), \
	      usr/share/texmf/doc/fonts/tex-gyre/$(FAM)-info.pdf \
	      usr/share/doc/$(PPP)/$(FAM)-info.pdf) \
            $(foreach FAM, $(FAMILIES), \
	      usr/share/texmf/doc/fonts/tex-gyre/$(FAM)-hist.txt \
	      usr/share/doc/$(PPP)/$(FAM)-hist.txt) \
	    usr/share/texmf/doc/fonts/tex-gyre-math/README-TeX-Gyre-Bonum-Math.txt \
	    usr/share/doc/$(PPP)/README-TeX-Gyre-Bonum-Math.txt \
	    usr/share/texmf/doc/fonts/tex-gyre-math/README-TeX-Gyre-Pagella-Math.txt \
	    usr/share/doc/$(PPP)/README-TeX-Gyre-Pagella-Math.txt \
	    usr/share/texmf/doc/fonts/tex-gyre-math/README-TeX-Gyre-Schola-Math.txt \
	    usr/share/doc/$(PPP)/README-TeX-Gyre-Schola-Math.txt \
	    usr/share/texmf/doc/fonts/tex-gyre-math/README-TeX-Gyre-Termes-Math.txt \
	    usr/share/doc/$(PPP)/README-TeX-Gyre-Termes-Math.txt \
	    usr/share/texmf/doc/fonts/tex-gyre-math/README-TeX-Gyre-DejaVu-Math.txt \
	    usr/share/doc/$(PPP)/README-TeX-Gyre-DejaVu-Math.txt \
	      ; )
	dh_link -p $(PKGFONTS) \
	    etc/fonts/conf.avail/65-$(PKGFONTS).conf \
	    etc/fonts/conf.d/65-$(PKGFONTS).conf

	dh_installdocs debian/VERSIONS
	dh_installchangelogs
	dh_lintian
	dh_compress -X.pdf
	dh_fixperms
	dh_installdeb
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary-arch:
# We have nothing to do here but the Debian Policy says this target must
# exist.

binary: binary-indep binary-arch

.PHONY: clean build build-arch build-indep binary binary-indep binary-arch
