<?xml version="1.0"?>
<!-- This file is part of the DITA Open Toolkit project hosted on 
     Sourceforge.net. See the accompanying license.txt file for 
     applicable licenses.-->
<!-- (c) Copyright IBM Corp. 2006 All Rights Reserved. -->
<project name="ditaot-preprocess" basedir=".">

  <target name="preprocess"
    depends="start-preprocess,clean-temp,gen-list,debug-filter,copy-files,conref,move-meta-entries,mapref,mappull,chunk,maplink,move-links,topicpull"
    description="Preprocessing ended" />

  <!-- preprocess modules begin -->
  <target name="start-preprocess" description="Preprocessing started" />

  <target name="clean-temp" unless="clean_temp"
    description="Clean temp directory">
    <delete includeemptydirs="true">
      <fileset dir="${dita.temp.dir}" includes="**/*"
        excludes="property.temp" />
    </delete>
  </target>

  <target name="gen-list"
    depends="gen-list-without-flagging, gen-list-with-flagging"
    description="Generate file list">
  </target>

  <target name="gen-list-without-flagging"
    unless="dita.input.valfile">
    <pipeline message="Generate list." module="GenMapAndTopicList"
      basedir="${basedir}" inputmap="${args.input}"
      tempdir="${dita.temp.dir}" extparam="ditadir=${dita.dir};validate=${validate};generatecopyouter=${generate.copy.outer};outercontrol=${outer.control};onlytopicinmap=${onlytopic.in.map};outputdir=${output.dir}" />
  </target>

  <target name="gen-list-with-flagging" if="dita.input.valfile">
    <pipeline message="Generate list." module="GenMapAndTopicList"
      basedir="${basedir}" inputmap="${args.input}"
      tempdir="${dita.temp.dir}"
      extparam="ditadir=${dita.dir};ditaval=${dita.input.valfile};validate=${validate};generatecopyouter=${generate.copy.outer};outercontrol=${outer.control};onlytopicinmap=${onlytopic.in.map};outputdir=${output.dir}" />
  </target>

  <target name="debug-filter" depends="gen-list, debug, debug-and-filter"
    description="Debug and filter input files" >
    <!--<property file="${dita.temp.dir}${file.separator}dita.list" />-->
    <xmlpropertyreader file="${dita.temp.dir}${file.separator}dita.xml.properties"/>
    <dirname property="dita.map.output.dir"
      file="${output.dir}${file.separator}${user.input.file}" />
    <condition property="noConref">
      <equals arg1="${conreflist}" arg2="" />
    </condition>
    <condition property="noMap">
      <equals arg1="${fullditamaplist}" arg2="" />
    </condition>
    <condition property="noTopic">
      <equals arg1="${fullditatopiclist}" arg2="" />
    </condition>
    <condition property="noImagelist">
      <equals arg1="${imagelist}" arg2="" />
    </condition>
    <condition property="noHtmllist">
      <equals arg1="${htmllist}" arg2="" />
    </condition>
    <condition property="noSubList">
      <equals arg1="${subtargetslist}" arg2="" />
    </condition>
  </target>

  <target name="debug" unless="dita.input.valfile"
    description="Debug input files">
    <pipeline message="Debug and filtering." module="DebugAndFilter"
      basedir="${basedir}" tempdir="${dita.temp.dir}"
      extparam="ditaext=${dita.extname};ditadir=${dita.dir};validate=${validate}"/>
  </target>

  <target name="debug-and-filter" if="dita.input.valfile"
    description="Debug and filter input files">
    <pipeline message="Debug and filtering." module="DebugAndFilter"
      basedir="${basedir}" tempdir="${dita.temp.dir}"
      extparam="ditaval=${dita.input.valfile};ditaext=${dita.extname};ditadir=${dita.dir};validate=${validate}" />
  </target>

  <target name="move-meta-entries" unless="noMap"
    depends="debug-filter" description="Move metadata entries">
    <pipeline message="Move metadata entries." module="MoveMeta"
      basedir="${basedir}" inputmap="${user.input.file}"
      tempdir="${dita.temp.dir}" />
  </target>

  <target name="conref" unless="noConref" depends="debug-filter"
    description="Resolve conref in input files">
    <xslt processor="trax" basedir="${dita.temp.dir}"
      destdir="${dita.temp.dir}" includes="${conreflist}"
      classpathref="dost.class.path"
      style="${dita.script.dir}${file.separator}preprocess${file.separator}conref.xsl">
      <mapper type="glob" from="*" to="*.cnrf" />
      <param name="DITAEXT" expression="${dita.ext}" />
    </xslt>
    <move todir="${dita.temp.dir}">
      <fileset dir="${dita.temp.dir}" includes="**/*.cnrf" />
      <mapper type="glob" from="*.cnrf" to="*" />
    </move>
  </target>
  
  <target name="mapref" unless="noMap" depends="move-meta-entries"
    description="Resolve mapref in ditamap">
    <dirname property="mapref.workdir"
      file="${dita.temp.dir}${file.separator}${user.input.file}" />
    <xslt processor="trax"
    	basedir="${dita.temp.dir}"
    	destdir="${dita.temp.dir}"
    	includes="${fullditamaplist}"
    	extension=".ditamap.ref"
        classpathref="dost.class.path"
      style="${dita.script.dir}${file.separator}preprocess${file.separator}mapref.xsl">
      <param name="DITAEXT" expression="${dita.ext}" if="dita.ext" />
      <param name="TRANSTYPE" expression="${transtype}" />
    </xslt>
    <move overwrite="true" todir="${dita.temp.dir}">
      <fileset dir="${dita.temp.dir}" includes="**/*.ditamap.ref" />
      <mapper type="glob" from="*.ditamap.ref" to="*.ditamap" />
    </move>
  </target>

  <target name="mappull" unless="noMap" depends="mapref"
    description="Pull the navtitle and topicmeta from topics to ditamap">
    <dirname property="mappull.workdir"
      file="${dita.temp.dir}${file.separator}${user.input.file}" />
    <xslt processor="trax"
  	    basedir="${dita.temp.dir}"
    	destdir="${dita.temp.dir}"
  	    includes="${fullditamaplist}"
    	extension=".ditamap.pull"
        classpathref="dost.class.path"
      style="${dita.script.dir}${file.separator}preprocess${file.separator}mappull.xsl">
      <param name="DITAEXT" expression="${dita.ext}" if="dita.ext" />
      <param name="TRANSTYPE" expression="${transtype}" />
    </xslt>
    <move overwrite="true" todir="${dita.temp.dir}">
      <fileset dir="${dita.temp.dir}" includes="**/*.ditamap.pull" />
      <mapper type="glob" from="*.ditamap.pull" to="*.ditamap" />
    </move>
  </target>
  
  <target name="chunk" unless="noMap"
    depends="mappull" description="Process chunks">
    <pipeline message="Process chunks." module="Chunk"
      basedir="${basedir}" inputmap="${user.input.file}"
      tempdir="${dita.temp.dir}" 
      extparam="ditaext=${dita.ext};transtype=${transtype}"/>
    <property file="${dita.temp.dir}${file.separator}dita.list" />
  </target>

  <target name="maplink" unless="noMap" depends="chunk"
    description="Find and generate related link information">
    <dirname property="maplink.workdir"
      file="${dita.temp.dir}/${user.input.file}" />
    <xslt processor="trax"
      in="${dita.temp.dir}${file.separator}${user.input.file}"
      out="${maplink.workdir}${file.separator}maplinks.unordered"
      classpathref="dost.class.path"
      style="${dita.script.dir}${file.separator}preprocess${file.separator}maplink.xsl">
      <param name="DITAEXT" expression="${dita.ext}" if="dita.ext" />
      <param name="INPUTMAP" expression="${user.input.file}" />
    </xslt>
  </target>

  <target name="move-links" unless="noMap" depends="maplink"
    description="Move the related link information to topics">
    <pipeline message="Move links." module="MoveLinks"
      extparam="maplinks=${maplink.workdir}${file.separator}maplinks.unordered"
      inputmap="${user.input.file}" tempdir="${dita.temp.dir}" />
  </target>

  <target name="topicpull" unless="noTopic" depends="debug-filter"
    description="Pull metadata for link and xref element">
    <xslt processor="trax" basedir="${dita.temp.dir}"
      destdir="${dita.temp.dir}" includes="${fullditatopiclist} ${chunkedtopiclist}"
      classpathref="dost.class.path"
      style="${dita.script.dir}${file.separator}preprocess${file.separator}topicpull.xsl">
      <param name="DITAEXT" expression="${dita.ext}" if="dita.ext" />
      <param name="ONLYTOPICINMAP" expression="${onlytopic.in.map}" if="onlytopic.in.map"/>
      <mapper type="glob" from="*" to="*.pull" />
    </xslt>
    <move todir="${dita.temp.dir}">
      <fileset dir="${dita.temp.dir}" includes="**/*.pull" />
      <mapper type="glob" from="*.pull" to="*" />
    </move>
  </target>

  <target name="copy-files"
    depends="debug-filter,copy-image, copy-html, copy-flag, copy-subsidiary, copy-generated-files" />

  <target name="copy-image" unless="noImagelist"
    description="Copy image files">
    <copy todir="${output.dir}">
      <fileset dir="${user.input.dir}" includes="${imagelist}" />
    </copy>
  </target>

  <target name="copy-html" unless="noHtmllist"
    description="Copy html files">
    <copy todir="${output.dir}">
      <fileset dir="${user.input.dir}" includes="${htmllist}"></fileset>
    </copy>
  </target>
  
  <target name="copy-flag" if="dita.input.valfile"
    description="Copy flag files">
    <copy todir="${dita.map.output.dir}">
      <fileset dir="${dita.resource.dir}"
        includes="delta.gif,deltaend.gif" />
    </copy>
    <dita-ot-copy todir="${output.dir}" includes="${flagimagelist}" relativepaths="${relflagimagelist}" />
  </target>
  
  <target name="copy-subsidiary" unless="noSubList"
    description="Copy subsidiary files">
    <copy todir="${dita.temp.dir}">
      <fileset dir="${user.input.dir}" includes="${subtargetslist}"/>
    </copy>
  </target>

  <target name="copy-generated-files"
    description="Copy generated files">
    <copy todir="${args.logdir}">
      <fileset dir="${dita.temp.dir}"
        includes="dita.list,property.temp,dita.xml.properties" />
    </copy>
  </target>

  <target name="copy-revflag" if="dita.input.valfile">
    <copy todir="${output.dir}">
      <fileset dir="${dita.resource.dir}" includes="*.gif" />
    </copy>
  </target>
    
  <target name="copy-css" unless="user.csspath.url" description="Copy CSS files">    
    <condition property="user.copycss.yes">
      <and>
        <equals arg1="${args.copycss}" arg2="yes"/>
        <isset property="args.css.present"/>
      </and>
    </condition>
    <property name="user.csspath.real" value="${output.dir}${file.separator}${user.csspath}"/>
    <mkdir dir="${user.csspath.real}"/>    
    <!-- Always copy system default css files -->
    <copy todir="${user.csspath.real}">
      <fileset dir="${dita.resource.dir}" includes="*.css"/>
    </copy>    
    <!-- Copy user specify css file when required -->
    <antcall target="copy-css-user"/>
  </target>

  <target name="copy-css-user" if="user.copycss.yes">
    <copy file="${args.css.real}" todir="${user.csspath.real}"/>
  </target>
  
  <!-- depecrated: keep for back-compatibility -->
  <target name="move-index-entries" depends="move-meta-entries"/>  
  
</project>
