SHELL = /bin/bash

WWWBASE=freedocs.mi.hdm-stuttgart.de:/home/info1/doc
SCRIPTBASENAME=lectures

#
# Eclipse Help XSL related plugin parameters, see
# http://docbook.sourceforge.net/release/xsl/current/doc/param.html
#

PLUGINID=de.hdm_stuttgart.mi.lectures

XSLPARAM=-p callout.graphics 0 \
-p callout.unicode 1

XSLPARAM_html=-p use.extensions 1 \
 -p eclipse.plugin.name Lectures  \
 -p eclipse.plugin.id $(PLUGINID)  \
 -p eclipse.plugin.provider 'Martin Goik, goik@hdm-stuttgart.de'

XSLPARAM_pdf=-p xep.extensions 1\
 -p highlight.source 1 

DOCMODULES=Sd1 Sda1 Sda2 Sdi

BUILDROOT=target

#+++ no changes below this line ++++++++++++++++++++++++++++++++++++

numCores=$(shell nproc --all)

SAXONCMD=java -cp "/usr/share/java/Saxon-HE.jar:/usr/share/java/xercesImpl.jar" \
-Djavax.xml.parsers.DocumentBuilderFactory=org.apache.xerces.jaxp.DocumentBuilderFactoryImpl \
    -Djavax.xml.parsers.SAXParserFactory=org.apache.xerces.jaxp.SAXParserFactoryImpl \
    -Dorg.apache.xerces.xni.parser.XMLParserConfiguration=org.apache.xerces.parsers.XIncludeParserConfiguration \
   net.sf.saxon.Transform 

#
# Dependent destinations
#
BUILDBASE=$(BUILDROOT)/$(SCRIPTBASENAME)
BUILDNAVI=navi/$(PLUGINID)
BUILDNONAVI=nonavi/$(PLUGINID)
JAVADOCDESTDIR=${BUILDNAVI}/Ref/api

#Video webm-->mp4 conversion rule
#
%.webm.mp4:%.webm
	avconv -i $< -acodec libx264  -c:a libvorbis -r 10 $<.mp4 || rm -f $<.mp4
	avconv -i $< -vframes 1 $<.png || rm -f $<.png

webmSet = $(shell /bin/ls target/*/Ref/Video/*.webm)
mp4Set  = $(foreach webmFile, $(webmSet), $(webmFile).mp4 )

videoformats:$(mp4Set)

all: ${BUILDROOT}/stage1
	cd ${BUILDROOT}; tar c- ${DOCMODULES} | tar -x -C webhelp
	cd ${BUILDROOT};zip -r webhelp/webhelp.zip webhelp -x webhelp/webhelp.zip
#	cd ${BUILDROOT}; tar c- ${DOCMODULES} | tar -x -C ${BUILDNAVI} 

${BUILDROOT}/${SCRIPTBASENAME}_print.xml \
${BUILDROOT}/${SCRIPTBASENAME}.xml: ${SCRIPTBASENAME}.xml Common/glossary.xml Common/bibliography.xml
	mkdir -p ${BUILDROOT};
	${SAXONCMD} -s:${SCRIPTBASENAME}.xml -xsl:../ws/Docbook/Extensions/Xsl/docbook2docbook.xsl >  ${BUILDROOT}/${SCRIPTBASENAME}.xml
	${SAXONCMD} -s:${BUILDROOT}/${SCRIPTBASENAME}.xml -xsl:../ws/Docbook/Extensions/Xsl/docbook2print.xsl >  ${BUILDROOT}/${SCRIPTBASENAME}_print.xml
	cd ${BUILDROOT}; ${SAXONCMD} -s:${SCRIPTBASENAME}.xml -xsl:../../ws/Docbook/Extensions/Xsl/docbookExtractFig.xsl 
	for module in ${DOCMODULES}; do \
            tar c- $$module/Ref| tar -x -C ${BUILDROOT}; \
          done;
	$(MAKE) $(mp4Set)
	cd ${BUILDROOT}; $(MAKE) -f ../make.rules -j ${numCores} pdf

${BUILDROOT}/stage1:\
 ${BUILDROOT}/webhelp/index.html \
 ${BUILDROOT}/webhelp/lectures.epub \
 ${BUILDROOT}/webhelp/printversion.pdf \
 ${BUILDROOT}/mobil/index.html
	touch ${BUILDROOT}/stage1

${BUILDROOT}/webhelp/index.html:${BUILDROOT}/${SCRIPTBASENAME}.xml
	cd  ${BUILDROOT};\
	/usr/share/xmlmind/bin/xxetool convert ${XSLPARAM} db5.toWebHelp \
            ${SCRIPTBASENAME}.xml -u webhelp 

${BUILDROOT}/mobil/index.html:${BUILDROOT}/${SCRIPTBASENAME}.xml
	cd  ${BUILDROOT};\
          /usr/share/xmlmind/bin/xxetool convert ${XSLPARAM} db5.toEclipseHelp \
           ${SCRIPTBASENAME}.xml -u mobil
	cd  ${BUILDROOT}/mobil;ln -sf /home/info1/doc/navi/de.hdm_stuttgart.mi.lectures/P .

${BUILDROOT}/webhelp/printversion.pdf: ${BUILDROOT}/${SCRIPTBASENAME}_print.xml
	cd ${BUILDROOT};\
          /usr/share/xmlmind/bin/xxetool convert ${XSLPARAM} ${XSLPARAM_pdf} db5.toPSFile \
          ${SCRIPTBASENAME}_print.xml -s pdf -s "|pdf" -u  webhelp/printversion.pdf

${BUILDROOT}/webhelp/lectures.epub: ${BUILDROOT}/${SCRIPTBASENAME}.xml
	cd ${BUILDROOT};\
          /usr/share/xmlmind/bin/xxetool convert ${XSLPARAM} \
          db5.toEpub  ${SCRIPTBASENAME}.xml -u  webhelp/lectures.epub

#${BUILDROOT}/${BUILDNAVI}/index.html: ${BUILDROOT}/${SCRIPTBASENAME}.xml
#        cd  ${BUILDROOT};\
#          /usr/share/xmlmind/bin/xxetool convert ${XSLPARAM} ${XSLPARAM_html} db5.toEclipseHelp \
#           ${SCRIPTBASENAME}.xml -u $(BUILDNAVI)
#       perl -i -p -e '$$/=undef;s/\n[ \t]*<!DOCTYPE[^>]+\n[^>]*>$$//m' ${BUILDNAVI}/*.xml
#       cp -r META-INF ${BUILDROOT}/${BUILDNAVI} 

#${BUILDROOT}/${BUILDNONAVI}/index.html: ${BUILDROOT}/${SCRIPTBASENAME}.xml
#        cd  ${BUILDROOT};\
#          /usr/share/xmlmind/bin/xxetool convert -p suppress.navigation 1 ${XSLPARAM} ${XSLPARAM_html} \
#          db5.toEclipseHelp ${SCRIPTBASENAME}.xml -u $(BUILDNONAVI);

clean:
	rm -rf ${BUILDROOT}
#end