#!/usr/bin/make -f
# -*- mode: makefile; coding: utf-8 -*-
# Made with the aid of debhelper by Joey Hess,
# based on the sample debian/rules file for GNU hello by Ian Jackson.
#
# This is free software; see the GNU General Public Licence
# version 2 or later for copying conditions.  There is NO warranty.
#
# Currently maintained by Danai SAE-HAN (韓達耐) <danai@debian.org>
# for Debian GNU/Linux.

SHELL = /bin/sh

include /usr/share/quilt/quilt.make

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

# When you use the official CVS, SVN, GIT source tree. =CVS:.svn:.git
export DH_ALWAYS_EXCLUDE=.git:CVS:.svn

# The build directory for Thai fonts.
build_thaifonts = build_thaifonts

# Stuff for get-orig-source.
# Getting the revision numbers according to debian/changelog, the HARD way.
upstream_version := $(shell dpkg-parsechangelog | sed -ne 's/^Version: \(.*\)-.*/\1/p')
isodate := $(shell dpkg-parsechangelog | sed -ne 's/^Version.*git\(.*\)-.*/\1/p')
unixdate := $(shell date -d $(isodate) +%s)
# Add 24 hours to the Unix timestamp.
unixdate := $(shell expr $(unixdate) + 86400 )

# To get the Master revision number (i.e. 4.8.0), use:
# dpkg-parsechangelog | sed -ne 's/^Version: *\([^+-]*\).*/\1/p'


build: $(QUILT_STAMPFN) build-stamp
build-arch:
	dh_testdir

	# Update config.{guess,sub}
	dh_autotools-dev_updateconfig
	# Add here commands to compile the package.
	$(MAKE)

	touch build-arch-stamp

build-indep:
	dh_testdir

	mkdir $(build_thaifonts)

# The upstream patch on 2008-12-28 in CJK provided support for
# the ThaiLaTeX fonts that shipped with v0.4.2.
# Before this patch CJK couldn't directly use the same fonts as
# ThaiLaTeX.  latex-cjk-thai therefore installed its own extra fonts.
# If you still wish to use these modified fonts, then don't forget to
# uncomment the last two lines in debian/latex-cjk-thai.install ,
# to comment/uncomment some of the lines below and to remove the
# dependency on thailatex.

#	# Extracting binary files
# How it was used: uuencode thaifonts.tar.gz thaifonts.tar.gz > thaifonts.tar.gz.uu
# You will need a Build-Dependency on the `sharutils' package in debian/control.
#	cd debian && uudecode thaifonts.tar.gz.uu && tar xzf thaifonts.tar.gz
#	(cd $(build_thaifonts) && cp ../utils/thaifont/tools/c90.* ../utils/thaifont/tools/*.fontinst ../debian/thaifonts/* . && cd ../debian/thaifonts && prename 's/_//' * && prename 's/\./n\./' norasi.* garuda.*)
	(cd $(build_thaifonts) && cp ../utils/thaifont/tools/c90.* ../utils/thaifont/tools/*.fontinst /usr/share/texlive/texmf-dist/fonts/afm/public/fonts-tlwg/garuda*.afm /usr/share/texlive/texmf-dist/fonts/afm/public/fonts-tlwg/norasi*.afm .)

	(cd $(build_thaifonts) && tex norasi-c90.fontinst && tex garuda-c90.fontinst && for i in f*.pl; do pltotf $$i; done)

	touch build-indep-stamp

build-stamp:
	touch build-stamp

clean: unpatch
#	dh_testdir doc/CJKutf8.txt examples/CJKbabel.tex
	dh_testroot
	rm -f build-stamp build-indep-stamp build-arch-stamp

	# Add here commands to clean up after the build process.

# If a Debian patch has been applied on Makefile, perform a distclean;
# if not, do nothing.  Reason: CJK upstream has no cleaning targets.
	if [ $(shell grep -c distclean Makefile) -gt 0 ]; then $(MAKE) distclean; fi

	# Remove the binary files.
	rm -rf $(build_thaifonts)

# Remove the .c file created by ctangle.
# This dirty hack is due to the introduction of the Lintian warning
# "patch-system-but-direct-changes-in-diff".  For more information,
# check out Debian bug #471263 at
# http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=471263 .
# It would otherwise also create problems for Debian Source 3.0 (quilt).
	rm -f utils/hbf2gf/config.log
	rm -f utils/hbf2gf/config.status
	rm -f utils/hbf2gf/hbf.o
	rm -f utils/hbf2gf/hbf2gf
	rm -f utils/hbf2gf/hbf2gf.c
	rm -f utils/hbf2gf/hbf2gf.o
	rm -f utils/hbf2gf/Makefile
	rm -f utils/Bg5conv/bg5conv
	rm -f utils/CEFconv/cefconv
	rm -f utils/CEFconv/cef5conv
	rm -f utils/CEFconv/cefsconv
	rm -f utils/SJISconv/sjisconv
	rm -f utils/extconv/extconv

	# Clean up config.{guess,sub}
	dh_autotools-dev_restoreconfig

	dh_clean

install:
	dh_testdir
	dh_testroot
#	dh_installdirs -platex-cjk-chinese usr/share/texmf/tex/latex/CJK/CEF

	# Add here commands to install the package into debian/tmp.
	$(MAKE) prefix=$(CURDIR)/debian/tmp/usr install

# Build architecture-independent files here.
binary-indep: build-indep
	dh_testdir -i
	dh_testroot -i

# Install files in the respective subpackages, according
# to the [package].install files.
	dh_install -i
	dh_link -platex-cjk-korean usr/share/doc/latex-cjk-common usr/share/doc/latex-cjk-korean
	dh_link -platex-cjk-all usr/share/doc/latex-cjk-common usr/share/doc/latex-cjk-all
	dh_link -pcjk-latex usr/share/doc/latex-cjk-common usr/share/doc/cjk-latex
	dh_link -platex-cjk-thai usr/share/doc/latex-cjk-common usr/share/doc/latex-cjk-thai
	dh_installtex -platex-cjk-thai --flavor=map:config_for_all_maps map=Map,garuda-c90.map map=Map,norasi-c90.map
	dh_installtex -platex-cjk-korean
	dh_installemacsen -i
	dh_compress -i
	dh_fixperms -i
	dh_shlibdeps -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# Build architecture-dependent files here.
binary-arch: build build-arch install
	dh_testdir -a
	dh_testroot -a

# Install files in the respective subpackages, according
# to the [package].install files.
	dh_install -a

	chmod +x debian/latex-cjk-common/usr/share/latex-cjk-common/utils/subfonts/*.pl

	dh_link -platex-cjk-chinese usr/share/doc/latex-cjk-common usr/share/doc/latex-cjk-chinese
	dh_link -platex-cjk-japanese usr/share/doc/latex-cjk-common usr/share/doc/latex-cjk-japanese

	# Get the documentation in texdoc.  texdoctk unfortunately uses another engine.
	dh_link -platex-cjk-common usr/share/doc/latex-cjk-common usr/share/texmf/doc/latex/latex-cjk

	dh_installtex -platex-cjk-common -platex-cjk-japanese
	dh_installtex -platex-cjk-chinese

	dh_installdocs -platex-cjk-common
	dh_installexamples -platex-cjk-common
	dh_installman -a
	dh_installchangelogs -platex-cjk-common ChangeLog
	dh_installemacsen -a
	dh_strip --exclude=.pl -a
	dh_compress -a
	dh_fixperms -a
	dh_shlibdeps -a
	dh_installdeb -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

get-orig-source:
	# Cleaning
	rm -rf cjk-$(upstream_version)
	rm -f ../cjk_$(upstream_version).orig.tar.gz

	# Download the latest GIT version and downgrade it to $(unixdate).
	git clone git://git.sv.gnu.org/cjk.git cjk-$(upstream_version)

#	cd cjk-$(upstream_version) && git reset --hard `git rev-list --all -n 1 --before=$(unixdate)`

	# Make a pristine package.
	mkdir -p ../tarballs
# --no-name/-n is important because it won't save the timestamp in the
# 5th to 8th byte of the gzip.
	cd cjk-$(upstream_version) && git archive --format=tar --prefix=cjk-$(upstream_version)/ $$(git rev-list --all -n 1 --before=$(unixdate)) | gzip -9 --no-name -c > ../../tarballs/cjk_$(upstream_version).orig.tar.gz

	# Clean again, because we use svn-buildpackage afterwards.
	rm -rf cjk-$(upstream_version)

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