# Build all Debian packaging examples and produce documentation
# Copyright (C) 2015 Osamu Aoki

# Define if not defined (Prevent error when Make is run independently)
SOURCE_DATE_EPOCH ?= "1400000000"

.PHONY: all build clean distclean
# build document
all: build

# run "make prep" before "make log" to keep example package source tree consistent
# run "make logs" before "make all" to make logs for building example packages
# make sure to commit logs to the git repository as a part of the document source
include Makefile.pkg

# Normalize build environment (Debian default is LANG=C etc.)
LC_ALL := C.UTF-8
export LC_ALL

# 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

#######################################################################
# Document translation strategy: use po4a
# 1. Generate $(BASEXML) using AsciiDoc with docbook4 backend
#    (Ease of writing main cantents)
# 2. Make docbook 5 source $(MANUAL).en.x01 with db4-upgrade.xsl
# 3. Write the info header section as $(HEADXML)
# 4. Merge $(HEADXML) and $(MANUAL).en.x01 as $(MANUAL).en.x02
# 5. Generate po/$(MANUAL).pot from $(MANUAL).en.x02 with po4a
# 6. Translate po/$(MANUAL).pot and create po/<lang>.po
# 7. Generate $(MANUAL).<lang>.x02 from $(MANUAL).en.x02 and po/<lang>.po
#    using po4a
# 8. Make $(MANUAL).<lang>.xml from $(MANUAL).<lang>.x02
# 9. Build html, ... from $(MANUAL).<lang>.xml
#######################################################################
# Build documentation
MANUAL	:= debmake-doc
DEBEMAIL := osamu@debian.org
DEBFULLNAME := "Osamu Aoki"
ASCIIDOC_DIR := asciidoc
BASEXML := $(ASCIIDOC_DIR)/$(MANUAL).xml
HEADXML := $(ASCIIDOC_DIR)/head.txt
XSLT_DIR := xslt
BASEDIR := $(CURDIR)/basedir
TMPDIR := $(CURDIR)/tmp

# languages translated with PO files (not used now)
LANGPO ?= ja zh-cn zh-tw de ru
# languages to skip generation of PDF files (not used now)
NOPDF ?=
# languages to build document
LANGALL := en $(LANGPO)
SRC_PO := $(addsuffix .po, $(addprefix  po4a/po/, $(LANGPO)))
SRC_X02 := $(addsuffix .x02, $(addprefix  $(MANUAL)., $(LANGPO)))
SRC_XML := $(addsuffix .xml, $(addprefix  $(MANUAL)., $(LANGALL)))
#######################################################################
# Build time determined text
# version of the debmake command used
DEBMAKEVERSION ?= $(shell { debmake -v 2>&1 | sed -ne '/^debmake/s/^.*(version: \([0-9.]*\)).*$$/\1/p' ; })
# version of this Debian package (debian/changelog)
DEBVERSION ?= $(shell { dpkg-parsechangelog -SVersion || echo "vcs-0.0" ; })
# short date of this Debian package (debian/changelog)
DEBDATE ?= $(shell { date +'%Y-%m-%d' -d"`dpkg-parsechangelog -SDate`" || date +'(No changelog) %Y-%m-%d' ; })
# debhelper compat = debhelper major version
DHCOMPAT ?= $(shell { dpkg-query -f'$${source:Version}' -W debhelper|sed 's/\..*$$//' ; })
#######################################################################
# Constants for asciidoc -> xml -> any conversion
# Program name and option
XSLT := xsltproc --novalid --nonet --xinclude
MKDIRP := mkdir -p
MSGCAT := msgcat
MSGFMT := msgfmt
MSGMERGE := msgmerge --update --previous
MSGATTRIB := msgattrib
DBLATEX	:= dblatex
# Change $(DRAFTMODE) from "yes" to "maybe" when this document
# should go into production mode
#export DRAFTMODE := yes
export DRAFTMODE := maybe
#######################################################################

build: $(wildcard $(ASCIIDOC_DIR)/*.txt)
	: # ========================== BUILD DOCS =============================
	# process ASCIIDOC after $(pkg) targets even under the parallel build
	$(MAKE) base-xml
	echo "LC_ALL='$(LC_ALL)'"
	echo "LANGPO='$(LANGPO)'"
	echo "LANGALL='$(LANGALL)'"
	echo "NOPDF='$(NOPDF)'"
	echo "BASEDIR='$(BASEDIR)'"
	echo "TMPDIR='$(TMPDIR)'"
	$(MAKE) all-xml	                # XML docs for all PO
	-mkdir -p $(TMPDIR)
	-mkdir -p $(BASEDIR)/html
	# leave fuzzy status in the package build log
	-cat fuzzy.log
	$(MAKE) css
	$(MAKE) html
	$(MAKE) txt
	$(MAKE) pdf
	$(MAKE) epub

clean:
	: # ========================== CLEAN SOURCE ===========================
	echo "LC_ALL='$(LC_ALL)'"
	echo "NOPDF='$(NOPDF)'"
	echo "BASEDIR='$(BASEDIR)'"
	echo "TMPDIR='$(TMPDIR)'"
	-rm -rf $(TMPDIR)
	-$(MAKE) -C $(ASCIIDOC_DIR) clean

distclean: clean

dbk:
	-mkdir -p $(TMPDIR)
	-mkdir -p $(BASEDIR)/html
	$(MAKE) -C $(ASCIIDOC_DIR) xml
	$(MAKE) css html

.PHONY: dbk

#######################################################################
# Normal doc content as English base xml
$(BASEXML): $(wildcard $(ASCIIDOC_DIR)/*.txt)
	$(MAKE) -C $(ASCIIDOC_DIR) xml	# base XML doc

# set last chapter as appendix.
$(MANUAL).en.x01: $(BASEXML)
	@$(call check-command, xsltproc, xsltproc)
	$(XSLT) $(XSLT_DIR)/modappendix.xsl $< > $@

# header is manually written and main content is generated from asciidoc
$(MANUAL).en.x02: $(MANUAL).en.x01 $(HEADXML)
	cat $(HEADXML) > $@
	sed -e '1,/<\/bookinfo>/d' $(MANUAL).en.x01 >> $@

$(MANUAL).po.stamp: $(MANUAL).en.x02 $(SRC_PO)
	-mkdir -p $(TMPDIR)
	po4a -v po4a/po4a.cfg

$(SRC_X02): $(MANUAL).po.stamp

fuzzy.log: $(SRC_X02)
	: > fuzzy.log
	for lang in $(LANGPO); do \
	MESS1="no-obsolete  $$lang  `$(MSGATTRIB) --no-obsolete  po4a/po/$$lang.po |grep ^msgid |sed 1d|wc -l`";\
	MESS2="untranslated $$lang  `$(MSGATTRIB) --untranslated po4a/po/$$lang.po |grep ^msgid |sed 1d|wc -l`";\
	MESS3="fuzzy        $$lang  `$(MSGATTRIB) --fuzzy        po4a/po/$$lang.po |grep ^msgid |sed 1d|wc -l`";\
	echo "$$MESS1" >>fuzzy.log ; \
	echo "$$MESS2" >>fuzzy.log ; \
	echo "$$MESS3" >>fuzzy.log ; \
	echo "" >>fuzzy.log ; \
	done

$(MANUAL).%.xml: $(MANUAL).%.x02
	sed \
	-e "s/@@@debmakeversion@@@/$(DEBMAKEVERSION)/g" \
	-e "s/@@@debversion@@@/$(DEBVERSION)/g" \
	-e "s/@@@debdate@@@/$(DEBDATE)/g" \
	-e "s/@@@dhcompat@@@/$(DHCOMPAT)/g" \
	< $^ > $@

.FORCE:
	echo "FORCE"

doc-clean:
	-rm -f *.swp *~ *.tmp
	-rm -f po4a/po/*~
	-rm -rf $(TMPDIR)
	-rm -rf $(BASEDIR)
	-rm -rf $(LANGALL)
	-rm -r $(MANUAL).po.stamp
	-rm -f $(addsuffix .x00, $(addprefix $(MANUAL)., $(LANGALL)))
	-rm -f $(addsuffix .x01, $(addprefix $(MANUAL)., $(LANGALL)))
	-rm -f $(addsuffix .x02, $(addprefix $(MANUAL)., $(LANGALL)))
	-rm -f $(addsuffix .xml, $(addprefix $(MANUAL)., $(LANGALL)))
	-rm -f fuzzy.log

clean: doc-clean

#######################################################################
.PHONY: base-xml
base-xml: $(BASEXML)

#######################################################################
.PHONY: all-xml
all-xml: $(SRC_XML) $(ENT_ALL)

#######################################################################
.PHONY: doc-all
doc-all: css html txt epub

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

#######################################################################
# $ 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 css       # update CSS and DIMG in $(BASEDIR)
#######################################################################
.PHONY: css
css:
	-rm -rf $(BASEDIR)/html/images
	mkdir -p $(BASEDIR)/html/images
	cp -f $(XSLT_DIR)/debian.css $(BASEDIR)/html/debian.css
	echo "AddCharset UTF-8 .txt" > $(BASEDIR)/html/.htaccess
	cd png ; \
	cp caution.png home.png important.png next.png note.png \
		prev.png tip.png warning.png $(BASEDIR)/html/images

#######################################################################
# $ make html      # update all HTML in $(BASEDIR)
#######################################################################
.PHONY: html
html:	$(foreach LX, $(LANGALL), $(BASEDIR)/html/index.$(LX).html) fuzzy.log
	cat fuzzy.log

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

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

# style-txt.xsl provides work around for hidden URL links by appending them explicitly.
$(BASEDIR)/$(MANUAL).%.txt.gz: $(MANUAL).%.xml
	@$(call check-command, w3m, w3m)
	@$(call check-command, xsltproc, xsltproc)
	-mkdir -p $(BASEDIR)
	@test -n "`which w3m`"  || { echo "ERROR: w3m not found. Please install the w3m package." ; false ;  }
	$(XSLT) $(XSLT_DIR)/style-txt.xsl $< | LC_ALL=C.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 $(BASEDIR)
#######################################################################
.PHONY: pdf nopdf
pdf:	$(foreach LX, $(LANGALL), $(BASEDIR)/$(MANUAL).$(LX).pdf)

nopdf: $(TMPDIR)/nopdf.pdf

$(TMPDIR)/nopdf.pdf: nopdf.tex
	cp -f nopdf.tex $(TMPDIR)/nopdf.tex
	cd "$(TMPDIR)"; \
	xelatex nopdf.tex

$(foreach LX, $(NOPDF), $(BASEDIR)/$(MANUAL).$(LX).pdf): $(TMPDIR)/nopdf.pdf
	cp $(TMPDIR)/nopdf.pdf $@


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

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

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

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

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


#######################################################################
# $ make package
#######################################################################
.PHONY: package
# Use this target on devel branch source
package:
	git deborig -f HEAD
	pdebuild

