<?xml version="1.0" encoding="utf-8"?>
<project basedir="." default="dist" name="teidocx">
<!--
	This library is free software; you can redistribute it and/or
	modify it under the terms of the GNU Lesser General Public
	License as published by the Free Software Foundation; either
	version 2.1 of the License, or (at your option) any later version.
	
	This library is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
	Lesser General Public License for more details.
	
	You should have received a copy of the GNU Lesser General Public
	License along with this library; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
	
        Id: $Id: build-to.xml 9875 2011-12-04 18:27:43Z rahtz $
        Copyright: 2011, TEI Consortium<
-->
  <!--
 Convert TEI documents into docx files.
 -->
  <!-- Path to TEI XML file. -->
  <property name="inputFile" value=""/>
  <!-- Path where the output docx file will be saved. -->
  <property name="outputFile" value=""/>
  <!-- which profile directory-->
  <property name="profileDir" value="../profiles"/>
  <!-- which profile -->
  <property name="profile" value="default"/>
  <!-- which language -->
  <property name="lang" value="en"/>
  <!-- where does oxygen keep stuff -->
  <property name="oxygenlib" value=""/>
  <!-- Stylesheet for transformation -->
  <property name="teitoDocx"       value="${profileDir}/${profile}/docx/to.xsl"/>
  <property name="listgraphics"    value="../tools/makegraphicsanttask.xsl"/>
  <dirname property="xslDir"       file="${teitoDocx}"/>
  <dirname property="inputFileDir" file="${inputFile}"/>
  <dirname property="outputDir"    file="${outputFile}"/>
  <property name="outputTempDir"   value="${outputDir}/temp-dir-for-ant"/>

  <path id="classpath">
    <pathelement path="${java.class.path}"/>
  </path>

  <condition property="makecoverpages">
    <available file="${profileDir}/${profile}/docx/makecoverpages.xsl"/>
  </condition>
  <import file="../odds2/build-to.xml"/>
  <target name="setup">
    <!-- Recreate temporary output directory -->
    <taskdef name="images" classname="ImageTask" classpath="../tools/ImageInfo/ImageInfo.jar"/>
    <delete dir="${outputTempDir}"/>
    <mkdir  dir="${outputTempDir}"/>
  </target>

  <target name="nocoverpages" unless="makecoverpages">
    <move file="${outputTempDir}/newdocument.xml" tofile="${outputTempDir}/word/document.xml" overwrite="true"/>
  </target>

  <target name="coverpages" if="makecoverpages">
    <echo>Make cover pages</echo>
    <xslt
	  style="${profileDir}/${profile}/docx/makecoverpages.xsl" 
	  in="${outputTempDir}/word/document.xml" 
	  out="${outputTempDir}/tmp3.xml" 
	  classpath="classpath">
      <xmlcatalog>
	<catalogpath>
	  <pathelement location="../../../../catalog.xml"/>
	  <pathelement location="../../../../../xhtml11/dtd/xhtmlcatalog.xml"/>
	</catalogpath>
      </xmlcatalog>
      <factory name="net.sf.saxon.TransformerFactoryImpl"/>
      <classpath location="${oxygenlib}/saxon9he.jar"/>
      <param name="headerFile"   expression="file:///${outputTempDir}/tmp2.xml"/>
      <param name="documentFile" expression="file:///${outputTempDir}/newdocument.xml"/>
    </xslt>
    <move file="${outputTempDir}/tmp3.xml" tofile="${outputTempDir}/word/document.xml" overwrite="true"/>
    <delete file="${outputTempDir}/newdocument.xml"/>
  </target>



  <target name="transform">
    <!-- The transformation for DOCX output. -->
    <echo level="info">unzip Word template</echo>
    <unzip src="${xslDir}/template.docx" dest="${outputTempDir}"/>
    <echo level="info">Get names of graphics files and write an ant task</echo>
    <xslt force="yes" style="${listgraphics}" classpath="classpath" in="${outputTempDir}/tmp2.xml" out="${outputTempDir}/copy.xml">
      <xmlcatalog>
        <catalogpath>
          <pathelement location="../../../../catalog.xml"/>
          <pathelement location="../../../../../xhtml11/dtd/xhtmlcatalog.xml"/>
        </catalogpath>
      </xmlcatalog>
      <factory name="net.sf.saxon.TransformerFactoryImpl"/>
      <classpath location="${oxygenlib}/saxon9ee.jar"/>
      <param name="inputDir" expression="${inputFileDir}"/>
      <param name="mediaDir" expression="word/media"/>
    </xslt>
    <echo level="info">Copy image files by running ant task</echo>
    <ant antfile="${outputTempDir}/copy.xml"/>
    <echo level="info">Read dimensions of image files and write info file</echo>
    <images outputFile="${outputTempDir}/image-size-info.xml">
      <fileset dir="${outputTempDir}/word" includes="media/*.*"/>
    </images>
    <echo level="info">XSLT generate files for docx </echo>
    <xslt force="yes" style="${teitoDocx}" in="${outputTempDir}/tmp2.xml" out="${outputTempDir}/newdocument.xml" classpath="classpath">
      <xmlcatalog>
        <catalogpath>
          <pathelement location="../../../../catalog.xml"/>
          <pathelement location="../../../../../xhtml11/dtd/xhtmlcatalog.xml"/>
        </catalogpath>
      </xmlcatalog>
      <factory name="net.sf.saxon.TransformerFactoryImpl"/>
      <classpath location="${oxygenlib}/saxon9ee.jar"/>
      <param name="word-directory" expression="file:///${outputTempDir}"/>
      <param name="inputDir" expression="${inputFileDir}"/>
      <!-- supply name of language-->
      <param name="lang" expression="${lang}" if="lang"/>
      <!-- emit some diagnostics"-->
      <param name="debug" expression="${debug}" if="debug"/>
      <!-- be verbose-->
      <param name="verbose" expression="${verbose}" if="verbose"/>
    </xslt>
    <move file="${outputTempDir}/docProps/newcore.xml" tofile="${outputTempDir}/docProps/core.xml" overwrite="true"/>
    <move file="${outputTempDir}/docProps/newcustom.xml" tofile="${outputTempDir}/docProps/custom.xml" overwrite="true"/>
  </target>

  <target name="zip">
    <echo level="info">ZIP: put files from ${outputTempDir} together into ${outputFile}</echo>
    <zip destfile="${outputDir}/temp-for-ant.zip" basedir="${outputTempDir}" compress="true" excludes="tmp1.xml,tmp2.xml,image-size-info.xml,copy.xml"/>
    <move file="${outputDir}/temp-for-ant.zip" tofile="${outputFile}" overwrite="true"/>
  </target>

  <target name="cleanup" unless="debug">
    <delete dir="${outputTempDir}"/>
  </target>

  <target name="dist"
	  depends="setup,odd,notodd,transform,nocoverpages,coverpages,zip,cleanup"/>


</project>
