<project name="text" default="build">
  <property name="lib.dir" value="lib"/>
  <property name="build.dir" value="build"/>
  <property name="scrub.dir" value="scrub"/>

  <property name="spextract-queue" value="SPEXTRACT_NEW_QUEUE"/>
  <property name="sectioner-queue" value="SECTIONER_QUEUE"/>
  <property name="modulo" value="-1"/>

  <path id="class.path">
    <pathelement location="${build.dir}"/>
    <pathelement location="${lib.dir}"/>
    <fileset dir="${lib.dir}">
      <include name="**/*.jar"/>
      <include name="**/*.zip"/>
    </fileset>
    <pathelement location="../nls/mmtx/lib/mmtxProject.jar"/>
    <pathelement location="../nls/mmtx"/>
    <pathelement location="../nls/mmtx/config"/>
  </path>

  <target name="prepare" description="set up necessary directories and configuration files">
    <mkdir dir="${build.dir}"/>
    <copy file="${scrub.dir}/scrub.properties" todir="${build.dir}"/>
  </target>

  <target name="build-nommtx" depends="prepare">
    <ant dir="util" target="build">
      <property name="build.dir" value="../${build.dir}"/>
      <property name="lib.dir" value="../${lib.dir}"/>
      <reference refid="class.path"/>
    </ant>
    <ant dir="scrub" target="build" inheritRefs="true">
      <property name="build.dir" value="../${build.dir}"/>
      <property name="lib.dir" value="../${lib.dir}"/>
      <reference refid="class.path"/>
    </ant>
  </target>

  <target name="build" depends="prepare, build-nommtx">
    <ant dir="mmtx" target="build" inheritRefs="true">
      <property name="build.dir" value="../${build.dir}"/>
      <property name="lib.dir" value="../${lib.dir}"/>
      <reference refid="class.path"/>
    </ant>
  </target>

  <target name="clean">
    <delete dir="${build.dir}"/>
    <ant dir="util" target="clean"/>
    <ant dir="scrub" target="clean"/>
  </target>

  <target name="reportdump" description="dump reports" depends="build-nommtx">
    <java fork="yes" classname="net.sf.saxon.Transform" taskname="saxon" failonerror="true" classpathref="class.path">
       <arg value="-x"/><arg value="org.apache.xml.resolver.tools.ResolvingXMLReader"/>
       <arg value="-y"/><arg value="org.apache.xml.resolver.tools.ResolvingXMLReader"/>
       <arg value="-r"/><arg value="org.apache.xml.resolver.tools.CatalogResolver"/>
       <arg value="reportdump/reportdump-sample.xml"/>
       <arg value="reportdump/reportdump.xsl"/>
       <arg value="username=${username}"/>
       <arg value="password=${password}"/>
    </java>
  </target>

  <target name="reportdump-crowley" description="dump reports from crowley sample" depends="build-nommtx">
    <java fork="yes" classname="net.sf.saxon.Transform" taskname="saxon" failonerror="true" classpathref="class.path">
       <arg value="-x"/><arg value="org.apache.xml.resolver.tools.ResolvingXMLReader"/>
       <arg value="-y"/><arg value="org.apache.xml.resolver.tools.ResolvingXMLReader"/>
       <arg value="-r"/><arg value="org.apache.xml.resolver.tools.CatalogResolver"/>
       <arg value="reportdump/reportdump-crowley.xml"/>
       <arg value="reportdump/reportdump-scrub.xsl"/>
       <arg value="username=${username}"/>
       <arg value="password=${password}"/>
    </java>
  </target>

  <target name="spextract.xsl" description="generate the spextract parser" depends="build">
    <java fork="yes" classname="org.regenstrief.util.TRAXPipe" taskname="traxpipe" failonerror="true" classpathref="class.path">
       <arg value="-x"/><arg value="org.apache.xml.resolver.tools.ResolvingXMLReader"/>
       <arg value="-y"/><arg value="org.apache.xml.resolver.tools.ResolvingXMLReader"/>
       <arg value="-r"/><arg value="org.apache.xml.resolver.tools.CatalogResolver"/>
       <arg value="-o"/><arg value="${build.dir}/spextract.xsl"/>
       <arg value="-i"/><arg value="spextract/spextract-mmtx.gut"/>
       <arg value="gut/gutpp.xsl"/>
       <arg value="gut/gut2xsl.xsl"/>
       <arg value="options=ucla,backtrack"/>
    </java>
  </target>

  <target name="spextract" description="dump reports and parse specimens" depends="build, spextract.xsl">
    <java fork="yes" classname="net.sf.saxon.Transform" taskname="saxon" failonerror="true" classpathref="class.path">
       <arg value="-fullregex"/>
       <arg value="-x"/><arg value="org.apache.xml.resolver.tools.ResolvingXMLReader"/>
       <arg value="-y"/><arg value="org.apache.xml.resolver.tools.ResolvingXMLReader"/>
       <arg value="-r"/><arg value="org.apache.xml.resolver.tools.CatalogResolver"/>
       <arg value="reportdump/reportdump-sample.xml"/>
       <arg value="spextract/reportdump-spextract.xsl"/>
       <arg value="username=${username}"/>
       <arg value="password=${password}"/>
       <arg value="prequeue=${spextract-queue}"/>
       <arg value="modulo=${modulo}"/>
       <arg value="check=yes"/>
    </java>
  </target>

  <target name="spextract-dataload" description="load specimens and observations from parsed reports into database" depends="build-nommtx">
    <java fork="yes" classname="net.sf.saxon.Transform" taskname="saxon" failonerror="true" classpathref="class.path">
       <arg value="-fullregex"/>
       <arg value="-x"/><arg value="org.apache.xml.resolver.tools.ResolvingXMLReader"/>
       <arg value="-y"/><arg value="org.apache.xml.resolver.tools.ResolvingXMLReader"/>
       <arg value="-r"/><arg value="org.apache.xml.resolver.tools.CatalogResolver"/>
       <arg value="spextract/spextract-dataload.xml"/>
       <arg value="spextract/spextract-dataload.xsl"/>
       <arg value="username=${username}"/>
       <arg value="password=${password}"/>
    </java>
  </target>

  <target name="sectioner.xsl" description="generate the sections parser" depends="build-nommtx">
    <java fork="yes" classname="org.regenstrief.util.TRAXPipe" taskname="traxpipe" failonerror="true" classpathref="class.path">
       <arg value="-x"/><arg value="org.apache.xml.resolver.tools.ResolvingXMLReader"/>
       <arg value="-y"/><arg value="org.apache.xml.resolver.tools.ResolvingXMLReader"/>
       <arg value="-r"/><arg value="org.apache.xml.resolver.tools.CatalogResolver"/>
       <arg value="-o"/><arg value="${build.dir}/sectioner.xsl"/>
       <arg value="-i"/><arg value="sectioner/sectioner.gut"/>
       <arg value="gut/gutpp.xsl"/>
       <arg value="gut/gut2xsl.xsl"/>
    </java>
  </target>

  <target name="sectioner" description="dump reports, parse sections and deidentify" depends="build-nommtx, sectioner.xsl">
    <java fork="yes" classname="net.sf.saxon.Transform" taskname="saxon" failonerror="true" classpathref="class.path">
       <arg value="-x"/><arg value="org.apache.xml.resolver.tools.ResolvingXMLReader"/>
       <arg value="-y"/><arg value="org.apache.xml.resolver.tools.ResolvingXMLReader"/>
       <arg value="-r"/><arg value="org.apache.xml.resolver.tools.CatalogResolver"/>
       <arg value="reportdump/reportdump-sample.xml"/>
       <arg value="sectioner/reportdump-sectioner.xsl"/>
       <arg value="username=${username}"/>
       <arg value="password=${password}"/>
       <arg value="prequeue=${sectioner-queue}"/>
       <arg value="modulo=${modulo}"/>
       <arg value="check=yes"/>
       <arg value="force=yes"/>
    </java>
  </target>

</project>
