# Makefile for debian-guide-zh for Debian.
#
# GNU GPL v2, Copyright (C) 2000, Anthony Fok <foka@debian.org>
#
# Software packages required for building the Postscript and HTML files:
#     cjk-latex, latex2html, gs, slice, zh-autoconvert,
#     tfm-arphic-bsmi00lp, tfm-arphic-gbsn00lp

SHELL = /bin/sh

title-zh_CN = Debian GNU/Linux װʹõ
DCP-zh_CN   = Debian ļƻ
title-zh_TW = Debian GNU/Linux wˤΨϥξɽ
DCP-zh_TW   = Debian p

locales = zh_CN zh_TW
frames = frame noframe
comma = ,
dvitargets = $(foreach i,$(locales),debian-guide.$(i).dvi)
pstargets = $(foreach i,$(locales),debian-guide.$(i).ps.gz)
htmltargets = $(foreach i,$(locales),$(addprefix html-$(i)-,$(frames)))

# Primary targets

all: dvi ps html

dvi: $(dvitargets)

ps: $(pstargets) dvi

html: $(htmltargets)

# Common rule

debian-guide.zh_CN.tex debian-guide.zh_TW.tex \
debian-guide-zh_CN-html.tex debian-guide-zh_TW-html.tex: \
	debian-guide.zh.tex.in \
	tocn.pl totw.pl totw_zhe.pl tohtml.pl totw_zhe_html.pl

	slice	-o UNDEFuCN:debian-guide.zh_CN.tex.tmp \
		-o UNDEFuTW:debian-guide.zh_TW.tex.tmp \
		debian-guide.zh.tex.in

	# For LaTeX
	autogb < debian-guide.zh_CN.tex.tmp | ./tocn.pl \
		> debian-guide.zh_CN.tex
	./totw.pl < debian-guide.zh_TW.tex.tmp | ./totw_zhe.pl \
		> debian-guide.zh_TW.tex

	# For LaTeX2HTML
	./tohtml.pl < debian-guide.zh_CN.tex.tmp | autogb | ./tocn.pl \
		> debian-guide-zh_CN-html.tex
	./tohtml.pl < debian-guide.zh_TW.tex.tmp | ./totw.pl \
		| ./totw_zhe_html.pl > debian-guide-zh_TW-html.tex

	# Checking to ensure all <s<????>> and <t<????>> are translated...
	@if grep -q '<[st]<' *.tex; then \
		echo "Error!  Unsubstituted <s<????>> or <t<????>> found!" > /dev/stderr; \
		echo "Please double check $< and the Perl scripts." > /dev/stderr; \
		false; \
	fi

	# Good, it passes the test.

# Rules for building gzip'ed Postscript files.

%.zh_CN.dvi: %.zh_CN.tex GPL GPL.zh_CN
	latex $<
	makeindex $(basename $<).idx
	latex $<
	latex $<

GPL.zh_TW.cjk: GPL.zh_TW
	bg5conv < $< | sed -e 's/^\\def\\CJKpreproc{}//;' > $@

%.zh_TW.dvi: %.zh_TW.tex GPL GPL.zh_TW.cjk
	bg5latex $<
	makeindex $(basename $<).idx
	bg5latex $<
	bg5latex $<

%.ps: %.dvi
	dvips -D 600 -X 600 -Y 600 $< -o

%.ps.gz: %.ps
	gzip -9v $<

%-zh_CN-html.aux: %-zh_CN-html.tex
	latex $<
	latex $<
	rm -f $(<F:tex=dvi)

%-zh_TW-html.aux: %-zh_TW-html.tex
	bg5latex $<
	bg5latex $<
	rm -f $(<F:tex=dvi)

chineseb5.perl chinesegb.perl: chinese.perl.in
	slice	-o UNDEFuCN:chinesegb.perl.tmp \
		-o UNDEFuTW:chineseb5.perl \
		$<
	autogb < chinesegb.perl.tmp > chinesegb.perl
	rm -f chinesegb.perl.tmp

# Rules for building HTML pages.

$(htmltargets): locale = $(strip $(foreach i,$(locales),$(findstring $(i),$@)))
$(htmltargets): frame = $(patsubst html-$(locale)-%,%,$@)
$(htmltargets): frameopt = $(if $(filter-out noframe,$(frame)),$(comma)$(frame))
$(htmltargets): title = $(title-$(locale))
$(htmltargets): DCP = $(DCP-$(locale))
$(htmltargets): debian-guide-zh_CN-html.aux debian-guide-zh_TW-html.aux \
		GPL GPL.zh_CN GPL.zh_TW chineseb5.perl chinesegb.perl
	rm -rf $(locale)/html/$(frame)s
	-mkdir -p $(locale)/html/$(frame)s
	latex2html -html_version 4.0,table$(frameopt) -split 4 \
		-iso_language $(subst zh_,ZH.,$(locale)) \
		-t '$(title)' \
		-toc_stars -local_icons -show_section_numbers \
		-address 'John Goerzen / Ossama Othman / $(DCP)' \
		-dir $(locale)/html/$(frame)s \
		debian-guide-$(locale)-html.tex

# Cleaning up...

clean:
	rm -f *~ DEADJOE *.tex.tmp
	rm -f *.dvi *.ps *.ps.gz

distclean: clean
	rm -rf zh_CN zh_TW
	rm -f *.tex *.cjk *.aux *.idx *.ilg *.ind *.lof *.log *.lot *.toc
	rm -f chineseb5.perl chinesegb.perl GPL.zh_TW.cjk

.PHONY: all dvi ps html clean distclean $(htmltargets)
