<?xml version="1.0" standalone="yes"?>
<!-- build configuration -->
<project name="narya" default="compile" basedir=".">

  <!-- import overriding properties -->
  <property file="build.properties"/>

  <!-- configuration parameters -->
  <property name="app.name"       value="narya"/>
  <property name="deploy.dir"     value="dist"/>
  <property name="savedoc.dir"    value="docs"/>
  <property name="lib.version"    value="1.0"/>

  <!-- derived properties -->  
  <property name="javadoc.home"   value="${deploy.dir}/docs"/>
  <property name="classes.dir"    value="${deploy.dir}/classes"/>

  <!-- declare our classpath business -->
  <path id="classpath">
    <pathelement location="${classes.dir}"/>
    <fileset dir="lib" includes="**/*.jar"/>
  </path>

  <!-- generates additional methods for distributed object classes -->
  <target name="gendobj" depends="prepare"> 
    <taskdef name="dobj"
             classname="com.threerings.presents.tools.GenDObjectTask"
             classpathref="classpath"/>
    <!-- make sure the dobject class files are all compiled -->
    <javac srcdir="src/java" destdir="${classes.dir}"
           debug="on" optimize="${build.optimize}" deprecation="on"
           source="1.4" target="1.4">
      <classpath refid="classpath"/>
      <include name="**/*Object.java"/>
    </javac>
    <!-- now generate the associated files -->
    <dobj classpathref="classpath">
      <fileset dir="src/java" includes="**/*Object.java"/>
    </dobj>
  </target>

  <!-- generates marshaller and dispatcher classes for all invocation -->
  <!-- service declarations -->
  <target name="genservice"> 
    <taskdef name="service"
             classname="com.threerings.presents.tools.GenServiceTask"
             classpathref="classpath"/>
    <!-- make sure the service class files are all compiled -->
    <javac srcdir="src/java" destdir="${classes.dir}"
           debug="on" optimize="${build.optimize}" deprecation="on"
           source="1.4" target="1.4">
      <classpath refid="classpath"/>
      <include name="**/*Service.java"/>
      <exclude name="**/InvocationService.java"/>
    </javac>
    <!-- now generate the associated files -->
    <service header="lib/SOURCE_HEADER" classpathref="classpath">
      <fileset dir="src/java" includes="**/*Service.java"
        excludes="**/InvocationService.java"/>
    </service>
  </target>

  <!-- generates sender and decoder classes for all invocation
       receiver declarations -->
  <target name="genreceiver">
    <apply executable="bin/genreceiver" failonerror="true" parallel="true">
      <arg value="--classpath"/>
      <arg value="${classes.dir}"/>
      <arg value="--sourcedir"/>
      <arg value="src/java"/>
      <fileset dir="src/java" includes="**/*Receiver.java"
        excludes="**/InvocationReceiver.java"/>
    </apply>
  </target>

  <!-- prepares the application directories -->
  <target name="prepare">
    <mkdir dir="${deploy.dir}"/>
    <mkdir dir="${classes.dir}"/>
    <mkdir dir="${classes.dir}/rsrc"/>
    <mkdir dir="${javadoc.home}"/>
    <copy todir="${classes.dir}">
      <fileset dir="src/java" includes="**/*.properties"/>
      <fileset dir="src/java" includes="**/*.tmpl"/>
    </copy>
    <copy todir="${classes.dir}/rsrc">
      <fileset dir="rsrc" includes="**/*" excludes="**/bundles/**"/>
    </copy>
  </target>

  <!-- cleans out the installed application -->
  <target name="clean">
    <delete dir="${deploy.dir}"/>
  </target>

  <!-- build the java class files -->
  <target name="compile" depends="prepare">
    <javac srcdir="src/java" destdir="${classes.dir}"
           debug="on" optimize="{$build.optimize}" deprecation="on"
           source="1.4" target="1.4">
      <classpath refid="classpath"/>
      <exclude name="**/OggPlayer.java"/>
      <exclude name="**/ModPlayer.java"/>
      <exclude name="**/MidiPlayer.java"/>
      <exclude name="**/Mp3Player.java"/>
    </javac>
  </target>

  <!-- build the native libraries -->
  <target name="ncompile" depends="prepare">
    <exec dir="src/java/com/threerings/util/keybd/i686-Linux" 
     executable="make"><arg line="install"/></exec>
    <exec dir="src/java/com/threerings/util/signal/i686-Linux" 
     executable="make"><arg line="install"/></exec>
    <exec dir="src/java/com/threerings/util/unsafe/i686-Linux" 
     executable="make"><arg line="install"/></exec>
  </target>

  <!-- build the javadoc documentation -->
  <target name="javadoc" depends="prepare">
    <javadoc sourcepath="src/java"
             packagenames="com.threerings.*"
             destdir="${javadoc.home}"
             additionalparam="-breakiterator"
             link="http://dev.threerings.net/devel/docs/narya/">
      <classpath refid="classpath"/>
      <link href="http://java.sun.com/j2se/1.4/docs/api/"/>
      <link href="http://dev.threerings.net/devel/docs/samskivert/"/>
    </javadoc>
    <copy todir="${javadoc.home}">
      <fileset dir="src/java" includes="**/*.png"/>
    </copy>
  </target>

  <!-- builds the javadocs and stuffs them in a directory where they won't
       be blown away when we do "clean" next time -->
  <target name="savedoc" depends="javadoc">
    <delete dir="${savedoc.dir}/api"/>
    <copy todir="${savedoc.dir}/api">
      <fileset dir="${javadoc.home}" includes="**/*"/>
    </copy>
  </target>

  <!-- a target for rebuilding everything -->
  <target name="all" depends="clean,prepare,compile,ncompile,javadoc,dist"/>

  <!-- builds our distribution files (war and jar) -->
  <target name="dist" depends="prepare,compile,ncompile">
    <!-- build our various jar files -->
    <jar destfile="${deploy.dir}/${app.name}-base.jar">
      <fileset dir="${classes.dir}" includes="com/threerings/io/**"/>
      <fileset dir="${classes.dir}" includes="com/threerings/util/**"/>
      <fileset dir="${classes.dir}" includes="com/threerings/resource/**"/>
      <fileset dir="${classes.dir}" includes="com/threerings/tools/**"/>
    </jar>
    <jar destfile="${deploy.dir}/${app.name}-media.jar">
      <fileset dir="${classes.dir}" includes="com/threerings/geom/**"/>
      <fileset dir="${classes.dir}" includes="com/threerings/media/**"/>
    </jar>
    <jar destfile="${deploy.dir}/${app.name}-distrib.jar">
      <fileset dir="${classes.dir}" includes="com/threerings/presents/**"/>
      <fileset dir="${classes.dir}" includes="com/threerings/crowd/**"/>
      <fileset dir="${classes.dir}" includes="com/threerings/admin/**"/>
      <fileset dir="${classes.dir}" includes="rsrc/config/presents/**"/>
      <fileset dir="${classes.dir}" includes="rsrc/config/crowd/**"/>
    </jar>
    <jar destfile="${deploy.dir}/${app.name}-parlor.jar">
      <fileset dir="${classes.dir}" includes="com/threerings/parlor/**"/>
    </jar>
    <jar destfile="${deploy.dir}/${app.name}-puzzle.jar">
      <fileset dir="${classes.dir}" includes="com/threerings/puzzle/**"/>
    </jar>
    <jar destfile="${deploy.dir}/${app.name}-micasa.jar">
      <fileset dir="${classes.dir}" includes="com/threerings/micasa/**"/>
      <fileset dir="${classes.dir}" includes="rsrc/config/micasa/**"/>
      <fileset dir="${classes.dir}" includes="rsrc/i18n/micasa*"/>
    </jar>
    <jar destfile="${deploy.dir}/${app.name}-whirled.jar">
      <fileset dir="${classes.dir}" includes="com/threerings/whirled/**"/>
      <fileset dir="${classes.dir}" includes="rsrc/config/whirled/**"/>
    </jar>
    <jar destfile="${deploy.dir}/${app.name}-miso.jar">
      <fileset dir="${classes.dir}" includes="com/threerings/miso/**"/>
      <fileset dir="${classes.dir}" includes="rsrc/config/miso/**"/>
    </jar>
    <jar destfile="${deploy.dir}/${app.name}-cast.jar">
      <fileset dir="${classes.dir}" includes="com/threerings/cast/**"/>
    </jar>
  </target>

  <!-- generate a class hierarchy diagram -->
  <target name="hierviz" depends="prepare,compile">
    <taskdef name="viztool" classname="com.samskivert.viztool.DriverTask"/>
    <viztool visualizer="com.samskivert.viztool.hierarchy.HierarchyVisualizer"
      pkgroot="com.threerings.miso" classes="com.threerings.miso.*"
      output="hierviz.ps">
      <classpath refid="classpath"/>
    </viztool>
  </target>

  <!-- generate a class hierarchy diagram -->
  <target name="sumgen" depends="prepare,compile">
    <taskdef name="viztool" classname="com.samskivert.viztool.DriverTask"/>
    <viztool visualizer="com.samskivert.viztool.summary.SummaryVisualizer"
      pkgroot="com.threerings.miso"
      classes="com.threerings.miso.*Scene.*"
      output="summary.ps">
      <classpath refid="base.classpath"/>
    </viztool>
  </target>

  <!-- creates a tarball and zipfile for source distribution -->
  <target name="distrib">
     <echo message="You may want to stop and run 'ant savedoc' first."/>
     <echo message="Building ${lib.version} tar.gz distribution..."/>
     <tar destfile="narya-${lib.version}.tar.gz" compression="gzip">
       <tarfileset dir=".." mode="0775" dirmode="0775">
         <include name="narya/bin/**"/>
         <include name="narya/tests/bin/**"/>
         <exclude name="**/CVS"/>
       </tarfileset>
       <tarfileset dir=".." mode="0664" dirmode="0775">
         <include name="narya/**"/>
         <exclude name="**/CVS"/>
         <exclude name="narya/bin/**"/>
         <exclude name="narya/tests/bin/**"/>
         <exclude name="narya/dist/**"/>
         <exclude name="narya/code/**"/>
         <exclude name="narya/narya-*.*"/>
       </tarfileset>
     </tar>
     <echo message="Building ${lib.version} zip distribution..."/>
     <zip destfile="narya-${lib.version}.zip">
       <fileset dir="..">
         <include name="narya/**"/>
         <exclude name="**/CVS"/>
         <exclude name="narya/dist/**"/>
         <exclude name="narya/code/**"/>
         <exclude name="narya/narya-*.*"/>
       </fileset>
     </zip>
  </target>

</project>
