<?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-init" basedir=".">
  <!--<import file="catalog-ant.xml"/>-->

	<!-- Default maximum memory to give to forked Java processes.
	
	     Set this higher either in another Ant script or on the
	     Ant command line using the -D parameter, e.g.:
	     
	     ant -DmaxJavaMemory=4G
	     
	     Memory values are integers followed by "M" for megabytes,
	     "G" for gigabytes.
	     
	     On 32-bit systems, the largest memory setting will be somewhere
	     between 1500M and 2000M depending on your system.
	     
	     64-bit JVMs can use essentially unlimited memory.
	     
	     For memory-intensive process (very large maps), 
	     2 or 3 gigabytes should be more than sufficient. 
	     
	     This value is used in the @maxmemory attribute the of the Ant
	     <java> task.
	  -->
	<property name="maxJavaMemory" value="500m"/>

  <dirname property="ant.file.ditaot-init.dir" file="${ant.file.ditaot-init}" />

  <!-- First try to initialize ${dita.dir} using the special 
    property passed by Ant's import task. -->
  <condition property="dita.dir" value="${ant.file.ditaot-init.dir}">
    <and>
      <isset property="ant.file.ditaot-init" />
      <not>
        <isset property="dita.dir" />
      </not>
    </and>
  </condition>

  <!-- Try to initialize ${dita.dir} again if it was not set. -->
  <condition property="dita.dir" value="${basedir}">
    <not>
      <isset property="dita.dir" />
    </not>
  </condition>
	
	<!-- Determine the Java architecture and set the JVM arguments
	     appropriately.
	     
	     Note that the Java architecture is determined by how the
	     JVM is started, so it is possible for the base Ant
	     JVM to have been started in 32-bit mode on a 64-bit machine.
	     
	     The is64bit and is32bit properties are mutually exclusive.
	     
	  -->
	<condition property="is64bit">
		<!-- NOTE: The following is not a complete list but should cover
		           most of the systems Open Toolkit users are likely
		           to use. Add more values as they become known.
		  -->
		<or>
			<os arch="x86_64"/>
			<os arch="amd64"/>
			<os arch="ppc64"/>
		</or>
  </condition>
	
	<condition property="is32bit">
		<isfalse value="${is64bit}"/>
  </condition>
	
	
	<!-- For 64-bit machines, the value -d64 runs the
	     the JVM in 64-bit mode. This parameter cannot
	     be specified on 32-bit machines (that is, you cannot
	     specify -d32 on a 32-bit machine, you get an "unrecognized
	     option error in that case).
	  -->
	<condition property="jvmArchFlag"
	  value="-d64"
	  else="">
		<istrue value="${is64bit}"/>
	</condition>

	<!-- Base JVM argument line to use with any forked Java processes.
	
	     Sets the JVM architecture.
	     
	     Use like so:
	     
	     <java ...>
	       <jvmarg line="${baseJVMArgLine}"/>
	       ...
	     </java>
	  -->
	<property name="baseJVMArgLine" value="${jvmArchFlag} -Xmx${maxJavaMemory}"/>

  <path id="dost.jar.path">
    <pathelement
      location="${dita.dir}${file.separator}lib${file.separator}dost.jar" />
  </path>

  <taskdef name="pipeline"
    classname="org.dita.dost.invoker.ExtensibleAntInvoker">
    <classpath refid="dost.jar.path" />
  </taskdef>

  <taskdef name="ext-pipeline"
    classname="org.dita.dost.invoker.ExtensibleAntInvoker">
    <classpath refid="dost.jar.path" />
  </taskdef>

  <taskdef name="config-logger"
    classname="org.dita.dost.log.LogConfigTask">
    <classpath refid="dost.jar.path" />
  </taskdef>

  <!-- Add by Alan to refacter:URIResolver Date2009-08-07 -->
  <taskdef name="config-URIResolver"
  	classname="org.dita.dost.resolver.URIResolverConfigTask">
  	<classpath refid="dost.jar.path"/>
  </taskdef>
	
  <taskdef name="dita-ot-version"
  	classname="org.dita.dost.util.AntVersion">
  	<classpath refid="dost.jar.path"/>
  </taskdef>

  <taskdef name="dita-ot-echo"
    classname="org.dita.dost.log.DITAOTEchoTask">
    <classpath refid="dost.jar.path" />
  </taskdef>

  <taskdef name="dita-ot-fail"
    classname="org.dita.dost.log.DITAOTFailTask">
    <classpath refid="dost.jar.path" />
  </taskdef>

  <taskdef name="dita-ot-copy"
    classname="org.dita.dost.util.DITAOTCopy">
    <classpath refid="dost.jar.path" />
  </taskdef>
	
  <taskdef name="xmlpropertyreader"
  	classname="org.dita.dost.platform.XmlPropertyTask">
  	<classpath refid="dost.jar.path"/>
  </taskdef>
	
  <typedef name="isabsolute"
    classname="org.dita.dost.util.IsAbsolute">
    <classpath refid="dost.jar.path" />
  </typedef>
  <tstamp>
    <format property="current.date" pattern="yyyyMMddHHmmssSSS"/>
  </tstamp>
  <property name="base.temp.dir" location="${basedir}/temp"/>
  <property name="dita.temp.dir" location="${base.temp.dir}${file.separator}temp${current.date}" />
  <property name="output.dir" location="${basedir}/out" />
  <property name="dita.extname" value="xml" />
  <property name="dita.script.dir"
    location="${dita.dir}${file.separator}xsl" />
  <property name="dita.resource.dir"
    location="${dita.dir}${file.separator}resource" />
  <property environment="env" />
  <property name="dita.empty" value="" />
  <property name="args.message.file"
    location="${dita.dir}${file.separator}resource${file.separator}messages.xml" />
  <!-- property values end -->

  <!-- Changed by Alan to refacter:URIResolver Date2009-08-07 -->
  <target name="build-init"
    depends="start-process, init-logger, init-URIResolver, use-init, output-version, check-arg, output-msg" />
    
  <target name="start-process" description="Processing started" />

  <target name="init-logger"
    description="Initialize log directory and file name">
    <config-logger />
  </target>
  
  <!-- Add by Alan to refacter:URIResolver Date2009-08-07 -->
  <target name="init-URIResolver">
  	<config-URIResolver basedir="${basedir}" tempdir="${dita.temp.dir}"/>
  </target>
  
  <target name="use-init.envhhcdir" if="env.HHCDIR">
    <available file="${env.HHCDIR}${file.separator}hhc.exe" property="HTMLHelpCompiler"
      value="${env.HHCDIR}${file.separator}hhc.exe"/>
  </target>
  
  <target name="use-init.hhcdir" unless="env.HHCDIR">
    <condition property="hhc.dir" value="C:\Program Files (x86)\HTML Help Workshop" else="C:\Program Files\HTML Help Workshop">
      <available file="C:\Program Files (x86)\HTML Help Workshop" type="dir"/>
    </condition>
    <available file="${hhc.dir}${file.separator}hhc.exe" property="HTMLHelpCompiler"
      value="${hhc.dir}${file.separator}hhc.exe" />
  </target>
  
  <target name="use-init" depends="use-init.envhhcdir,use-init.hhcdir">
    <condition property="xml.parser" value="XMLReader ${org.xml.sax.driver}">
      <and>
        <isset property="org.xml.sax.driver"/>
        <not><isset property="xml.parser"/></not>
      </and>
    </condition>
    <condition property="xml.parser" value="Xerces">
      <and>
        <available classname="org.apache.xerces.parsers.SAXParser"/>
        <not><isset property="xml.parser"/></not>
      </and>
    </condition>
    <condition property="xml.parser" value="Xerces in Sun JDK 1.5">
      <and>
        <available classname="com.sun.org.apache.xerces.internal.parsers.SAXParser"/>
        <not><isset property="xml.parser"/></not>
      </and>
    </condition>
    <condition property="xml.parser" value="Crimson">
      <and>
        <available classname="org.apache.crimson.parser.XMLReaderImpl"/>
        <not><isset property="xml.parser"/></not>
      </and>
    </condition>
    <available classname="net.sf.saxon.StyleSheet" property="hasSaxon" />
    <available classname="net.sf.saxon.Transform" property="hasSaxon" />
    <available classname="org.apache.fop.tools.anttasks.Fop" property="hasFOP">
      <classpath>
        <pathelement location="${dita.dir}/demo/legacypdf/lib/fop.jar"/>
      </classpath>
    </available>
  </target>
	
  <target name="output-version">
  	<dita-ot-version/>
  	<echo level="info">Version: ${otversion}</echo>
  </target>

  <!-- property value validation begin -->
  <target name="check-arg" depends="use-init"
    description="Validate and init input arguments">
    <!-- begin to check input parameters -->
    <dita-ot-fail id="DOTA002F">
      <condition>
        <and>
          <or>
            <not>
              <isset property="args.input" />
            </not>
            <and>
              <isset property="args.input" />
              <not>
                <available file="${args.input}" type="file" />
              </not>
            </and>
          </or>
          <or>
            <not>
              <isset property="dita.input" />
            </not>
            <not>
              <isset property="dita.input.dirname" />
            </not>
            <and>
              <isset property="dita.input" />
              <isset property="dita.input.dirname" />
              <not>
                <available file="${dita.input}"
                  filepath="${dita.input.dirname}" type="file" />
              </not>
            </and>
          </or>
        </and>
      </condition>
    </dita-ot-fail>

    <dita-ot-fail id="DOTA003F" params="%1=${args.xsl}">
      <condition>
        <and>
          <isset property="args.xsl" />
          <not>
            <available file="${args.xsl}" type="file" />
          </not>
        </and>
      </condition>
    </dita-ot-fail>
  	
  	<!--Added by William on 2010-06-21 for bug:3012392 start -->
    <dita-ot-fail id="DOTA066F" params="%1=${args.xsl.pdf}">
      <condition>
        <and>
          <isset property="args.xsl.pdf" />
          <not>
            <available file="${args.xsl.pdf}" type="file" />
          </not>
        </and>
      </condition>
    </dita-ot-fail>
  	<!--Added by William on 2010-06-21 for bug:3012392 start -->
  	
    <dita-ot-fail id="DOTA004F" params="%1=${dita.extname}">
      <condition>
        <not>
          <or>
            <contains string="${dita.extname}" substring="dita" />
            <contains string="${dita.extname}" substring="xml" />
          </or>
        </not>
      </condition>
    </dita-ot-fail>
   <dita-ot-fail id="DOTA007E" params="%1=${args.ftr}">
        <condition>
        	<and>
        	 <isset property="args.ftr" />
        	 <not>
        	<available file="${args.ftr}" type="file"/>
        	</not>
        	</and>
        </condition>
     </dita-ot-fail>
    <dita-ot-fail id="DOTA008E" params="%1=${args.hdr}">
      <condition>
        <and>
          <isset property="args.hdr" />
          <not>
            <available file="${args.hdr}" type="file"/>
          </not>
        </and>
      </condition>
    </dita-ot-fail>
    <dita-ot-fail id="DOTA009E" params="%1=${args.hdf}">
      <condition>
        <and>
          <isset property="args.hdf" />
          <not>
            <available file="${args.hdf}" type="file"/>
          </not>
        </and>
      </condition>
    </dita-ot-fail>
    <!-- end to check input parameters -->

    <!-- begin to init required parameters -->

    <condition property="msg.deprecate">
      <and>
        <not>
          <isset property="args.input" />
        </not>
        <isset property="dita.input" />
        <isset property="dita.input.dirname" />
      </and>
    </condition>

    <condition property="args.input"
      value="${dita.input.dirname}${file.separator}${dita.input}">
      <not>
        <isset property="args.input" />
      </not>
    </condition>

    <condition property="dita.ext" value=".xml">
      <and>
        <isset property="dita.extname" />
        <contains string="${dita.extname}" substring="xml" />
      </and>
    </condition>
    <condition property="dita.ext" value=".dita">
      <and>
        <isset property="dita.extname" />
        <contains string="${dita.extname}" substring="dita" />
      </and>
    </condition>

    <!-- append a '.' to out.ext if args.outext not contains '.' -->
    <condition property="out.ext" value=".${args.outext}">
      <and>
        <isset property="args.outext" />
        <not>
          <contains string="${args.outext}" substring="." />
        </not>
      </and>
    </condition>
    <condition property="out.ext" value="${args.outext}">
      <and>
        <isset property="args.outext" />
        <contains string="${args.outext}" substring="." />
      </and>
    </condition>

    <!-- append a '.' to ${fo.img.ext} if ${args.fo.img.ext} not contains '.' -->
    <condition property="fo.img.ext" value=".${args.fo.img.ext}">
      <and>
        <isset property="args.fo.img.ext" />
        <not>
          <contains string="${args.fo.img.ext}" substring="." />
        </not>
      </and>
    </condition>
    <condition property="fo.img.ext" value="${args.fo.img.ext}">
      <and>
        <isset property="args.fo.img.ext" />
        <contains string="${args.fo.img.ext}" substring="." />
      </and>
    </condition>
  	
	<!--Added by William on 2009-11-17 for bug:2893493 start-->
  	<condition property="clean.temp"
      value="true">
	      <not>
	        <isset property="clean.temp" />
	      </not>
  	</condition>
  	<!--Added by William on 2009-11-17 for bug:2893493 start-->
  	<!-- Added on 2010-08-24 for bug:2994593 enable grammar pool caching by default start-->
  	<condition property="args.grammar.cache"
  	      value="yes">
  		      <not>
  		        <isset property="args.grammar.cache" />
  		      </not>
  	</condition> 	
  	<!-- Added on 2010-08-24 for bug:2994593 enable grammar pool caching by default end-->
  	
  	<!-- Added on 2010-11-16 for bug:3086552 enable system id setting in XMLReader.parse start-->
  	<condition property="args.xml.systemid.set"
  	      value="yes">
  		      <not>
  		        <isset property="args.xml.systemid.set" />
  		      </not>
  	</condition> 	
  	<!-- Added on 2010-11-16 for bug:3086552 enable system id setting in XMLReader.parse end-->
  	
  	<condition property="args.odt.img.embed"
  	  	      value="yes">
  	  		      <not>
  	  		        <isset property="args.odt.img.embed"/>
  	  		      </not>
  	</condition>
  	<condition property="clean_temp">
      <and>
        <isset property="clean.temp" />
        <equals arg1="${clean.temp}" arg2="no" casesensitive="false" />
      </and>
    </condition>

    <basename property="dita.input.filename" file="${args.input}" />
    <basename property="dita.map.filename.root"
      file="${dita.input.filename}" suffix=".ditamap" />
    <basename property="dita.topic.filename.root"
      file="${dita.input.filename}" suffix="${dita.ext}" />
    <!-- end to init required parameters -->

    <!-- create required directories -->
    <mkdir dir="${output.dir}" />
    <mkdir dir="${dita.temp.dir}" />

    <!-- begin to check and init css relevant properties -->
    <condition property="user.csspath.url">
      <or>
        <contains string="${args.csspath}" substring="http://" />
        <contains string="${args.csspath}" substring="https://" />
      </or>
    </condition>
    <condition property="args.csspath.absolute">
      <isabsolute path="${args.csspath}" />
    </condition>
    <!-- Set the csspath as default empty directory when there is no user
      input csspath or user input csspath is a local absolute path. -->
    <condition property="user.csspath" value="">
      <or>
        <not>
          <isset property="args.csspath" />
        </not>
        <isset property="args.csspath.absolute" />
      </or>
    </condition>
    <condition property="user.csspath" value="${args.csspath}/">
      <not>
        <isset property="user.csspath" />
      </not>
    </condition>
    <condition property="args.css.real"
      value="${args.cssroot}${file.separator}${args.css}">
      <isset property="args.cssroot" />
    </condition>
    <condition property="args.css.real" value="${args.css}">
      <not>
        <isset property="args.cssroot" />
      </not>
    </condition>
    <available file="${args.css.real}" property="args.css.present"
      type="file" />
    <basename property="args.css.file.temp" file="${args.css}" />
    <condition property="args.css.file" value="${args.css.file.temp}">
    	<or>
    		<isset property="args.css.present" />
    		<isset property="user.csspath.url" />
    	</or>
    </condition>
    <!-- end to check and init css relevant parameters -->

    <condition property="args.logdir" value="${output.dir}">
      <not>
        <isset property="args.logdir" />
      </not>
    </condition>

    <condition property="xslt.parser" value="SAXON">
      <isset property="hasSaxon" />
    </condition>
    <condition property="xslt.parser" value="XALAN">
      <not>
        <isset property="xslt.parser" />
      </not>
    </condition>
  	
  	<!-- Validate the xml file or not,default is validation(true)-->
  	<condition property="validate" value="true">
  	   <not>
  		 <isset property="validate" />
  	   </not>
  	</condition>
  	
  	<!--solution for the output control-->
  	<condition property="generate.copy.outer" value="1">
  	   <not>
  		 <isset property="generate.copy.outer" />
  	   </not>
  	</condition>
  	
  	<condition property="onlytopic.in.map" value="false">
  	   <not>
  		 <isset property="onlytopic.in.map" />
  	   </not>
	</condition>
  	<!--
  	default value : warn
	fail     :1a) Fail quickly if files are going to be generated/copied outside of that directory
	warn  :1b) Complete if files will be generated/copied outside, but log a warning
	quiet  :1c) Quietly finish with only those files (no warning or error)
  	-->
  	<condition property="outer.control" value="warn">
  	   <not>
  		 <isset property="outer.control" />
  	   </not>
  	</condition>
  	
  	<!-- Move the outer.transform to build_general.xml -->
	<!--
  	<condition property="outer.transform">
	   <equals arg1="${generate.copy.outer}" arg2="2"></equals>
	</condition>	
    -->
    
    <condition property="inner.transform">
	   <or>
	 	 <equals arg1="${generate.copy.outer}" arg2="1"></equals>
	     <equals arg1="${generate.copy.outer}" arg2="2"></equals>
	   </or>
  	</condition>
  	
  	<condition property="old.transform">
    	<equals arg1="${generate.copy.outer}" arg2="3"></equals>
    </condition>
    
    <!-- output parameters info -->
    <echo level="info">*****************************************************************</echo>
    <echo level="info">* basedir = ${basedir}</echo>
    <echo level="info">* dita.dir = ${dita.dir}</echo>
    <echo level="info">* input = ${args.input}</echo>
    <echo level="info">* transtype = ${transtype}</echo>
    <echo level="info">* tempdir = ${dita.temp.dir}</echo>
    <echo level="info">* outputdir = ${output.dir}</echo>
    <echo level="info">* extname = ${dita.ext}</echo>
    <echo level="info">* clean.temp = ${clean.temp}</echo>
    <echo level="info">* xslt.parser = ${xslt.parser}</echo>
    <echo level="info">*****************************************************************</echo>
  	<echoproperties regex="^(arg|preprocess|dita)" taskname="echo" failonerror="false"/>
  	<echo level="info">*****************************************************************</echo>
    <echo level="info">* Using ${xml.parser}</echo>
    <echo level="info">*****************************************************************</echo>
  </target>

  <target name="output-msg" depends="output-deprecated-msg, output-css-warn-message"/>
  
  <target name="output-deprecated-msg" if="msg.deprecate">
    <dita-ot-echo id="DOTA005W" />
  </target>
  
  <target name="output-css-warn-message" if="args.csspath.absolute">
    <dita-ot-echo id="DOTA006W" />
  </target>

</project>
