<project name="MEMESuite" default="compile" basedir=".">
  <description>
    A build file for the website components of the MEME Suite.
    This is responsible for creating the meme war file.
  </description>

  <property file="MemeSuite.properties"/>
    <echo message="catalina home ${catalina.home}"/>
    <echo message="catalina base ${catalina.base}"/>

  <target name="check_prop">
    <fail message="Make sure that catalina.home points to a valid Tomcat installation">
      <condition>
        <not>
          <and>
            <isset property="catalina.home"/>
            <length string="${catalina.home}" when="greater" length="0"/>
            <available file="${catalina.home}/webapps" type="dir"/>
            <available file="${catalina.home}/lib" type="dir"/>
          </and>
        </not>
      </condition>
    </fail>
  </target>

  <!-- list of libraries -->
  <path id="all.libs">
    <fileset dir="@MYHOME@/website/lib">
      <include name="**/*.jar"/>
    </fileset>
    <fileset file="${catalina.home}/lib/servlet-api.jar"/>
    <fileset file="${catalina.home}/lib/catalina.jar"/>
  </path>

  <path id="updater.libs">
    <fileset dir="@MYHOME@/website/lib">
      <include name="axis.jar"/> <!-- used by RSATWS -->
      <include name="commons-discovery-0.5.jar"/> <!-- used by commons-logging -->
      <include name="commons-logging-1.1.3.jar"/> <!-- used by axis -->
      <include name="commons-io-2.4.jar"/> <!-- various file IO functions used -->
      <include name="commons-net-3.3.jar"/> <!-- ftp -->
      <include name="javamail-1.4.3.jar"/> <!-- used by axis (I think) -->
      <include name="jaxrpc.jar"/> <!-- remote procedure calls -->
      <include name="JSAP-2.1.jar"/> <!-- command line processing -->
      <include name="jsoup-1.7.3.jar"/> <!-- html scraping -->
      <include name="sqlite-jdbc-3.7.2.jar"/> <!-- database -->
      <include name="jtar-1.1.jar"/> <!-- untar files -->
      <include name="RSATWS.jar"/> <!-- RSA Tools webservices -->
      <include name="wsdl4j-1.6.3.jar"/> <!-- used by RSATWS -->
    </fileset>
  </path>

  <pathconvert property="updater.classpath" pathsep=" ">
    <path refid="updater.libs"/>
    <flattenmapper/>
  </pathconvert>

  <target name="compile" depends="check_prop" description="compile the source">
    <javac srcdir="@MYHOME@/website/src" debug="true" includeantruntime="false">
      <compilerarg value="-Xlint:unchecked"/>
      <classpath refid="all.libs"/>
    </javac>
  </target>

  <!-- The release distribution does not include the unit tests because
       they need bulky files we don't want to keep around. -->
  <target name="check_for_tests">
    <available file="@MYHOME@/website/src/au/edu/uq/imb/memesuite/tests/TestAll.class"
      type="file" property="testable"/>
  </target>

  <target name="test" depends="compile,check_for_tests" if="testable">
    <java classname="au.edu.uq.imb.memesuite.tests.TestAll" failonerror="true">
      <classpath>
        <path refid="all.libs"/>
        <pathelement location="@MYHOME@/website/src"/>
      </classpath>
    </java>
  </target>

  <target name="clean_war" description="remove war file" if="version">
    <delete file="meme_${version}.war"/>
  </target>

  <target name="clean" depends="clean_war" description="remove built files">
    <delete verbose="true">
      <fileset dir="@MYHOME@/website/src">
        <include name="**/*.class"/>
      </fileset>
    </delete>
  </target>

  <target name="build_sequencedb_updater" depends="compile">
    <available file="MemeSuite.properties" type="file" property="configured"/>
    <fail unless="configured" message="Can not build jar as the MemeSuite.properties file has not been generated. Please run make."/>
    <jar destfile="updatedb.jar">
      <fileset file="MemeSuite.properties"/>
      <fileset dir="@MYHOME@/website/src">
        <include name="au/edu/uq/imb/memesuite/data/Alph.class"/>
        <include name="au/edu/uq/imb/memesuite/data/Alph$*.class"/>
        <include name="au/edu/uq/imb/memesuite/data/AlphStd.class"/>
        <include name="au/edu/uq/imb/memesuite/data/AlphStd$*.class"/>
        <include name="au/edu/uq/imb/memesuite/data/SequenceInfo.class"/>
        <include name="au/edu/uq/imb/memesuite/data/SequenceInfo$*.class"/>
        <include name="au/edu/uq/imb/memesuite/db/*.class"/>
        <include name="au/edu/uq/imb/memesuite/io/alph/*.class"/>
        <include name="au/edu/uq/imb/memesuite/updatedb/*.class"/>
        <include name="au/edu/uq/imb/memesuite/updatedb/*.properties"/>
        <include name="au/edu/uq/imb/memesuite/util/GlobFilter.class"/>
        <include name="au/edu/uq/imb/memesuite/util/GlobFilter$*.class"/>
        <include name="au/edu/uq/imb/memesuite/util/JsonWr.class"/>
        <include name="au/edu/uq/imb/memesuite/util/JsonWr$*.class"/>
        <include name="au/edu/uq/imb/memesuite/util/MultiSourceStatus.class"/>
        <include name="au/edu/uq/imb/memesuite/util/MultiSourceStatus$*.class"/>
        <include name="au/edu/uq/imb/memesuite/util/Progress.class"/>
        <include name="au/edu/uq/imb/memesuite/util/Progress$*.class"/>
        <include name="au/edu/uq/imb/memesuite/util/SemanticVersion.class"/>
        <include name="au/edu/uq/imb/memesuite/util/SemanticVersion$*.class"/>
      </fileset>
      <manifest>
        <attribute name="Main-Class" value="au.edu.uq.imb.memesuite.updatedb.UpdateSequenceDB"/>
        <attribute name="Class-Path" value="${updater.classpath}"/>
      </manifest>
    </jar>
  </target>

  <target name="fasta-indexer" depends="compile">
    <available file="MemeSuite.properties" type="file" property="configured"/>
    <fail unless="configured" message="Can not build jar as the MemeSuite.properties file has not been generated. Please run make."/>
    <jar destfile="fasta-indexer.jar">
      <fileset file="MemeSuite.properties"/>
      <fileset dir="@MYHOME@/website/src">
        <include name="au/edu/uq/imb/memesuite/data/SequenceInfo.class"/>
        <include name="au/edu/uq/imb/memesuite/data/SequenceInfo$*.class"/>
        <include name="au/edu/uq/imb/memesuite/db/*.class"/>
        <include name="au/edu/uq/imb/memesuite/io/alph/*.class"/>
        <include name="au/edu/uq/imb/memesuite/updatedb/*.class"/>
        <include name="au/edu/uq/imb/memesuite/updatedb/*.properties"/>
        <include name="au/edu/uq/imb/memesuite/util/GlobFilter.class"/>
        <include name="au/edu/uq/imb/memesuite/util/GlobFilter$*.class"/>
      </fileset>
      <manifest>
        <attribute name="Main-Class" value="au.edu.uq.imb.memesuite.updatedb.FastaIndexer"/>
        <attribute name="Class-Path" value="${updater.classpath}"/>
      </manifest>
    </jar>
  </target>

  <target name="build_war" depends="compile">
    <available file="MemeSuite.properties" type="file" property="configured"/>
    <fail unless="configured" message="Can not build jar as the MemeSuite.properties file has not been generated. Please run make."/>
    <!-- 
      We have to modify one of the script files under the doc directory for use on the server.
      Create a temp directory and copy and combine the contents of the doc directory
      with the modified version.
    -->
    <tempfile property="temp.dir" destDir="${java.io.tmpdir}" prefix="."/>
    <mkdir dir="${temp.dir}"/>
    <copy todir="${temp.dir}">
      <fileset dir=".">
        <include name="doc/*.pdf"/>
        <include name="doc/*.html"/>
        <include name="doc/js/*.js"/>
        <exclude name="doc/js/menu-path.js"/>
        <include name="doc/css/*.css"/>
        <include name="doc/images/*.png"/>
        <include name="doc/images/*.jpg"/>
        <include name="doc/images/*.gif"/>
        <include name="doc/images/*.ico"/>
        <include name="doc/examples/*.glam2"/>
        <include name="doc/examples/*.meme"/>
        <include name="doc/examples/*.plib"/>
        <include name="doc/examples/*.txt"/>
        <include name="doc/examples/example-datasets/crp0.fna"/>
        <include name="doc/examples/example-datasets/Klf1.fna"/>
        <include name="doc/examples/example-datasets/At.faa"/>
        <include name="doc/examples/example-datasets/lex0.fna"/>
        <include name="doc/examples/example-datasets/dpinteract.meme"/>
        <include name="doc/examples/example-datasets/some_vertebrates.meme"/>
        <include name="doc/examples/example-datasets/Klf1.meme"/>
        <include name="doc/examples/example-datasets/dpinteract_subset.meme"/>
        <include name="doc/examples/example-datasets/mm9_tss_500bp_sampled_1000.fna"/>
        <include name="doc/examples/example-datasets/P300.chr21.bed"/>
        <include name="doc/examples/example-datasets/adh.meme"/>
        <include name="doc/examples/example-datasets/adh.faa"/>
        <include name="doc/examples/example-datasets/crp0.meme"/>
        <include name="doc/examples/example-datasets/At.glam2"/>
        <include name="doc/examples/example-datasets/STRGGTCAN.meme"/>
        <include name="doc/examples/example-datasets/"/>
        <include name="doc/examples/**_files/*"/>
        <include name="doc/examples/**_files/**/*"/>
      </fileset>
    </copy>
    <!-- Copy the server version of the menu-path.js file -->
    <copy todir="${temp.dir}/doc/js">
      <fileset dir="@MYHOME@/website/js">
        <include name="menu-path.js"/>
      </fileset>
    </copy>
    <war destfile="meme_${version}.war" webxml="@MYHOME@/website/web.xml">
      <lib dir="@MYHOME@/website/lib">
        <include name="*.*"/>
        <exclude name="sqlite-jdbc*"/>
      </lib>
      <classes dir="@MYHOME@/website/src">
        <include name="**/*.class"/>
        <include name="**/*.properties"/>
        <exclude name="**/tests/*.*"/>
      </classes>
      <classes file="MemeSuite.properties"/>
      <webinf dir="@MYHOME@/website">
        <include name="templates/*.*"/>
      </webinf>
      <fileset dir="@MYHOME@/website">
        <include name="*.html"/>
        <include name="js/*.js"/>
        <exclude name="js/menu-path.js"/>
        <include name="css/*.css"/>
        <include name="images/*.png"/>
        <include name="images/*.jpg"/>
      </fileset>
      <fileset dir="${temp.dir}">
        <include name="doc/*.pdf"/>
        <include name="doc/*.html"/>
        <include name="doc/js/*.js"/>
        <include name="doc/css/*.css"/>
        <include name="doc/images/*.png"/>
        <include name="doc/images/*.jpg"/>
        <include name="doc/images/*.gif"/>
        <include name="doc/images/*.ico"/>
        <include name="doc/examples/*.glam2"/>
        <include name="doc/examples/*.meme"/>
        <include name="doc/examples/*.plib"/>
        <include name="doc/examples/*.txt"/>
        <include name="doc/examples/example-datasets/crp0.fna"/>
        <include name="doc/examples/example-datasets/Klf1.fna"/>
        <include name="doc/examples/example-datasets/At.faa"/>
        <include name="doc/examples/example-datasets/lex0.fna"/>
        <include name="doc/examples/example-datasets/dpinteract.meme"/>
        <include name="doc/examples/example-datasets/some_vertebrates.meme"/>
        <include name="doc/examples/example-datasets/Klf1.meme"/>
        <include name="doc/examples/example-datasets/dpinteract_subset.meme"/>
        <include name="doc/examples/example-datasets/mm9_tss_500bp_sampled_1000.fna"/>
        <include name="doc/examples/example-datasets/P300.chr21.bed"/>
        <include name="doc/examples/example-datasets/adh.meme"/>
        <include name="doc/examples/example-datasets/adh.faa"/>
        <include name="doc/examples/example-datasets/crp0.meme"/>
        <include name="doc/examples/example-datasets/At.glam2"/>
        <include name="doc/examples/example-datasets/STRGGTCAN.meme"/>
        <include name="doc/examples/**_files/*"/>
        <include name="doc/examples/**_files/**/*"/>
      </fileset>
    </war>
    <delete dir="${temp.dir}"/>
  </target>

  <target name="build" depends="build_war,build_sequencedb_updater,fasta-indexer"/>

  <target name="install" depends="build,check_prop">
    <!-- install the libraries needed by the updatedb.jar -->
    <copy todir="${lib.dir}/java" preservelastmodified="true">
      <path refid="updater.libs"/>
    </copy>
    <!-- install the updatedb.jar -->
    <copy file="updatedb.jar" todir="${lib.dir}/java" preservelastmodified="true"/>
    <!-- install the fasta-indexer.jar -->
    <copy file="fasta-indexer.jar" todir="${lib.dir}/java" preservelastmodified="true"/>
    <!-- install the SQLite library -->
    <copy todir="${catalina.base}/lib" preservelastmodified="true">
      <fileset dir="@MYHOME@/website/lib">
        <include name="sqlite-jdbc-*.jar"/>
      </fileset>
    </copy>
    <!-- install the WAR file -->
    <copy file="meme_${version}.war" todir="${catalina.base}/webapps" preservelastmodified="true"/>
  </target>


</project>
