#
# New Makefile Template (Osamu Aoki)
#
#  Build html(multi-page), txt, ps, pdf, and other formats.
#
# Should work both for a manual in the Debian Documentation Project
# manuals.sgml tree, and for the package build.
# ------------------------------------------------------------------- #
#                          WARNING                                    #
#         Use with caution, aimed at Woody system                     #
#   "ps" and "pdf" tends to break in some ill-configured systems      #
# ------------------------------------------------------------------- #
# Read local texmf.cnf file

export TEXMFCNF=texmf:

# Following default shall be edited by the coordinator for the entire
# set of languages.  If a subsection owner wishes to override settings,
# they can be overridden by running make with "make 'LANGS=fi'" etc.. 

# =================================================================== #
#                 Default configuration part: Customize               #
# =================================================================== #

# The directory in which this makefile resides must also contain a file
# called <directoryname>.[<language>.]sgml, which is the top-level file
# for the manual in this directory.

# Basename for language-dependent SGML (DDP default, generated)
#MANUAL := $(notdir $(CURDIR))
MANUAL := apt-howto

# Build type: Possible values are BUILD_TYPE = web|package|sf
BUILD_TYPE  := web

# Build option
BILD_OPT := 

#PUBLISHDIR := /org/www.debian.org/www/doc/manuals
PUBLISHDIR := ~/public_html

# Locale list of "publish" target for the DDP documents:
LANGS := ca de el en es fr it ja ko pl pt-br ru tr 

# All SGML source files
SGMLSRCS := $(foreach lang, $(LANGS), $(MANUAL).$(lang).sgml) 

# =================================================================== #
#                 Build target default part: Routine                  #
# =================================================================== #
all: html txt ps pdf README

# List of html stamp files to be built
HTMLS := $(foreach lang,$(LANGS),$(MANUAL).$(lang).html.stamp)

# List of txt to be built
TXTS := $(foreach lang,$(LANGS),$(MANUAL).$(lang).txt)

# List of dvi to be built
DVIS := $(foreach lang,$(LANGS),$(MANUAL).$(lang).dvi)

# List of ps to be built
PSS := $(foreach lang,$(LANGS),$(MANUAL).$(lang).ps)

# List of pdf to be built
PDFS := $(foreach lang,$(LANGS),$(MANUAL).$(lang).pdf)

# =================================================================== #
#                 Build target part: Customize                        #
# =================================================================== #
# If some languages have problems building, filter-out in here.

html: $(HTMLS)
text txt:  $(TXTS)
dvi:   $(DVIS)
ps:   $(shell echo $(PSS) | sed s/apt-howto.el.ps//g)
pdf:  $(shell echo $(PDFS) | sed s/apt-howto.el.pdf//g)
publish: publish-html publish-txt publish-ps publish-pdf

# =================================================================== #
#                 Build rule part: If not package build               #
# =================================================================== #

$(MANUAL).%.ent:
	echo "<!ENTITY language \"$*\">"                 > $@
	echo "<!ENTITY % lang-$* \"INCLUDE\">"          >> $@
	echo "<!ENTITY docdate \"$$(bin/getdocdate $*)\">" >> $@
	echo "<!ENTITY docversion \"CVS\">"             >> $@

# Version list
README: README.org
	-bin/doc-check -ha  >README.html
	-w3m -dump -cols 78 README.html >README.txt
	sed -e "/^  ----------------------------------------------------------------------------/r README.txt"  \
	   <README.org >README
	-rm README.html README.txt

# =================================================================== #
#                 Build rule part: Routine                            #
# =================================================================== #

# HTML

$(MANUAL).%.html.stamp: $(MANUAL).%.sgml  $(MANUAL).%.ent
	debiandoc2html $(BUILD_OPT) -t$(MANUAL) -l $$(echo $* | bin/getlocale) -c $<
	bin/fixhtml $* $(MANUAL).$*.html
# since $(MANUAL).%.html/index.%.html cannot be a target file
	touch $(MANUAL).$*.html.stamp

# TXT

$(MANUAL).%.txt: $(MANUAL).%.sgml $(MANUAL).%.ent
	debiandoc2text $(BUILD_OPT) -l     $$(echo $* | bin/getlocale) $<
	bin/fixtxt $* $(MANUAL).$*.txt

# generate LaTeX format file with all necessary languages preloaded
 
# even if language.dat is in . it isn't found without setting TEXINPUTS
# (. is always in path, so I do not understand this!)

latex.fmt: texmf/language.dat
	TEXINPUTS=$$(pwd)/texmf/:$$(kpsetool -n tex -p tex) \
	tex -ini latex.ini > /dev/null
#	fmtutil --byfmt latex --fmtdir .

pdflatex.fmt: texmf/language.dat
	TEXINPUTS=$$(pwd)/texmf/:$$(kpsetool -n pdftex -p tex) \
	pdftex -ini pdflatex.ini > /dev/null
#	fmtutil --byfmt pdflatex --fmtdir .

# required by thumbpdf (called from debiandoc2latexpdf)
pdftex.fmt: texmf/language.dat
	TEXINPUTS=$$(pwd)/texmf/:$$(kpsetool -n pdftex -p tex) \
	pdftex -ini pdftex.ini > /dev/null
#	fmtutil --byfmt pdftex --fmtdir .

latex.efmt: texmf/language.dat
	TEXINPUTS=$$(pwd)/texmf/:$$(kpsetool -n tex -p tex) \
	etex -ini latex.ini > /dev/null
#	fmtutil --byfmt latex --fmtdir .

pdflatex.efmt: texmf/language.dat
	TEXINPUTS=$$(pwd)/texmf/; \
	pdfetex -ini pdflatex.ini > /dev/null
#	fmtutil --byfmt pdflatex --fmtdir .

# required by thumbpdf (called from debiandoc2latexpdf)
pdftex.efmt: texmf/language.dat
	TEXINPUTS=$$(pwd)/texmf/; \
	pdfetex -ini pdftex.ini > /dev/null
#	fmtutil --byfmt pdftex --fmtdir .

# DVI
$(MANUAL).el.dvi: $(MANUAL).el.sgml
	@echo Locale 'el' not yet supported

$(MANUAL).%.dvi: $(MANUAL).%.sgml $(MANUAL).%.ent latex.fmt
	bin/debiandoc2latexdvi $(BUILD_OPT) -l  $$(echo $* | bin/getlocale) $<

# PS
$(MANUAL).el.ps: $(MANUAL).el.sgml
	@echo Locale 'el' not yet supported

$(MANUAL).%.ps: $(MANUAL).%.sgml $(MANUAL).%.ent latex.fmt latex.efmt
	bin/debiandoc2latexps $(BUILD_OPT) -l  $$(echo $* | bin/getlocale) $<

# PDF
$(MANUAL).el.pdf: $(MANUAL).el.sgml
	@echo Locale 'el' not yet supported

$(MANUAL).%.pdf: $(MANUAL).%.sgml $(MANUAL).%.ent pdflatex.fmt pdftex.fmt pdflatex.efmt pdftex.efmt
	bin/debiandoc2latexpdf $(BUILD_OPT) -l $$(echo $* | bin/getlocale) $<


# =================================================================== #
#                 Build rule part: Web publish                        #
# =================================================================== #

publish-html: html
	test -d $(PUBLISHDIR)/$(MANUAL) \
	   || install -d -m 755 $(PUBLISHDIR)/$(MANUAL)
	rm -f $(PUBLISHDIR)/$(MANUAL)/*.html
	# install all html
	@$(foreach lang,$(LANGS),\
	 install -p -m 644 $(MANUAL).$(lang).html/*.html \
	          $(PUBLISHDIR)/$(MANUAL)/ ;\
	)
# Create index page
	# Make index page for web
ifneq ("$(BUILD_TYPE)", "package")
	@{ \
	echo "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\">" ; \
	echo "<html>" ;\
	echo "<head>" ;\
	echo "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">" ;\
	echo "<title>APT-HOWTO</title>" ;\
	echo "</head>" ;\
	echo "<body>" ;\
	echo "<hr>" ;\
	echo "<h1>APT HOWTO</h1>" ;\
	echo "<hr>" ;\
	echo "<dl>" ;\
	} > $(PUBLISHDIR)/$(MANUAL)/index.html
	@{$(foreach lang,$(LANGS), \
	 . bin/makeindexhtml "$(lang)" "$(MANUAL)" ; ) } >> $(PUBLISHDIR)/$(MANUAL)/index.html 
	@{ \
	echo "</dl>" ;\
	echo "</body>" ;\
	echo "</html>" ;\
	} >> $(PUBLISHDIR)/$(MANUAL)/index.html
endif

publish-txt:  txt
	# install all txt
	test -d $(PUBLISHDIR)/$(MANUAL) \
	   || install -d -m 755 $(PUBLISHDIR)/$(MANUAL)
	rm -f $(PUBLISHDIR)/$(MANUAL)/*.txt
	# install all txt
	@$(foreach lang,$(LANGS),\
	 install -p -m 644 $(MANUAL).$(lang).txt \
	          $(PUBLISHDIR)/$(MANUAL)/ ;\
	)

publish-ps:  ps
	# install all ps
	test -d $(PUBLISHDIR)/$(MANUAL) \
	   || install -d -m 755 $(PUBLISHDIR)/$(MANUAL)
	rm -f $(PUBLISHDIR)/$(MANUAL)/*.ps
	# install all ps
	@$(foreach lang,$(LANGS),\
	 install -p -m 644 $(MANUAL).$(lang).ps \
	          $(PUBLISHDIR)/$(MANUAL)/ ;\
	)

publish-pdf:  pdf
	# install all pdf
	test -d $(PUBLISHDIR)/$(MANUAL) \
	   || install -d -m 755 $(PUBLISHDIR)/$(MANUAL)
	rm -f $(PUBLISHDIR)/$(MANUAL)/*.pdf
	# install all pdf
	@$(foreach lang,$(LANGS),\
	 install -p -m 644 $(MANUAL).$(lang).pdf \
	          $(PUBLISHDIR)/$(MANUAL)/ ;\
	)

#====[ DDP test-script ]==============================================
web:
	$(MAKE) BUILD_TYPE=web "LANGS=en fr" "LANGS2=en" publish-all

#====[ validating SGML ]=======================================================
validate:
	set -x; for i in $(LANGS); do $(MAKE) validate-$$i ; done

validate-%: $(SGMLSRCS) $(MANUAL).%.ent 
	nsgmls -gues -wall $(MANUAL).$*.sgml

# check hyphenations created by LaTeX against validLaTeX.$*.hyphen
check-hyphen:
	for i in $(LANGS); do $(MAKE) check-hyphen-$$i ; done

check-hyphen-%: $(MANUAL).%.dvi
	hyphen_show $(MANUAL).$*.dvi validLaTeX.$*.hyphen


#====[ cleaning up ]===========================================================
distclean: clean
	rm -Rf $(PUBLISHDIR)/$(MANUAL)

# This rely on BASH
clean:
	rm -f $(MANUAL)*.{txt,ps,dvi,pdf,info*,log,tex,aux,toc,sasp*,out,tov,tpt,hyp}
	rm -f *~ prior.aux pprior.aux *.log typescript README README.html
	rm -f {latex,pdflatex,pdftex}.{fmt,log,efmt} *.tex[01]pdf *.tex[01]ps
	rm -f *.error $(MANUAL).*.ent date.ent
	rm -rf $(MANUAL)*.html *stamp

.PHONY: all html text txt ps pdf files tar sf \
	publish publish-all publish-html publish-ps publish-pdf \
	clean distclean validate

