#######################################################################
# Build debian-reference (v2)
# vim: set ts=8:
#######################################################################
### key adjustable parameters
#######################################################################
# base file name excluding file extension
MANUAL	:=	debian-reference
# languages translated with PO files
LANGPO	:=	ja fr it pt de
# languages to skip generation of PDF files (not used now)
NOPDF	:=	ja fr it pt de
# languages to build document
LANGALL	=	en $(LANGPO)

ifndef TMPDIR
TMPDIR	:= $(CURDIR)/tmp
endif

# Change $(DRAFTMODE) from "yes" to "maybe" when this document 
# should go into production mode
#DRAFTMODE      := yes
DRAFTMODE       := maybe
export DRAFTMODE
#######################################################################
### basic constant parameters
#######################################################################
# Directories (no trailing slash)
DXSL	:=	xslt
DBIN	:=	bin
DASC	:=	asciidoc
DPO	:=	po
DIMG	:=	/usr/share/xml/docbook/stylesheet/nwalsh/images

# Program name and option
XLINT	:=	xmllint --format
XPNO	:=	xsltproc --novalid --nonet
XPINC	:=	xsltproc --novalid --nonet --xinclude
# The threshold should be 80 if translation is completed.
THRESHOLD:=	0
TRANSLATE:=	po4a-translate  -M utf-8          --format docbook --keep $(THRESHOLD) -v 
GETTEXT	:=	po4a-gettextize -M utf-8 -L utf-8 --format docbook
UPDATEPO:=	msgmerge --update --previous
MSGATTR	:=	msgattrib
MSGCAT	:=	msgcat
DBLATEX	:=	dblatex

# Debian packge archive URL
#DEBM	:=	http://ftp.us.debian.org/debian/dists
DEBM	:=	http://ftp.jp.debian.org/debian/dists
# Debian popcon data source URL
UPOPC	:=	http://popcon.debian.org/all-popcon-results.txt.gz
# Debian release name and arch used
CODE	:=	sid
ARCH	:=	amd64
UDEBA	:=	$(DEBM)/$(CODE)
UDEBB	:=	$(DEBM)/experimental

# cached data removed for package related to remotely fetched data
RCACHE	:=      all-popcon-results.txt packages.txt packages.bkup.txt pkg.lst
# AsciiDoc source file names in $(DASC) directories for local update
ASC_NM	:=	00_preface.txt 01_tutorial.txt 02_package.txt \
		03_sysinit.txt 04_auth.txt 05_network.txt 06_netapp.txt \
		07_xwindow.txt 08_i18nl10n.txt 09_systips.txt 10_datamngt.txt \
		11_dataconv.txt 12_program.txt 99_appendix.txt \
		copyright.txt header1.txt header2.txt $(MANUAL).txt
# source asciidoc files (local asciidoc conversion script used for stability)
SRC_ASC :=	$(addprefix $(DASC)/, $(ASC_NM))
# source XML inclusion files (excluding common.ent)
ENT_STAT:=	datadatepop.ent datadatesize.ent popcon.ent pkgsize.ent
ENT_ALL	:=	$(ENT_STAT) common.ent
# source PO files for all languages (build prcess requires these)
SRC_PO	:=	$(addsuffix .po, $(addprefix  $(DPO)/, $(LANGPO)))
# source XML files for all languages (build prcess requires these)
SRC_XML	:=	$(addsuffix .xml, $(addprefix  $(MANUAL)., $(LANGALL)))

#######################################################################
# Used as $(call check-command, <command>, <package>)
define check-command
set -e; if ! which $(1) >/dev/null; then \
  echo "Missing command: $(1), install package: $(2)"; \
  false; \
fi
endef

#######################################################################
# $ make all       # build all
#######################################################################
.PHONY: all
# set LANGPO to limit language to speed up build
all: css html txt epub 

#######################################################################
# $ make test      # build html for testing (for Translator)
#######################################################################
.PHONY: test
test: html css

#######################################################################
# $ make publish   # build html text from RAWXML/PO for DDP
#######################################################################
.PHONY: package
# $(PUBLISHDIR) is set to be: /org/www.debian.org/www/doc/manuals for master-www
publish:
	-mkdir -p $(PUBLISHDIR)/$(MANUAL)
	$(MAKE) css html txt "TMPDIR=$(PUBLISHDIR)/$(MANUAL)"

#######################################################################
# $ make clean     # clean files ready for tar 
#######################################################################
.PHONY: clean
clean:
	-rm -f *.swp *~ *.tmp
	-rm -f $(DPO)/*~ $(DPO)/*.mo $(DPO)/*.po.*
	-rm -f $(DASC)/*~
	-rm -rf $(TMPDIR) $(CURDIR)/tmp
	-rm -f $(addsuffix .xml, $(addprefix $(MANUAL)., $(LANGALL)))
	-rm -f $(MANUAL).en.xml $(MANUAL).en.xmlt header.txt
	-rm -f $(RCACHE)

#######################################################################
# $ make distclean # clean files to reset RAWXML/ENT/POT
#######################################################################
.PHONY: distclean
distclean: clean
	-rm -f $(MANUAL).raw.xml
	-rm -f $(ENT_ALL)
	-rm -f $(DPO)/*.pot
	-rm -f fuzzy.log

#######################################################################
# $ make rawxml    # update RAWXML from ASCIIDOC
#######################################################################
.PHONY: rawxml
rawxml: $(MANUAL).raw.xml

$(MANUAL).raw.xml: $(SRC_ASC)
	@$(call check-command, python, python)
	@$(call check-command, xmllint, xmllint)
	$(DBIN)/asciidoc -a 'newline=\n' -o - $(DASC)/$(MANUAL).txt |\
	sed -e "/<\/author>/r $(DASC)/copyright.txt" |\
	$(XLINT) -  |\
	sed -f $(DBIN)/replace > $(MANUAL).raw.xml

#######################################################################
# $ make entity    # update ENT and local cache data from RAWXML/REMOTE
#######################################################################
# This ensures not to run remote except by manually
$(ENT_STAT):
	@echo "******** You are missing ENTITY files *******"
	@echo "	$(ENT_STAT)"
	@echo "*********************************************"
	@echo " run \"make entity\""
	@echo "*********************************************"
	@false


common.ent: $(MANUAL).raw.xml
	# Release for package match with actual situation
	echo "<!ENTITY build-date \"$(shell date -u +'%F %T %Z')\">"	>  common.ent
	echo "<!ENTITY arch \"$(ARCH)\">"				>> common.ent
	echo "<!ENTITY codename-stable   \"jessie\">"			>> common.ent
	echo "<!ENTITY Codename-stable   \"Jessie\">"                  >> common.ent
	echo "<!ENTITY codename-testing  \"stretch\">"			>> common.ent
	echo "<!ENTITY Codename-testing  \"Stretch\">"                   >> common.ent
	echo "<!ENTITY codename-nexttesting  \"buster\">"		>> common.ent
	echo "<!ENTITY Codename-nexttesting  \"Buster\">"		>> common.ent
	echo "<!ENTITY codename-unstable \"sid\">"			>> common.ent
	echo "<!ENTITY Codename-unstable \"Sid\">"                      >> common.ent

.PHONY: entity
entity: rawxml
	@$(call check-command, wget, wget)
	@$(call check-command, xsltproc, xsltproc)
	@$(call check-command, grep-dctrl, dctrl-tools)
	# PACKAGE (sid)
	wget -O - $(UDEBA)/main/binary-$(ARCH)/Packages.xz      | xzcat - > packages.main.tmp
	grep-dctrl -e -sPackage,Installed-Size -P "." packages.main.tmp > packages.main
	rm packages.main.tmp
	wget -O - $(UDEBA)/contrib/binary-$(ARCH)/Packages.xz   | xzcat - > packages.contrib.tmp
	grep-dctrl -e -sPackage,Installed-Size -P "." packages.contrib.tmp > packages.contrib
	rm packages.contrib.tmp
	wget -O - $(UDEBA)/non-free/binary-$(ARCH)/Packages.xz  | xzcat - > packages.non-free.tmp
	grep-dctrl -e -sPackage,Installed-Size -P "." packages.non-free.tmp > packages.non-free
	rm packages.non-free.tmp
	cat packages.main packages.contrib packages.non-free >packages.txt
	#
	echo "<!ENTITY all-packages \"$$(grep -e '^Package:' packages.txt | wc -l)\">"			>  datadatesize.ent
	echo "<!ENTITY main-packages \"$$( grep -e '^Package:' packages.main | wc -l)\">"		>> datadatesize.ent
	echo "<!ENTITY contrib-packages \"$$(grep -e '^Package:' packages.contrib | wc -l)\">"		>> datadatesize.ent
	echo "<!ENTITY non-free-packages \"$$(grep -e '^Package:' packages.non-free | wc -l)\">"	>> datadatesize.ent
	echo "<!ENTITY pkgsize-date \"$(shell date -u +'%F %T %Z')\">"					>> datadatesize.ent
	rm packages.main packages.contrib packages.non-free
	# PACKAGE (experimental)
	wget -O - $(UDEBB)/main/binary-$(ARCH)/Packages.xz      | xzcat - > packages.bkup.tmp
	grep-dctrl -e -sPackage,Installed-Size -P "." packages.bkup.tmp > packages.bkup.txt
	rm packages.bkup.tmp
	# PACKAGE LIST of packages mentioned in the source XML
	$(XPNO) $(DXSL)/pkg.xsl $(MANUAL).raw.xml > pkg.lst
	# ENT for package size
	sort pkg.lst | uniq | $(DBIN)/sizeent packages.txt packages.bkup.txt	> pkgsize.ent
	# POPCON
	wget -O - $(UPOPC) | zcat - > all-popcon-results.txt
	echo "<!ENTITY pop-date \"$(shell date -u +'%F %T %Z')\">" > datadatepop.ent
	echo "<!ENTITY pop-submissions \"$$(sed -n -e 's/^Submissions: *\([^ ]*\) *$$/\1/p' < all-popcon-results.txt)\">"	>  popcon.ent
	echo "<!ENTITY pop-architectures \"$$(grep -e '^Architecture:' all-popcon-results.txt | wc -l)\">"			>> popcon.ent
	echo "<!ENTITY pop-packages \"$$(grep -e '^Package:' all-popcon-results.txt | wc -l)\">"				>> popcon.ent
	TOTAL=$$(sed -n -e 's/^Submissions: *\([^ ]*\) *$$/\1/p' < all-popcon-results.txt) ;\
	grep -e '^Package:' all-popcon-results.txt | grep -f pkg.lst | $(DBIN)/popconent $$TOTAL				>> popcon.ent

#######################################################################
# $ make po        # update all PO from RAWXML
#######################################################################
.PHONY: po pot
pot: $(DPO)/templates.pot
po: $(SRC_PO)

# Do not record line number to avoid useless diff in po/*.po files: --no-location
# Do not update templates.pot if contents are the same as before; -I '^"POT-Creation-Date:'
$(DPO)/templates.pot: $(MANUAL).en.xmlt FORCE
	@$(call check-command, po4a-gettextize, po4a)
	@$(call check-command, msgcat, gettext)
	$(GETTEXT) -m $(MANUAL).en.xmlt | $(MSGCAT) --no-location -o $(DPO)/templates.pot.new -
	if diff -I '^"POT-Creation-Date:' -q $(DPO)/templates.pot $(DPO)/templates.pot.new ; then \
	  echo "Don't update templates.pot" ;\
	  touch $(DPO)/templates.pot ;\
	  rm -f $(DPO)/templates.pot.new ;\
	else \
	  echo "Update templates.pot" ;\
	  mv -f $(DPO)/templates.pot.new $(DPO)/templates.pot ;\
	fi
	: > fuzzy.log

# Always update
$(DPO)/%.po: $(DPO)/templates.pot FORCE
	@$(call check-command, msgmerge, gettext)
	$(UPDATEPO) $(DPO)/$*.po $(DPO)/templates.pot
	MESS1="no-obsolete  $*  `$(MSGATTR) --no-obsolete  $(DPO)/$*.po |grep ^msgid |sed 1d|wc -l`";\
	MESS2="untranslated $*  `$(MSGATTR) --untranslated $(DPO)/$*.po |grep ^msgid |sed 1d|wc -l`";\
	MESS3="fuzzy        $*  `$(MSGATTR) --fuzzy        $(DPO)/$*.po |grep ^msgid |sed 1d|wc -l`";\
	echo "$$MESS1" >>fuzzy.log ; \
	echo "$$MESS2" >>fuzzy.log ; \
	echo "$$MESS3" >>fuzzy.log ; \
	echo "" >>fuzzy.log

FORCE:

#######################################################################
# $ make wrap       # wrap all PO
#######################################################################
.PHONY: wrap nowrap wip
wrap:
	@$(call check-command, msgcat, gettext)
	for XX in $(foreach LX, $(LANGPO), $(DPO)/$(LX).po); do \
	$(MSGCAT) -o $$XX $$XX ;\
	done
nowrap:
	@$(call check-command, msgcat, gettext)
	for XX in $(foreach LX, $(LANGPO), $(DPO)/$(LX).po); do \
	$(MSGCAT) -o $$XX --no-wrap $$XX ;\
	done

wip:
	@$(call check-command, msgattrib, gettext)
	for XX in $(foreach LX, $(LANGPO), $(DPO)/$(LX).po); do \
	$(MSGATTR) -o $$XX.fuzz --fuzzy        $$XX ;\
	$(MSGATTR) -o $$XX.untr --untranslated $$XX ;\
	done

#######################################################################
# $ make xml       # update all *.XML from RAWXML/ENT/PO/ADD
#######################################################################
.PHONY: xml
xml: $(SRC_XML)

# Update URL list header from $(MANUAL).raw.xml
header.txt: $(DASC)/header1.txt $(MANUAL).raw.xml $(DASC)/header2.txt
	cat $(DASC)/header1.txt >  header.txt
	$(XPNO) $(DXSL)/urls.xsl $(MANUAL).raw.xml | sort | uniq |\
	sed -e "s/&/\&amp;/g"   >> header.txt
	cat $(DASC)/header2.txt >> header.txt

# Replace table contents with @-@popcon*@-@ and @@@psize*@-@ and 
# fix URL referencees and table ID.
$(MANUAL).en.xml: $(MANUAL).raw.xml header.txt common.ent
	@$(call check-command, xsltproc, xsltproc)
	# use asciidoc generated xml file as main contents 
	$(XPNO) $(DXSL)/table.xsl $(MANUAL).raw.xml |\
	$(DBIN)/colspec.py  |\
	sed -e '/<!DOCTYPE /d' -e "1r header.txt" |\
	sed -e 's/@-@amp@-@/\&/g' -e 's/@-@\([^@]\+\)@-@/\&\1;/g' > $@

# Replace table contents with dummy text and 
# fix URL referencees and table ID as the template for translation.
# This avoids bloated PO/POT files. (tablet.xsl used insted of table.xsl)
$(MANUAL).en.xmlt: $(MANUAL).raw.xml header.txt
	@$(call check-command, xsltproc, xsltproc)
	# use asciidoc generated xml file as main contents 
	$(XPNO) $(DXSL)/tablet.xsl $(MANUAL).raw.xml |\
	$(DBIN)/colspec.py  |\
	sed -e '/<!DOCTYPE /d' -e "1r header.txt" |\
	sed -e 's/@-@amp@-@/\&/g' -e 's/@-@\([^@]\+\)@-@/\&\1;/g' > $@

$(MANUAL).%.xml: $(DPO)/%.po $(MANUAL).en.xml
	@$(call check-command, po4a-translate, po4a)
	@$(call check-command, msgcat, gettext)
	if [ -f $(DPO)/$*.add ]; then \
	$(TRANSLATE) -m $(MANUAL).en.xml -a $(DPO)/$*.add -p $(DPO)/$*.po -l $(MANUAL).$*.xml ;\
	else \
	$(TRANSLATE) -m $(MANUAL).en.xml -p $(DPO)/$*.po -l $(MANUAL).$*.xml ;\
	fi

#######################################################################
# $ make css       # update CSS and DIMG in $(TMPDIR)
#######################################################################
.PHONY: css
css:
	-rm -rf $(TMPDIR)/images
	mkdir -p $(TMPDIR)/images
	cp -f $(DXSL)/$(MANUAL).css $(TMPDIR)/$(MANUAL).css
	echo "AddCharset UTF-8 .txt" > $(TMPDIR)/.htaccess
	#cd $(DIMG) ; cp caution.png home.png important.png next.png note.png prev.png tip.png up.gif warning.png $(TMPDIR)/images
	cd $(DIMG) ; cp caution.png important.png note.png tip.png up.gif warning.png $(TMPDIR)/images
	cd png ; cp home.png next.png prev.png $(TMPDIR)/images

#######################################################################
# $ make html      # update all HTML in $(TMPDIR)
#######################################################################
.PHONY: html
html:	$(foreach LX, $(LANGALL), $(TMPDIR)/index.$(LX).html)

$(TMPDIR)/index.%.html: $(MANUAL).%.xml $(ENT_ALL)
	@$(call check-command, xsltproc, xsltproc)
	-mkdir -p $(TMPDIR)
	$(XPINC)   --stringparam base.dir $(TMPDIR)/ \
                --stringparam html.ext .$*.html \
                $(DXSL)/style-html.xsl $<

#######################################################################
# $ make txt       # update all Plain TEXT in $(TMPDIR)
#######################################################################
.PHONY: txt
txt:	$(foreach LX, $(LANGALL), $(TMPDIR)/$(MANUAL).$(LX).txt.gz)

# style-txt.xsl provides work around for hidden URL links by appending them explicitly.
$(TMPDIR)/$(MANUAL).%.txt.gz: $(MANUAL).%.xml $(ENT_ALL)
	@$(call check-command, w3m, w3m)
	@$(call check-command, xsltproc, xsltproc)
	-mkdir -p $(TMPDIR)
	@test -n "`which w3m`"  || { echo "ERROR: w3m not found. Please install the w3m package." ; false ;  }
	$(XPINC) $(DXSL)/style-txt.xsl $< | LC_ALL=en_US.UTF-8 \
	  w3m -o display_charset=UTF-8 -cols 70 -dump -no-graph -T text/html | \
	  gzip -n -9 - > $@


#######################################################################
# $ make pdf       # update all PDF in $(TMPDIR)
#######################################################################
.PHONY: pdf
pdf:	$(foreach LX, $(LANGALL), $(TMPDIR)/$(MANUAL).$(LX).pdf)

$(foreach LX, $(NOPDF), $(TMPDIR)/$(MANUAL).$(LX).pdf):
	-mkdir -p $(TMPDIR)
	echo "PDF generation skipped." >$@

# dblatex.xsl provide work around for hidden URL links by appending them explicitly.
$(TMPDIR)/$(MANUAL).%.pdf: $(MANUAL).%.xml $(ENT_ALL)
	@$(call check-command, dblatex, dblatex)
	@$(call check-command, xsltproc, xsltproc)
	-mkdir -p $(CURDIR)/tmp
	@test -n "`which $(DBLATEX)`"  || { echo "ERROR: dblatex not found. Please install the dblatex package." ; false ;  }
	export TEXINPUTS=".:"; \
	export TMPDIR="$(CURDIR)/tmp/"; \
	$(XPINC) $(DXSL)/dblatex.xsl $<  | \
	$(DBLATEX) --style=native \
		--debug \
		--backend=xetex \
		--xsl-user=$(DXSL)/user_param.xsl \
		--xsl-user=$(DXSL)/xetex_param.xsl \
		--param=draft.mode=$(DRAFTMODE) \
		--param=lingua=$* \
		--output=$@ - || { echo "OMG!!!!!! XXX_CHECK_XXX ... Do not worry ..."; true ; }

#######################################################################
# $ make tex       # update all TeX source in $(TMPDIR)
#######################################################################
.PHONY: tex
tex:	$(foreach LX, $(LANGALL), $(TMPDIR)/$(MANUAL).$(LX).tex)

# dblatex.xsl provide work around for hidden URL links by appending them explicitly.
$(TMPDIR)/$(MANUAL).%.tex: $(MANUAL).%.xml $(ENT_ALL)
	-mkdir -p $(CURDIR)/tmp
	@test -n "`which $(DBLATEX)`"  || { echo "ERROR: dblatex not found. Please install the dblatex package." ; false ;  }
	export TEXINPUTS=".:"; \
	export TMPDIR="$(CURDIR)/tmp/"; \
	$(XPINC) $(DXSL)/dblatex.xsl $<  | \
	$(DBLATEX) --style=native \
		--debug \
		--type=tex \
		--backend=xetex \
		--xsl-user=$(DXSL)/user_param.xsl \
		--xsl-user=$(DXSL)/xetex_param.xsl \
		--param=draft.mode=$(DRAFTMODE) \
		--param=lingua=$* \
		--output=$@ - || { echo "OMG!!!!!! XXX_CHECK_XXX ... Do not worry ..."; true ; }

#######################################################################
# $ make epub      # update all epub in $(TMPDIR)
#######################################################################
.PHONY: epub
epub:	$(foreach LX, $(LANGALL), $(TMPDIR)/$(MANUAL).$(LX).epub)

$(TMPDIR)/$(MANUAL).%.epub: $(MANUAL).%.xml $(ENT_ALL)
	@$(call check-command, xsltproc, xsltproc)
	-mkdir -p $(TMPDIR)/$*/
	cd $(TMPDIR)/$*/ ; $(XPINC) $(CURDIR)/$(DXSL)/style-epub.xsl $(CURDIR)/$<
	cp -f $(DXSL)/mimetype $(TMPDIR)/$*/mimetype
	cp -f $(DXSL)/debian-reference.css $(TMPDIR)/$*/OEBPS/debian-reference.css
	cp -f $(DXSL)/debian-openlogo.png $(TMPDIR)/$*/OEBPS/debian-openlogo.png
	cd $(TMPDIR)/$*/ ; zip -r $@ ./

#######################################################################
### Utility targets
#######################################################################
#######################################################################
# $ make rsync     
# export build result to http://people.debian.org/~osamu/debian-reference/
#######################################################################
.PHONY: rsync
rsync: all
	rsync -avz $(TMPDIR)/ osamu@people.debian.org:public_html/debian-reference/

#######################################################################
# $ make url       # check duplicate URL references
#######################################################################
.PHONY: url
url: $(MANUAL).en.xml
	@echo "----- Duplicate URL references (start) -----"
	-sed -ne "/^<\!ENTITY/s/<\!ENTITY \([^ ]*\) .*$$/\" \1 \"/p"  < $< | uniq -d | xargs -n 1 grep $< -e  | grep -e "^<\!ENTITY"
	@echo "----- Duplicate URL references (end) -----"

#######################################################################
# Translate all
#######################################################################
$(DPO)/wikipedia.%.pot: $(DPO)/wikipedia.list
	$(DBIN)/interwiki $* "PRINT" < $< > $@

#######################################################################
# Translate untranslated
#######################################################################
$(DPO)/wikipedia.%.po: $(DPO)/wikipedia.%.list
	$(DBIN)/interwiki $* "NO"   < $< > $@


