#!/usr/bin/make -f
#
# debian/rules file for tfm-arphic for Debian.
# GNU GPL v2, Copyright (C) 1999-2001, Anthony Fok <foka@debian.org>

package = tfm-arphic
SHELL = /bin/sh

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

# This is the debhelper compatibility version to use.
export DH_COMPAT=3

# export DH_OPTIONS doesn't work for this file.  Yes, I tried already.
#export DH_OPTIONS
#

# Arphic PL fonts information

fonts = bsmi bkai gbsn gkai

bsmi_typeface = bsmi00lp
bsmi_familyname = AR PL Mingti2L Big5
bsmi_encoding = UBig5

bkai_typeface = bkai00mp
bkai_familyname = AR PL KaitiM Big5
bkai_encoding = UBig5

gbsn_typeface = gbsn00lp
gbsn_familyname = AR PL SungtiL GB
gbsn_encoding = UGB

gkai_typeface = gkai00mp
gkai_familyname = AR PL KaitiM GB
gkai_encoding = UGB

UBig5_mapfile = cubig5.map
UBig5_planes = 55
UGB_mapfile = cugb.map
UGB_planes = 35

fonttype = truetype
supplier = arphic
mode = modeless

typeface = $($(abbrev)_typeface)
familyname = $($(abbrev)_familyname)
encoding = $($(abbrev)_encoding)
mapfile = $($(encoding)_mapfile)
planes = $($(encoding)_planes)
bin-package = tfm-$(supplier)-$(typeface)
binary_fonts = $(addprefix binary_,$(fonts))
build-stamp_fonts = $(addprefix build-stamp.,$(fonts))
install_fonts = $(addprefix install_,$(fonts))
clean_fonts = $(addprefix clean_,$(fonts))

# Path names

d = debian/$(bin-package)
mapsdir = /usr/share/ttf2pt1/maps
texmf = usr/share/texmf
fontpath = usr/share/fonts/$(fonttype)/$(supplier)
dest_texfontpath = $(texmf)/fonts/$(fonttype)/$(supplier)
dest_tfmpath = $(texmf)/fonts/tfm/$(supplier)/$(typeface)
dest_docpath = usr/share/doc/texmf/fonts/$(fonttype)/$(supplier)
dest_encpath = $(texmf)/dvips/$(supplier)

build: build-stamp
build-stamp: $(build-stamp_fonts)
	dh_testdir
	touch build-stamp

$(build-stamp_fonts): abbrev = $(patsubst build-stamp.%,%,$@)
$(build-stamp_fonts):
	dh_testdir
	ln -sf /$(fontpath)/$(typeface).ttf .
	ttf2tfm $(typeface) -q $(abbrev)@$(encoding)@
	ttf2tfm $(typeface) -q -s 0.167 $(abbrev)s@$(encoding)@
	ttf2tfm $(typeface) -q -x $(abbrev)r@$(encoding)@
	ttf2tfm $(typeface) -q -x -s 0.167 $(abbrev)rs@$(encoding)@

	for i in \
	  `perl -e 'for $$p (01..$(planes)) { printf "%02d ", $$p; }'`; \
	  do \
	    ttf2pt1 -L $(mapsdir)/$(mapfile)+$$i \
		-Oh $(typeface).ttf $(abbrev)$$i; \
	    rm -f $(abbrev)$$i.t1a $(abbrev)$$i.afm; \
	done

	touch $@

clean: pre-clean $(clean_fonts)
	dh_testdir
	dh_testroot
	rm -f build-stamp

	# Add here commands to clean up after the build process.
	rm -f *.ttf *.tfm *.afm *.t1a *.enc

	dh_clean

pre-clean:
	cd debian; cp control.stem.in control

$(clean_fonts): abbrev = $(patsubst clean_%,%,$@)
$(clean_fonts): DH_OPTIONS = -p$(bin-package) -P$(d)
$(clean_fonts):
	cd debian; \
	    sed -e 's/@TYPEFACE@/$(typeface)/g' \
		-e 's/@FAMILYNAME@/$(familyname)/g' \
		control.leaf.in >> control
	cd debian; \
	    sed -e 's/@ABBREV@/$(abbrev)/g' \
		-e 's/@FAMILYNAME@/$(familyname)/g' \
		-e 's/@TYPEFACE@/$(typeface)/g' \
		copyright.in > $(bin-package).copyright
	cd debian; \
	    cp postinst $(bin-package).postinst
	cd debian; \
	    sed -e 's/@MODE@/$(mode)/g' \
		-e 's/@SUPPLIER@/$(supplier)/g' \
		-e 's/@TYPEFACE@/$(typeface)/g' \
		postrm.in > $(bin-package).postrm
	dh_clean $(DH_OPTIONS)
	rm -f build-stamp.$(abbrev)

install: build $(install_fonts)
$(install_fonts): abbrev = $(patsubst install_%,%,$@)
$(install_fonts): DH_OPTIONS = -p$(bin-package) -P$(d)
$(install_fonts):
	dh_testdir $(DH_OPTIONS)
	dh_testroot $(DH_OPTIONS)
	dh_clean -k $(DH_OPTIONS)
	dh_installdirs $(DH_OPTIONS) $(dest_tfmpath) $(dest_docpath) \
		$(dest_texfontpath) $(dest_encpath)
	ln -s ../../../../fonts/$(fonttype)/$(supplier)/$(typeface).ttf \
		$(d)/$(dest_texfontpath)/
	cp -a $(abbrev)*.tfm $(d)/$(dest_tfmpath)/
	cp -a $(abbrev)*.enc $(d)/$(dest_encpath)/

# Build architecture-independent files here.
binary-indep: build install $(binary_fonts)
$(binary_fonts): abbrev = $(patsubst binary_%,%,$@)
$(binary_fonts): DH_OPTIONS = -p$(bin-package) -P$(d)
$(binary_fonts):
	dh_testdir $(DH_OPTIONS)
	dh_testroot $(DH_OPTIONS)
	dh_installdocs $(DH_OPTIONS)
	ln -s ../../../../$(bin-package) $(d)/$(dest_docpath)/$(typeface)
	dh_installchangelogs $(DH_OPTIONS)
	dh_link $(DH_OPTIONS)
	dh_compress $(DH_OPTIONS)
	dh_fixperms $(DH_OPTIONS)
	dh_installdeb $(DH_OPTIONS)
	dh_gencontrol $(DH_OPTIONS)
	dh_md5sums $(DH_OPTIONS)
	dh_builddeb $(DH_OPTIONS)

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

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install \
	$(fonts) $(install_fonts) $(binary_fonts) $(clean_fonts) pre-clean
