<?xml version="1.0" encoding="UTF-8"?>
<!-- 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. 2004, 2006 All Rights Reserved. -->
<!-- build whole bin and source package for DITA Open Toolkit -->
<project name="build_DITA-OT_package" default="build-all-packages" basedir=".">
  <description>Build source and binary packages for DITA Open Toolkit</description>

  <property file="buildPackage.properties"/>

  <condition property="HTMLHelpCompiler" value="C:\Program Files\HTML Help Workshop\hhc.exe">
    <available file="C:\Program Files\HTML Help Workshop\hhc.exe"/>
  </condition>
  <condition property="HTMLHelpCompiler" value="C:\Program Files (x86)\HTML Help Workshop\hhc.exe">
    <available file="C:\Program Files (x86)\HTML Help Workshop\hhc.exe"/>
  </condition>
  <property name="package.name.root" value="DITA-OT"/>

  <property name="src.dir" location="${basedir}"/>
  <property name="java.dir" location="${src.dir}/src/"/>
  <property name="lib.dir" location="${src.dir}/lib/"/>
  <property name="doc.dir" location="${src.dir}/doc/"/>
  <property name="dtd.dir" location="${src.dir}/dtd/"/>
  <property name="schema.dir" location="${src.dir}/schema/"/>
  <property name="xsl.dir" location="${src.dir}/xsl/"/>
  <property name="demo.dir" location="${src.dir}/demo/"/>
  <property name="sample.dir" location="${src.dir}/samples/"/>
  <property name="css.dir" location="${src.dir}/css/"/>
  <property name="ant.dir" location="${src.dir}/ant/"/>
  <property name="resource.dir" location="${src.dir}/resource/"/>

  <property name="top.work.dir" location="${basedir}"/>
  <property name="package.output.dir" location="${top.work.dir}/build/"/>
  <property name="bin.dir" location="${top.work.dir}/output2/"/>
  <tstamp>
    <format property="current.date" pattern="yyyyMMddHHmmssSSS"/>
  </tstamp>
  <property name="work.dir" location="${top.work.dir}/temp/${current.date}/"/>
  <property name="build.dir" location="${bin.dir}/build"/>
  <property name="out.dir" location="${work.dir}/out/"/>
  <property name="doc.out.dir" location="${out.dir}/doc"/>

  <property name="dost.jar" location="${lib.dir}/dost.jar"/>
  <property name="binary-min" value="DITA-OT${version}_minimal_bin"/>
  <property name="binary-big" value="DITA-OT${version}_full_easy_install_bin"/>
  <property name="binary" value="DITA-OT${version}_standard_bin"/>
  <property name="source" value="DITA-OT${version}_src"/>
  <property name="win32.zip.suffix" value=".zip"/>
  <property name="linux.zip.suffix" value=".tar.gz"/>

  <target name="init-package" depends="clean-package">
    <mkdir dir="${package.output.dir}"/>
  </target>

  <target name="clean-package">
    <delete dir="${package.output.dir}"/>
  </target>

  <target name="init-java" depends="clean-java">
    <mkdir dir="${lib.dir}"/>
    <mkdir dir="${bin.dir}"/>
  </target>

  <target name="clean-java">
    <delete dir="${lib.dir}/dost.jar"/>
    <delete dir="${bin.dir}"/>
  </target>

  <target name="build-java" depends="compile"/>
  <target name="compile" depends="init-java" description="Compile Java">
    <!-- Copying the files to a build directory before we modify the files -->
    <copy todir="${build.dir}">
      <fileset dir="${java.dir}" excludes="com/**,org/dita/dost/junit/**"/>
    </copy>
    <!-- Update the token so that we can find out what the version of DITA-OT users are using -->
    <replace file="${build.dir}/org/dita/dost/util/Version.java" token="@@VERSION@@"
      value="${package.name.root} ${version}"/>
    <replace file="${build.dir}/org/dita/dost/util/VersionUtil.java" token="@@OTVERSION@@"
      value="${version}"/>
    <javac srcdir="${build.dir}" destdir="${bin.dir}"
      excludes="com/**,org/dita/dost/junit/**, org/dita/dost/invoker/JavaInvoker.java" debug="on"
      source="1.5" target="1.5">
      <classpath>
        <pathelement location="${lib.dir}/resolver.jar"/>
        <pathelement location="${lib.dir}/commons-codec-1.4.jar"/>
      </classpath>
    </javac>
    <delete dir="${build.dir}" includeemptydirs="yes"/>
  </target>

  <target name="package-java" depends="jar"/>
  <target name="jar" depends="compile" description="Build Java libraries">
    <copy todir="${bin.dir}/resource/">
      <fileset dir="${resource.dir}" includes="messages.*"/>
    </copy>
    <copy todir="${bin.dir}">
      <fileset dir="${java.dir}" includes="**/*.properties, **/*.xml"/>
    </copy>
    <jar destfile="${dost.jar}" basedir="${bin.dir}" includes="**">
      <manifest>
        <attribute name="Implementation-Version" value="${version}"/>
        <attribute name="Main-Class" value="org.dita.dost.invoker.CommandLineInvoker"/>
      </manifest>
    </jar>
  </target>
	
  <patternset id="pattern.global.excludes">
    <exclude name=".*"/>
    <exclude name="build.log"/>
    <exclude name="build.number"/>
    <exclude name="relnotes.txt"/>
    <exclude name="lib/commons-logging-1.1.1.jar"/>
    <exclude name="plugins/cxxapiref/"/>
    <exclude name="plugins/net.sourceforge.dita-ot.html/**"/>
    <exclude name="demo/book/**"/>
    <exclude name="demo/elementref/**"/>
    <exclude name="demo/enote/**"/>
    <exclude name="demo/faq/**"/>
    <exclude name="demo/java/**"/>
  	<exclude name="buildSourceForge.xml"/>
  	<exclude name="doc/sourceforge/**"/>
    <exclude name="doc/userguide-orig/**"/>
    <exclude name="doc/ot-userguide/**"/>
    <exclude name="doc/archive/**"/>
    <exclude name="doc/langref-dita1.1/**"/>
  </patternset>
  
  <patternset id="pattern.package.ant">
    <include name="tools/ant/*.*"/>
    <include name="tools/ant/bin/**"/>
    <include name="tools/ant/lib/**"/>
  </patternset>

  <patternset id="pattern.package.minimum">
    <patternset refid="pattern.global.excludes"/>
    <exclude name="demo/fo/fop/build/"/>
    <exclude name="demo/fo/fop/conf/"/>
    <exclude name="demo/fo/fop/lib/"/>
    <exclude name="demo/fo/lib/icu4j.jar"/>
    <exclude name="lib/saxon/*"/>
    <exclude name="lib/icu4j.jar"/>
    <exclude name="lib/log4j.properties"/>
    <exclude name="startcmd.*"/>
    <exclude name="buildPackage.*"/>
    <include name="demo/tocjs/**"/>
    <include name="demo/h2d/**"/>
    <include name="demo/fo/**"/>
    <include name="plugins/org.dita.*/**"/>
    <include name="*.*"/>
    <include name="css/**"/>
    <include name="dtd/**"/>
    <include name="resource/**"/>
    <include name="schema/**"/>
    <include name="xsl/**"/>
    <include name="lib/dost.jar"/>
    <include name="lib/APACHE-LICENSE-2_0.html"/>
    <include name="lib/resolver.jar"/>
    <include name="lib/CatalogManager.properties"/>
    <include name="lib/**"/>
  </patternset>

  <patternset id="pattern.package.full">
  	<patternset refid="pattern.global.excludes"/>
    <exclude name="buildPackage.*"/>
    <exclude name="lib/log4j.properties"/>
    <include name="*.*"/>
    <include name="ant/**"/>
    <include name="css/**"/>
    <include name="demo/eclipsemap/**"/>
    <include name="demo/h2d/**"/>
    <include name="demo/dita11/**"/>
    <include name="demo/dita132/**"/>
    <include name="demo/fo/**"/>
    <include name="demo/legacypdf/**"/>
    <include name="demo/tocjs/**"/>
    <include name="doc/**"/>
    <include name="dtd/**"/>
    <include name="plugins/**"/>
    <include name="resource/**"/>
    <include name="samples/**"/>
    <include name="schema/**"/>
    <include name="xsl/**"/>
    <include name="lib/**"/>
    <patternset refid="pattern.package.ant"/>
  </patternset>

  <patternset id="pattern.package.standard.base">
  	<patternset refid="pattern.global.excludes"/>
    <exclude name="demo/fo/fop/build/"/>
    <exclude name="demo/fo/fop/conf/"/>
    <exclude name="demo/fo/fop/lib/"/>
    <exclude name="demo/fo/lib/icu4j.jar"/>
    <exclude name="lib/saxon/"/>
    <exclude name="lib/icu4j.jar"/>
    <exclude name="lib/log4j.properties"/>
    <include name="*.*"/>
    <include name="ant/**"/>
    <include name="css/**"/>
    <include name="demo/eclipsemap/**"/>
    <include name="demo/h2d/**"/>
    <include name="demo/dita11/**"/>
    <include name="demo/dita132/**"/>
    <include name="demo/fo/**"/>
    <include name="demo/legacypdf/**"/>
    <include name="demo/tocjs/**"/>
    <include name="doc/**"/>
    <include name="dtd/**"/>
    <include name="plugins/**"/>
    <include name="resource/**"/>
    <include name="samples/**"/>
    <include name="schema/**"/>
    <include name="xsl/**"/>
    <include name="lib/**"/>
  </patternset>
  
  <patternset id="pattern.package.standard">
    <patternset refid="pattern.package.standard.base"/>
    <exclude name="startcmd.*"/>
    <exclude name="buildPackage.*"/>
  </patternset>
  
  <patternset id="pattern.package.source">
    <patternset refid="pattern.package.standard.base"/>
    <exclude name="src/org/dita/dost/invoker/JavaInvoker.java"/>
    <exclude name="lib/dost.jar"/>
    <exclude name="files.txt"/>
    <exclude name="startcmd.*"/>
    <include name="src/**"/>
  </patternset>

  <patternset id="pattern.package.doc">
    <include name="relnotes.html"/>
    <include name="doc/**"/>
    <exclude name="**/*.hhp"/>
    <exclude name="**/*.hhc"/>
    <exclude name="**/*.hhk"/>
    <exclude name="**/*.fo"/>
    <exclude name="**/*.log"/>
    <exclude name="**/*.list"/>
    <exclude name="**/*.temp"/>
    <exclude name="**/*.db"/>
  </patternset>

  <target name="package-source">
    <fail unless="param.out.file">param.out.file not defined</fail>
    <property name="temp.dist.dir" location="${work.dir}/${param.out.file}/${package.name.root}${version}"/>
    <delete dir="${temp.dist.dir}"/>
    <mkdir dir="${temp.dist.dir}"/>
    <copy todir="${temp.dist.dir}">
      <fileset dir="${basedir}">
        <patternset refid="pattern.package.source"/>
      </fileset>
    </copy>
    <copy todir="${temp.dist.dir}">
      <fileset dir="${out.dir}">
        <patternset refid="pattern.package.doc"/>
      </fileset>
    </copy>
    <ant antfile="${temp.dist.dir}/integrator.xml"/>
    <zip destfile="${package.output.dir}/${param.out.file}${win32.zip.suffix}">
      <fileset dir="${work.dir}/${param.out.file}"/>
    </zip>
    <tar destfile="${package.output.dir}/${param.out.file}${linux.zip.suffix}" compression="gzip" longfile="gnu">
      <tarfileset dir="${work.dir}/${param.out.file}"/>
    </tar>
  </target>

  <target name="package-binary-minimum">
    <fail unless="param.out.file">param.out.file not defined</fail>
    <property name="temp.dist.dir" location="${work.dir}/${param.out.file}/${package.name.root}${version}"/>
    <delete dir="${temp.dist.dir}"/>
    <mkdir dir="${temp.dist.dir}"/>
    <copy todir="${temp.dist.dir}">
      <fileset dir="${basedir}">
        <patternset refid="pattern.package.minimum"/>
      </fileset>
    </copy>
    <!-- no documentation -->
    <ant antfile="${temp.dist.dir}/integrator.xml"/>
    <zip destfile="${package.output.dir}/${param.out.file}${win32.zip.suffix}">
      <fileset dir="${work.dir}/${param.out.file}"/>
    </zip>
    <tar destfile="${package.output.dir}/${param.out.file}${linux.zip.suffix}" compression="gzip" longfile="gnu">
      <tarfileset dir="${work.dir}/${param.out.file}"/>
    </tar>
  </target>

  <target name="package-binary-standard">
    <fail unless="param.out.file">param.out.file not defined</fail>
    <property name="temp.dist.dir" location="${work.dir}/${param.out.file}/${package.name.root}${version}"/>
    <delete dir="${temp.dist.dir}"/>
    <mkdir dir="${temp.dist.dir}"/>
    <copy todir="${temp.dist.dir}">
      <fileset dir="${basedir}">
        <patternset refid="pattern.package.standard"/>
      </fileset>
    </copy>
    <copy todir="${temp.dist.dir}">
      <fileset dir="${out.dir}">
        <patternset refid="pattern.package.doc"/>
      </fileset>
    </copy>
    <ant antfile="${temp.dist.dir}/integrator.xml"/>
    <zip destfile="${package.output.dir}/${param.out.file}${win32.zip.suffix}">
      <fileset dir="${work.dir}/${param.out.file}"/>
    </zip>
    <tar destfile="${package.output.dir}/${param.out.file}${linux.zip.suffix}" compression="gzip" longfile="gnu">
      <tarfileset dir="${work.dir}/${param.out.file}"/>
    </tar>
  </target>

  <target name="package-binary-full">
    <fail unless="param.out.file">param.out.file not defined</fail>
    <property name="temp.dist.dir" location="${work.dir}/${param.out.file}/${package.name.root}${version}"/>
    <delete dir="${temp.dist.dir}"/>
    <mkdir dir="${temp.dist.dir}"/>
    <copy todir="${temp.dist.dir}">
      <fileset dir="${basedir}">
        <patternset refid="pattern.package.full"/>
      </fileset>
    </copy>
    <copy todir="${temp.dist.dir}">
      <fileset dir="${out.dir}">
        <patternset refid="pattern.package.doc"/>
      </fileset>
    </copy>
    <ant antfile="${temp.dist.dir}/integrator.xml"/>
    <zip destfile="${package.output.dir}/${param.out.file}${win32.zip.suffix}">
      <zipfileset dir="${work.dir}/${param.out.file}">
        <exclude name="*/startcmd.sh"/>
      </zipfileset>
      <zipfileset dir="${work.dir}/${param.out.file}" filemode="755">
        <include name="*/startcmd.sh"/>
      </zipfileset>
    </zip>
    <tar destfile="${package.output.dir}/${param.out.file}${linux.zip.suffix}" compression="gzip" longfile="gnu">
      <tarfileset dir="${work.dir}/${param.out.file}">
        <exclude name="*/startcmd.sh"/>
      </tarfileset>
      <tarfileset dir="${work.dir}/${param.out.file}" mode="755">
        <include name="*/startcmd.sh"/>
      </tarfileset>
    </tar>
  </target>

  <target name="build-package-source" depends="jar, update-doc" description="Build source distribution package">
    <antcall target="package-source">
      <param name="param.out.file" value="${source}"/>
    </antcall>
  </target>

  <target name="build-package-binary" depends="jar, update-doc" description="Build binary distribution packages">
    <sequential>
      <antcall target="package-binary-full">
        <param name="param.out.file" value="${binary-big}"/>
      </antcall>
      <antcall target="package-binary-standard">
        <param name="param.out.file" value="${binary}"/>
      </antcall>
      <antcall target="package-binary-minimum">
        <param name="param.out.file" value="${binary-min}"/>
      </antcall>
    </sequential>
  </target>

  <target name="build-package-binary-with-buildnum" depends="jar, update-doc">
    <buildnumber/>
    <tstamp>
      <format property="build.time" pattern="yyyyMMddhhmm"/>
    </tstamp>
    <sequential>
      <antcall target="package-binary-full">
        <param name="param.out.file" value="${package.name.root}${version}b${build.number}_full_easy_install_bin.${build.time}"/>
      </antcall>
      <antcall target="package-binary-standard">
        <param name="param.out.file" value="${package.name.root}${version}b${build.number}_standard_bin.${build.time}"/>
      </antcall>
      <antcall target="package-binary-minimum">
        <param name="param.out.file" value="${package.name.root}${version}b${build.number}_minimal_bin.${build.time}"/>
      </antcall>
    </sequential>
  </target>

  <target name="build-all-packages"
    depends="init-package, build-package-source, build-package-binary, cleanup" description="Build distribution packages"/>

  <target name="cleanup" unless="skip.cleanup">
    <delete dir="${work.dir}"/>
  </target>

  <target name="update-doc">
    <ant antfile="${basedir}/integrator.xml" target="integrate"/>
    <!--mkdir dir="${work.dir}/doc"/-->
    
    <ant antfile="${basedir}/build.xml">
      <property name="args.input" location="${doc.dir}/userguide-book.ditamap"/>
      <property name="output.dir" location="${doc.out.dir}"/>
      <property name="outputFile.base" value="userguide"/>
      <property name="transtype" value="pdf"/>
      <property name="base.temp.dir" location="${work.dir}"/>
    </ant>

    <ant antfile="${basedir}/build.xml">
      <property name="args.input" location="${doc.dir}/userguide.ditamap"/>
      <property name="output.dir" location="${work.dir}/htmlhelp"/>
      <property name="transtype" value="htmlhelp"/>
      <property name="base.temp.dir" location="${work.dir}"/>
    </ant>
    <copy todir="${doc.out.dir}" failonerror="no">
      <fileset dir="${work.dir}/htmlhelp">
        <include name="*.chm"/>
      </fileset>
    </copy>

    <ant antfile="${basedir}/build.xml">
      <property name="args.input" location="${doc.dir}/userguide.ditamap"/>
      <property name="output.dir" location="${doc.out.dir}/html"/>
      <property name="outputFile.base" value="index"/>
      <property name="args.csspath" value="css"/>
      <property name="transtype" value="tocjs"/>
      <property name="base.temp.dir" location="${work.dir}"/>
    </ant>
    <move file="${doc.out.dir}/html/userguide.html" tofile="${doc.out.dir}/html/index.html"/>
    
    <ant antfile="${basedir}/build.xml">
      <property name="args.input" location="${doc.dir}/readme/changes/rel1.5.4.xml"/>
      <property name="output.dir" location="${work.dir}/relnotes"/>
      <property name="args.csspath" value="resource"/>
      <property name="transtype" value="xhtml"/>
      <property name="base.temp.dir" location="${work.dir}"/>
    </ant>
    <copy file="${work.dir}/relnotes/rel1.5.4.html" tofile="${out.dir}/relnotes.html"/>
    
    <delete failonerror="yes" includeemptydirs="true">
      <fileset dir="${doc.out.dir}">
        <include name="**/dita.list"/>
        <include name="**/dita.xml.properties"/>
      </fileset>
    </delete>
  </target>

  <target name="doc" depends="update-doc" description="Build documentation">
    <copy todir="${src.dir}">
      <fileset dir="${out.dir}">
        <patternset refid="pattern.package.doc"/>
      </fileset>
    </copy>
  </target>

</project>