<?xml version="1.0"?>
<!--
NAnt build script for the TortoiseGit documentation

All project specific configuration is stored in doc.build.include.
All user/site specific configuration is stored in doc.build.user.

-->

<project name="Documentation" default="all" basedir=".">
	<description>
		Builds the Documentation and selected all output formats
	</description>

	<!-- default: we don't clean first -->
	<property name="cleanup" value="" overwrite="false" />

	<include buildfile="doc.build.include"/>

<!-- ====================================================================== -->
<!-- Loop over all applications and output formats                          -->
<!-- ====================================================================== -->

	<target name="all"
		description="Loop over all output formats and applications and build documentation"
		depends="prepare" >

		<foreach item="String" in="${applications}" delim="," property="app">
			<property name="doc.source.file" value="source/en/${app}.xml"/>
			<property name="doc.base.name" value="${app}_en"/>
			<property name="doc.base.work" value="output/${doc.base.name}"/>

			<property name="doc.target.name" value="${app}_en"/>
			<property name="doc.target.work" value="output/${doc.target.name}"/>
			<property name="doc.target.source" value="source/en/${app}/${app}.xml"/>

			<echo message="------------------------------------------------------------"/>
			<echo message="Creating '${doc.target.name}' documentation"/>

			<delete file="source/en/TortoiseGit/git_doc/git-doc.xml" if="${app=='TortoiseGit'}"/>
			<copy file="source/en/TortoiseGit/git_doc/git-doc.xml.in" tofile="source/en/TortoiseGit/git_doc/git-doc.xml" overwrite="true" if="${app=='TortoiseGit' and external.gitdocs=='0'}"/>

			<!-- update version info in version.xml -->
			<call target="UpdateVersionInfo"/>

			<call target="spellcheck" if="${property::exists('spellcheck') and spellcheck=='true'}"/>

			<property name="xslt.source" value="${doc.target.source}"/>
			<foreach item="String" in="${docformats}" delim="," property="docformat">
				<call target="copyimages"/>
				<call target="${docformat}"/>
			</foreach>
		</foreach>
	</target>

<!-- ====================================================================== -->
<!-- ====================================================================== -->

	<target name="clean" description="remove all generated files">
		<if test="${cleanup == 'yes'}">
			<delete dir="output" failonerror="false"/>
		</if>
	</target>

<!-- ====================================================================== -->
<!-- ====================================================================== -->

	<target name="prepare" description="Create directories" depends="clean">
		<mkdir dir="output"/>
		<call target="prepare.custom" if="${target::exists('prepare.custom')}"/>

		<property name="fontpath" value="/usr/share/fonts/truetype/" if="${platform::is-unix() and not property::exists('fontpath')}"/>
		<property name="fontpath" value="${string::replace(environment::get-variable('windir'), '\', '/')}/Fonts/" if="${platform::is-windows() and not property::exists('fontpath')}"/>
		<fail unless="${directory::exists(fontpath)}">fontpath '${fontpath}' does not exist</fail>

		<!-- ensure "Version" translation invariant -->
		<copy file="source/en/Version.in" tofile="source/en/version.xml" overwrite="true"/>

		<!-- tune path to xsltproc.exe in aspell.bat -->
		<copy file="Aspell/aspell.bat.in" tofile="Aspell/aspell.bat" overwrite="true" unless="${platform::is-unix()}">
			<filterchain>
				<replacetokens begintoken="$" endtoken="$">
					<token key="XSLTProcPath" value="${path.bin}" />
				</replacetokens>
			</filterchain>
		</copy>

	</target>

<!-- ====================================================================== -->
<!-- ====================================================================== -->

	<target name="copyimages" description="Copy images">
		<delete dir="${doc.target.work}/images" failonerror="false"/>
		<mkdir dir="${doc.target.work}/images"/>
		<mkdir dir="${doc.target.work}/images/callouts" if="${app=='TortoiseGit' and external.gitdocs=='0'}"/>

		<property name="xslt.params" value="${xsl.pdf.params}"/>
		<property name="xslt.target" value="${path::get-temp-file-name()}"/>
		<exec program="xsltproc"
					commandline="${xslt.params} --output ${xslt.target} images/extract-images.xsl ${xslt.source}"
					basedir="${path.bin}"
					verbose="false"/>
		<foreach item="Line" in="${xslt.target}" property="filename" trim="Both">
			<property name="destname" value="${doc.target.work}/images/${filename}"/>
			<copy file="images/en/${filename}" tofile="${destname}" if="${file::exists(path::combine('images', path::combine('en', filename)))}"/>
			<copy file="images/en/${filename}" tofile="${destname}" unless="${file::exists(destname)}"/>
		</foreach>

		<copy todir="${doc.target.work}/images">
			<fileset basedir="images/en">
				<include name="caution.png"/>
				<include name="warning.png"/>
				<include name="important.png"/>
				<include name="tip.png"/>
				<include name="note.png"/>
				<include name="link.png"/>
			</fileset>
		</copy>

		<copy todir="${doc.target.work}/images/callouts" if="${app=='TortoiseGit' and external.gitdocs=='0'}">
			<fileset basedir="images/en/callouts">
				<include name="*.svg"/>
			</fileset>
		</copy>
	</target>

<!-- ====================================================================== -->
<!-- ====================================================================== -->

	<target name="uptodate" description="Check whether the given target is up to date">

		<echo message="Checking if '${file.target}' is up to date"/>
		<uptodate property="file.target.uptodate">
			<sourcefiles>
				<include name="source/en/${app}/**/*.xml"/>
				<include name="source/en/glossary.xml"/>
				<include name="source/en/wishlist.xml"/>
				<include name="source/en/version.in"/>
			</sourcefiles>
			<targetfiles>
				<include name="${file.target}"/>
			</targetfiles>
		</uptodate>
		<if test="${file.target.uptodate}">
			<echo message="Up to date"/>
		</if>
	</target>

<!-- ====================================================================== -->
<!-- Run xsltproc with the defined parameters                               -->
<!-- ====================================================================== -->

	<target name="xsltproc" description="Run xsltproc with the defined parameters">
		<echo message="Processing XML Source" />

		<!-- Check whether a language specific xsl file exists and use it -->
		<!-- if none is found, load XSL file from default location.       -->
		<property name="xslt.file.lang" value="${path.user.xsl}/en/${xslt.file}" />
		<property name="xslt.file" value="${xslt.file.lang}" if="${file::exists(xslt.file.lang)}"/>
		<property name="xslt.file" value="${path.user.xsl}/${xslt.file}" unless="${file::exists(xslt.file.lang)}"/>

		<echo message="Stylesheet: ${xslt.file}" />
		<echo message="Parameters: ${xslt.params}" />
		<property name="xslt.output.file" value="${path::get-temp-file-name()}"/>
		<exec program="xsltproc"
					commandline="${xslt.params} --output ${xslt.target} ${xslt.file} ${xslt.source}"
					basedir="${path.bin}"
					output="${xslt.output.file}"
					verbose="false"/>
		<loadfile file="${xslt.output.file}" property="xsltproc.output" />
		<fail if="${string::contains(xsltproc.output, ': parser error :')}">"Parser error" found</fail>
		<fail if="${string::contains(xsltproc.output, 'Error: no ID for constraint linkend:')}">"Error: no ID for constraint linkend:" found</fail>
		<fail if="${string::contains(xsltproc.output, 'ERROR: xref linking to ')}">"ERROR: xref linking to" found</fail>
		<fail if="${string::contains(xsltproc.output, ', but no template matches.')}">unknown element found (look for "Element X encountered in Y, but no template matches.")</fail>
	</target>

<!-- ====================================================================== -->
<!-- Try to update version info in the version file                         -->
<!-- ====================================================================== -->

	<target name="UpdateVersionInfo" description="Updates the version file with version info">

		<copy
			file="source/en/Version.in"
			tofile="source/en/version.xml"
			overwrite="true">

			<filterchain>
				<replacetokens begintoken="$" endtoken="$">
					<token key="MajorVersion" value="${environment::get-variable('MajorVersion')}"/>
					<token key="MinorVersion" value="${environment::get-variable('MinorVersion')}"/>
					<token key="MicroVersion" value="${environment::get-variable('MicroVersion')}"/>
				</replacetokens>
			</filterchain>
		</copy>

	</target>

<!-- ====================================================================== -->
<!-- Run spellchecker on files                                              -->
<!-- ====================================================================== -->

	<target name="spellcheck" description="Run spellchecker on files">
		<echo message="Spellchecking: '${app} en' This may take a few minutes"/>
		<property name="spellcheck.log" value="Aspell/${app}_en.log"/>
		<property name="script.extension" value=".bat" if="${platform::is-windows()}"/>
		<property name="script.extension" value=".sh" if="${platform::is-unix()}"/>
		<mkdir dir="Aspell/${app}_en"/>

		<delete dir="${spellcheck.log}" failonerror="false"/>

		<copy file="Aspell/TortoiseGit.tmpl.pws" tofile="Aspell/Temp.pws" overwrite="true" inputencoding="utf-8" outputencoding="utf-8">
			<filterchain>
				<replacetokens begintoken="$" endtoken="$">
					<token key="LANG" value="en" />
				</replacetokens>
			</filterchain>
		</copy>
		<exec program="sed" commandline=" -i '1s/^\xEF\xBB\xBF//' ./Aspell/Temp.pws" if="${platform::is-unix()}"/> <!-- outputencoding="utf-8" inserts BOM, aspell doesn't like that -->

		<foreach item="File" property="file.base">
			<in>
				<items>
					<exclude name="source/en/TortoiseGit/git_doc/*.xml"/>
					<include name="source/en/${app}/**/*.xml"/>
					<include name="source/en/glossary.xml"/>
					<include name="source/en/wishlist.xml"/>
				</items>
			</in>
			<do>
				<property name="file.target" value="${file.base}"/>
				<property name="file.log" value="Aspell/${app}_en/${path::get-file-name(file.target)}.log"/>

				<uptodate property="file.target.uptodate">
					<sourcefiles>
						<include name="${file.target}"/>
						<include name="Aspell/en.pws"/>
						<include name="Aspell/TortoiseGit.tmpl.pws"/>
					</sourcefiles>
					<targetfiles>
						<include name="${file.log}"/>
					</targetfiles>
				</uptodate>

				<if test="${not file.target.uptodate}">
					<echo message="Checking: ${path::get-file-name(file.target)}"/>
					<exec program="Aspell/aspell${script.extension}"
						commandline="&quot;${path.spellcheck}&quot; &quot;en&quot; &quot;${file.target}&quot; &quot;${file.log}&quot;"
						failonerror="false"/>
				</if>
				<exec program="Aspell/append${script.extension}"
					commandline="${file.log} ${spellcheck.log}"/>
			</do>
		</foreach>
	</target>

<!-- ====================================================================== -->
<!-- Create PDF documentation                                               -->
<!-- ====================================================================== -->

	<target name="pdf" description="Create PDF documentation">

		<property name="file.target" value="output/${app}-${docverstring}-en.pdf"/>
		<call target="uptodate"/>

		<if test="${not file.target.uptodate}">
			<property name="xslt.params" value="${xsl.pdf.params}"/>
			<property name="xslt.file" value="${xsl.pdf.file}"/>
			<property name="xslt.final" value="${doc.target.work}/${doc.target.name}"/>
			<!-- set xslt.target to the name of the temp file -->
			<property name="xslt.target" value="${xslt.final}.tmp"/>

			<call target="xsltproc" />

			<!-- Removes the span="inherit" attribute added by xsltproc, which causes a warning -->
			<move file="${xslt.target}" tofile="${xslt.final}.fo" overwrite="true" inputencoding="utf-8" outputencoding="utf-8">
				<filterchain>
					<replacestring from="span=&quot;inherit&quot; " to="" />
					<replacestring from="width=&quot;auto&quot; height=&quot;auto&quot; content-width=&quot;auto&quot; content-height=&quot;auto&quot;" to="inline-progression-dimension.maximum=&quot;100%&quot; content-width=&quot;scale-down-to-fit&quot; content-height=&quot;scale-down-to-fit&quot;" />
				</filterchain>
			</move>

			<property name="userconfig.langtemplate" value="${path.user.xsl}/en/userconfig.template.xml" />
			<property name="userconfig.lang" value="${path.user.xsl}/en/userconfig.xml" />

			<if test="${file::exists(userconfig.langtemplate)}">
				<loadfile file="${userconfig.langtemplate}" property="configfile">
					<filterchain>
						<replacetokens begintoken="%" endtoken="%">
							<token key="FONTSDIR" value="${fontpath}" />
						</replacetokens>
					</filterchain>
				</loadfile>
				<echo file="${userconfig.lang}" message="${configfile}" />
			</if>
			<property name="commandline" value="-q -fo ${xslt.final}.fo -pdf ${file.target}"/>
			<property name="commandline" value="-c ${userconfig.lang} ${commandline}" if="${file::exists(userconfig.lang)}"/>

			<exec program="${name.fop}"
						commandline="${commandline}"
						basedir="${path.fop}"
						verbose="false"/>
		</if>
	</target>

<!-- ====================================================================== -->
<!-- Create HTML multi page documentation                                   -->
<!-- ====================================================================== -->
	<target name="html" description="Create HTML documentation" depends="">

		<property name="file.target" value="${doc.target.work}/index.html"/>
		<call target="uptodate"/>

		<if test="${not file.target.uptodate}">
			<property name="xslt.params" value="${xsl.htmlchunk.params}"/>
			<property name="xslt.file" value="${xsl.htmlchunk.file}"/>
			<property name="xslt.target" value="${doc.target.work}/${doc.target.name}"/>

			<copy file="${path.user.css}/styles_html.css" todir="${doc.target.work}"/>

			<call target="xsltproc" />
		</if>
		<if test="${help.mapping=='1'}">
			<call target="helpmapping"/>
			<property name="help.wixfilelist" value="../src/TortoiseGitSetup/HTMLHelpfiles.wxi" unless="${property::exists('help.wixfilelist')}"/>
			<exec program="${name.python}"
						commandline="scripts/generate_wix_filelist.py"
						output="${help.wixfilelist}"
						verbose="false"/>
			<exec program="sed" commandline=" -i 's/$/\r/' ${help.wixfilelist}" if="${platform::is-unix()}"/>
			<echo message="Updated help WiX filelist: '${help.wixfilelist}'"/>
		</if>
	</target>

<!-- ====================================================================== -->
<!-- Create mapping files for TortoiseGit online help                       -->
<!-- ====================================================================== -->

	<target name="helpmapping" description="Create help mapping for IDs to filenames">
		<call target="help.prepare.${app}" />
		<exec program="${name.python}"
					commandline="scripts/create_html_mapping.py ${help.resource} ${doc.target.work}/alias.h"
					output="${help.mappingfile}"
					verbose="false"/>
		<exec program="sed" commandline=" -i 's/$/\r/' ${help.mappingfile}" if="${platform::is-unix()}"/>
		<echo message="Updated help mapping: '${help.mappingfile}'"/>
	</target>

</project>
