<?xml version="1.0"?>
<!--
============================================================ 
 $Date: 2008-12-14 09:06:16 +0800 (Sun, 14 Dec 2008) $
 $Author: simonlarge $
 $Rev: 14835 $
============================================================ 

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 in all languages and all output formats
	</description>
	
	<!-- default: we don't clean first -->
	<property name="cleanup" value="" overwrite="false" />

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

<!-- ====================================================================== -->
<!-- Loop over all languages                                                -->
<!-- ====================================================================== -->

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

		<foreach item="String" in="${languages}" delim="," property="lang.target">
			<!-- language code for English (US) (default)-->
			<property name="lang.target.code" value="1033" />
			<property name="languagebuildfile" value="${path.user.xsl}/${lang.target}/lang.build" />
			<if test="${file::exists(languagebuildfile)}" >
				<include buildfile="${languagebuildfile}" />
			</if>
			<call target="doit" failonerror="${critical or lang.base==lang.target}"/>
		</foreach>
	</target>
	
<!-- ====================================================================== -->
<!-- Loop over all applications and output formats                          -->
<!-- ====================================================================== -->

	<target name="doit">

		<foreach item="String" in="${applications}" delim="," property="app">
			<property name="doc.source.file" value="source/${lang.base}/${app}.xml"/>
			<property name="doc.base.name" value="${app}_${lang.base}"/>
			<property name="doc.base.work" value="output/${doc.base.name}"/>
					
			<property name="doc.target.name" value="${app}_${lang.target}"/>
			<property name="doc.target.work" value="output/${doc.target.name}"/>
			<property name="doc.target.source" value="source/${lang.target}/${app}/${app}.xml"/>
			<property name="pofiletotranslate" value="po/${app}_${lang.target}.po" />

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

			<if test="${file::exists(pofiletotranslate)}" >
				<call target="translate" unless="${lang.base==lang.target}" />
			</if>
			<if test="${lang.base==lang.target or file::exists(pofiletotranslate)}" >
				<!-- update version info in version.xml -->
				<call target="UpdateVersionInfo"/>

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

				<property name="xslt.source" value="${doc.target.source}"/>
				<foreach item="String" in="${docformats}" delim="," property="docformat">
					<call target="${docformat}"/>
				</foreach>
			</if>
		</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')}"/>

		<!-- find the path to the fonts -->
		<script language="C#">			
			<code>
				<![CDATA[
					public static void ScriptMain(Project project)
					{
						string systemPath = Environment.GetFolderPath(Environment.SpecialFolder.System);
						string fontsPath = Path.GetDirectoryName(systemPath) + Path.DirectorySeparatorChar + "FONTS";
						fontsPath = fontsPath.Replace('\\','/');
						project.Properties.Add("fontpath", fontsPath );
					}
				]]>
			</code>
		</script>

		<!-- ensure "Version" translation invariant -->
		<copy file="source\${lang.base}\version.in" tofile="source\${lang.base}\version.xml" overwrite="true"/>

		<!-- tune path to xsltproc.exe in aspell.bat -->
		<copy file="Aspell/aspell.in" tofile="Aspell/aspell.bat" overwrite="true">
			<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"/>
		<copy todir="${doc.target.work}/images">
			<fileset basedir="images/${lang.base}">
				<include name="*"/>
			</fileset>
		</copy>

		<copy todir="${doc.target.work}/images" unless="${lang.base==lang.target}" overwrite="true">
			<fileset basedir="images/${lang.target}">
				<include name="*"/>
			</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/${lang.target}/${app}/**/*.xml"/>
				<include name="source/${lang.target}/pubdate.xml"/>
				<include name="source/${lang.target}/glossary.xml"/>
				<include name="source/${lang.target}/wishlist.xml"/>
				<include name="source/${lang.target}/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}/${lang.target}/${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}" />
		<exec program="xsltproc"
					commandline="${xslt.params} --output ${xslt.target} ${xslt.file} ${xslt.source}"
					basedir="${path.bin}"
					verbose="false"/>
	</target>
	
<!-- ====================================================================== -->
<!-- Profile source into a single file                                      -->
<!-- ====================================================================== -->

	<target name="profile" description="Profile source into a single file" >

		<call target="uptodate"/>

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

			<echo message="Profiling into: ${file.target}"/>
			<call target="xsltproc"/>

			<!-- Removes the moreinfo="" attributes added by xsltproc -->
			<move file="${xslt.target}" tofile="${file.target}" overwrite="true">
				<filterchain>
					<replacestring from=" moreinfo=&quot;none&quot;" to="" />
				</filterchain>
			</move>
		</if>
	</target>

<!-- ====================================================================== -->
<!-- Create po template for each application from sources                   -->
<!-- ====================================================================== -->

	<target name="potfile" description="Create po template from sources">
		<property name="lang.target" value="${lang.base}"/>
		
		<!-- ensure "Version" translation invariant -->
		<copy file="source\${lang.base}\version.in" tofile="source\${lang.base}\version.xml" overwrite="true"/>

		<foreach item="String" in="${potemplates}" delim="," property="app">

			<property name="potfile" value="po/${app}.pot" />
			<property name="xslt.source" value="source/${lang.base}/${app}/${app}.xml"/>
			<property name="file.target" value="source/${app}_${lang.base}.xml"/>

			<echo message="------------------------------------------------------------"/>
			<echo message="Checking if ${potfile} is up to date"/>

			<call target="profile" />

			<if test="${not file::up-to-date(file.target, potfile)}">
				<echo message="Creating po template from sources"/>
				<exec program="python"
							commandline="xml2po.py -e -o ${potfile}.tmp ${file.target}"
							verbose="false"/>

				<!-- Adjust path to source file -->
				<move file="${potfile}.tmp" tofile="${potfile}" overwrite="true">
					<filterchain>
						<replacestring from="#: ${file.target}" to="#: ../${file.target}" />
					</filterchain>
				</move>
			</if>

			<echo message="" />
		</foreach>
	</target>

<!-- ====================================================================== -->
<!-- Create translated source files                                         -->
<!-- ====================================================================== -->

	<target name="translate" description="Create translated source file">
		<property name="lang.pofile" value="po/${app}_${lang.target}.po"/>

		<echo message="Translating: '${app} ${lang.target}' This may take a few minutes"/>
		
		<exec program="msgfmt"
					commandline="${lang.pofile} -o ${lang.target}.mo"
					basedir="${path.gettext}"
					verbose="false"/>

		<foreach item="File" property="file.base">
			<in>
				<items>
					<include name="source/${lang.base}/${app}/**/*.xml"/>
					<include name="source/${lang.base}/pubdate.xml"/>
					<include name="source/${lang.base}/glossary.xml"/>
					<include name="source/${lang.base}/wishlist.xml"/>
					<include name="source/${lang.base}/version.in"/>
				</items>
			</in>
			<do>
				<property name="file.target" value="${string::replace(file.base,'\'+lang.base+'\','\'+lang.target+'\')}"/>

				<uptodate property="file.target.uptodate">
					<sourcefiles>
						<include name="${file.base}"/>
						<include name="${lang.pofile}"/>
					</sourcefiles>
					<targetfiles>
						<include name="${file.target}"/>
					</targetfiles>
				</uptodate>

				<if test="${not file.target.uptodate}">

					<echo message="Translating: ${path::get-file-name(file.target)}"/>
					<property name="dir.target" value="${path::get-directory-name(file.target)}"/>
					<mkdir dir="${dir.target}" unless="${directory::exists(dir.target)}"/>
					<exec program="python"
								commandline='xml2po.py -t "${lang.target}.mo" -o "${file.target}" "${file.base}"'
								verbose="false"/>
				</if>

			</do>
		</foreach>

<!--
		<foreach item="File" property="file.base">
			<in>
				<items refid="files.exclude" />
			</in>
			<do>
				<property name="file.target" value="${string::replace(file.base,'\'+lang.base+'\','\'+lang.target+'\')}"/>
				<copy file="${file.base}" tofile="${file.target}" overwrite="true"/>
			</do>
		</foreach>
-->

		<delete file="${lang.target}.mo"/>
	</target>

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

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

		<copy
			file="source\${lang.target}\version.in" 
			tofile="source\${lang.target}\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')}"/>
<!--
					<token key="WCREV" value="${environment::get-variable('WCREV')}"/>
-->
				</replacetokens>
			</filterchain>
		</copy>

	</target>

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

	<target name="spellcheck" description="Run spellchecker on files">
		<echo message="Spellchecking: '${app} ${lang.target}' This may take a few minutes"/>
		<property name="spellcheck.log" value="ASpell/${app}_${lang.target}.log"/>
		<mkdir dir="ASpell/${app}_${lang.target}"/>
		
		<echo message="--- Spellchecking: '${lang.target}'"
			file="${spellcheck.log}"
			failonerror="false"/>

		<copy file="Aspell/TortoiseGit.tmpl.pws" tofile="Aspell/Temp.pws" overwrite="true">
			<filterchain>
				<replacetokens begintoken="$" endtoken="$">
					<token key="LANG" value="${lang.target}" />
				</replacetokens>
			</filterchain>
		</copy>

		<foreach item="File" property="file.base">
			<in>
				<items>
					<include name="source/${lang.base}/${app}/**/*.xml"/>
					<include name="source/${lang.base}/glossary.xml"/>
					<include name="source/${lang.base}/wishlist.xml"/>
				</items>
			</in>
			<do>
				<property name="file.target" value="${string::replace(file.base,'\'+lang.base+'\','\'+lang.target+'\')}"/>
				<property name="file.log" value="ASpell\${app}_${lang.target}\${path::get-file-name(file.target)}.log"/>

				<uptodate property="file.target.uptodate">
					<sourcefiles>
						<include name="${file.target}"/>
						<include name="ASpell/${lang.target}.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.bat"
						commandline="${path.spellcheck} ${lang.target} ${file.target} ${file.log}"
						failonerror="false"/>
				</if>
				<exec program="Aspell/append.bat"
					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}-${lang.target}.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.target" value="${doc.target.work}/${doc.target.name}.fo"/>

			<call target="xsltproc" />

			<property name="userconfig.langtemplate" value="${path.user.xsl}/${lang.target}/userconfig.template.xml" />
			<property name="userconfig.lang" value="${path.user.xsl}/${lang.target}/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.target} -pdf ${file.target}"/>
			<property name="commandline" value="-c ${userconfig.lang} ${commandline}" if="${file::exists(userconfig.lang)}"/>
			
			<exec program="fop.bat"
						commandline="${commandline}"
						basedir="${path.fop}"
						verbose="false"/>
		</if>
	</target>

<!-- ====================================================================== -->
<!-- Create CHM documentation                                               -->
<!-- ====================================================================== -->

	<target name="chm" description="Create CHM documentation">
	
		<property name="file.target" value="${doc.target.work}.chm"/>
		<call target="uptodate"/>

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

			<call target="xsltproc" />
			
			<!-- The CHM helpcontext target resides in an external build file -->
			<call target="help.prepare" if="${target::exists('help.prepare')}"/>

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

			<exec program="SBAppLocale.exe"
						commandline="${lang.target.code} ${path.bin}/hhc.exe ${doc.target.work}/htmlhelp.hhp"
						basedir="${path.bin}"
						resultproperty="result.hhc"
						failonerror="false"
						verbose="false"
						timeout="600000" />

			<copy file="${doc.target.work}/htmlhelp.chm" tofile="${file.target}"/>
		</if>
		<!--
		<delete>
			<fileset>
				<include name="${doc.target.work}/*.*"/>
			</fileset>
		</delete>
		-->
	</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.htmlsingle.params}"/>
			<property name="xslt.file" value="${xsl.htmlsingle.file}"/>
			<property name="xslt.target" value="${doc.target.work}/${doc.target.name}"/>

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

			<call target="xsltproc" />

		</if>
	</target>

<!-- ====================================================================== -->
<!-- Create HTML single page documentation                                  -->
<!-- ====================================================================== -->

	<target name="htmlsingle" description="Create HTML single page documentation" depends="">

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

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

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

			<call target="xsltproc" />

		</if>
	</target>

<!-- ====================================================================== -->
<!-- Create the API documentation                                           -->
<!-- ====================================================================== -->

	<target name="apidoc" depends="GitAPIdoc,TortoiseGitAPIdoc" />

	<target name="GitAPIdoc">
		<loadfile file="API\DoxyfileSVN" property="GitDoxyFile" />
		<echo file="Doxyfile" message="${GitDoxyFile}" />
		<echo file="Doxyfile" message="HHC_LOCATION=${environment::get-variable('HHLOC')}" append="true" />
		<mkdir dir="output" />
		<exec program="doxygen.exe">
			<arg value="Doxyfile" />
		</exec>
		<move file="output\doxygen\html\GitAPI.chm" tofile="output\GitAPI.chm" overwrite="true" />
		<delete dir="output\doxygen" />
		<delete file="Doxyfile" />
	</target>

	<target name="TortoiseGitAPIdoc">
		<loadfile file="API\Doxyfile" property="TortoiseGitDoxyFile" />
		<echo file="Doxyfile" message="${TortoiseGitDoxyFile}" />
		<echo file="Doxyfile" message="HHC_LOCATION=${environment::get-variable('HHLOC')}" append="true" />
		<mkdir dir="output" />
		<exec program="doxygen.exe">
			<arg value="Doxyfile" />
		</exec>
		<move file="output\doxygen\html\TortoiseAPI.chm" tofile="output\TortoiseGitAPI.chm" overwrite="true" />
		<delete dir="output\doxygen" />
		<delete file="Doxyfile" />
	</target>

</project>


