<!--
build.xml

Ant build file for common sphinx targets.
Download Apache Ant from http://ant.apache.org/.
Type "ant -p" for a list of targets.
-->

<project name="rules" default="html" basedir=".">
  <description>Common sphinx targets</description>
  <dirname property="imported.basedir" file="${ant.file.rules}"/>
  <import file="${imported.basedir}/sphinx.xml"/>

  <!-- Ant-Contrib Tasks: http://ant-contrib.sourceforge.net/ -->
  <taskdef resource="net/sf/antcontrib/antcontrib.properties"
    classpath="${imported.basedir}/ant-contrib-1.0b3.jar"/>
  <!-- HACK - some environments do not install the antcontrib tasks properly
    from antcontrib.properties above; also load them from antlib.xml -->
  <taskdef resource="net/sf/antcontrib/antlib.xml"
    classpath="${imported.basedir}/ant-contrib-1.0b3.jar"/>

  <target name="init">
    <property name="omero.shortversion" value=""/>
    <if>
      <equals arg1="omero.shortversion" arg2=""/>
      <then>
        <property name="source.branch" value="v.${omero.shortversion}"/>
      </then>
    </if>
    <property name="source.branch" value="develop"/>
    <property name="source.user" value="openmicroscopy"/>
    <property name="omero.release" value="${omero.shortversion}"/>
    <property name="formats.release" value="${omero.shortversion}"/>
  </target>

  <macrodef name="sphinx" description="Run sphinx-build">
    <attribute name="buildtype" default="html"/>
    <attribute name="srcdir" default="."/>
    <attribute name="destdir" default=""/>
    <attribute name="opts" default=""/>
    <attribute name="warnopts" default=""/>
    <sequential>
      <exec executable="${sphinx.build}" failonerror="true">
        <arg value="-D"/>
        <arg value="release=${omero.release}"/>
        <arg value="-D"/>
        <arg value="version=${omero.shortversion}"/>
        <arg value="-b"/>
        <arg value="@{buildtype}"/>
        <arg line="@{opts}"/>
        <arg line="@{warnopts}"/>
        <arg value="@{srcdir}"/>
        <arg value="@{destdir}"/>
      </exec>
    </sequential>
  </macrodef>
  <target name="sphinx-deps"/>
  <target name="clean-deps"/>

  <target name="html" depends="sphinx-deps, init">
        <sphinx buildtype="html"
            srcdir="."
            destdir="${sphinx.builddir}/html"
            opts="${sphinx.opts}"/>
  </target>

  <target name="latexpdf" depends="sphinx-deps, init">
    <sphinx buildtype="latex"
            srcdir="."
            destdir="${sphinx.builddir}/latex"
            opts="${sphinx.opts}"
            warnopts="${sphinx.warnopts}"/>

    <for param="file">
      <path>
       <fileset dir="${sphinx.builddir}/latex" includes="*.tex"/>
      </path>
      <sequential>
        <runlatex file="@{file}"/>
      </sequential>
    </for>
    <copy file="_build/latex/omero.pdf" tofile="${dist.dir}/docs/omero-${omero.release}.pdf"/>
  </target>

  <target name="pdf" depends="latexpdf"/>

  <target name="linkcheck" depends="sphinx-deps, init">
    <sphinx buildtype="linkcheck" srcdir="."
            destdir="${sphinx.builddir}/linkcheck"
            opts="${sphinx.opts}"
            warnopts="${sphinx.warnopts}"/>
  </target>

  <target name="clean" depends="clean-deps">
    <delete dir="${sphinx.builddir}"/>
  </target>

</project>
