<!-- build configuration -->
<project name="narya tests" default="compile" basedir=".">

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

  <!-- things you may want to change -->
  <property name="junit.fork"     value="true"/>

  <!-- things you probably don't want to change -->
  <property name="test.dir"   value="."/>
  <property name="src.dir"    value="src/java"/>
  <property name="deploy.dir" value="dist"/>
  <property name="lib.name"   value="naryatests"/>

  <property name="cbundle.dir" value="rsrc/bundles/components"/>
  <property name="tbundle.dir" value="rsrc/bundles/tiles"/>

  <!-- this may be changed if you don't have msoy built -->
  <property name="avmthane" value="/export/msoy/dist/lib/avmthane"/>

  <!-- declare our classpath -->
  <property name="classes.dir" value="${deploy.dir}/classes"/>
  <property name="narya.classes.dir" value="../${deploy.dir}/classes"/>
  <path id="classpath">
    <pathelement location="${classes.dir}"/>
    <pathelement location="${narya.classes.dir}"/>
    <fileset dir="../${deploy.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" deprecation="on" source="1.5" target="1.5">
      <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" classpathref="classpath"
             classname="com.threerings.presents.tools.GenServiceTask"/>
    <!-- make sure the service class files are all compiled -->
    <javac srcdir="src/java" destdir="${classes.dir}" debug="on"
           deprecation="on" source="1.5" target="1.5">
      <classpath refid="classpath"/>
      <include name="**/*Service.java"/>
    </javac>
    <!-- now generate the associated files -->
    <service header="../lib/SOURCE_HEADER" classpathref="classpath">
      <fileset dir="src/java" includes="**/*Service.java"/>
    </service>
  </target>

  <!-- generates sender and decoder classes for all invocation -->
  <!-- receiver declarations -->
  <target name="genreceiver">
    <taskdef name="receiver" classpathref="classpath"
             classname="com.threerings.presents.tools.GenReceiverTask"/>
    <!-- make sure the receiver class files are all compiled -->
    <javac srcdir="src/java" destdir="${classes.dir}"
           debug="on" deprecation="on" source="1.5" target="1.5">
      <classpath refid="classpath"/>
      <include name="**/*Receiver.java"/>
      <exclude name="**/InvocationReceiver.java"/>
    </javac>
    <!-- now generate the associated files -->
    <receiver header="../lib/SOURCE_HEADER" classpathref="classpath">
      <fileset dir="src/java" includes="**/*Receiver.java"/>
    </receiver>
  </target>

  <!-- prepares the application directories -->
  <target name="prepare">
    <mkdir dir="${deploy.dir}"/>
    <mkdir dir="${classes.dir}"/>
    <copy todir="${classes.dir}">
      <fileset dir="${src.dir}" includes="**/*.properties"/>
    </copy>
    <copy todir="${classes.dir}/rsrc">
      <fileset dir="rsrc" includes="**/*"/>
    </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.dir}" destdir="${classes.dir}"
      debug="on" optimize="off" deprecation="on">
      <classpath refid="classpath"/>
      <compilerarg value="-Xlint:unchecked"/>
    </javac>
  </target>

  <!-- checks whether our Flash library needs building -->
  <target name="checkaslib">
    <condition property="no_build_aslib"><or>
      <not><available file="${flexsdk.dir}/lib/compc.jar"/></not>
      <and>
        <uptodate targetfile="${deploy.dir}/${lib.name}.swc">
          <srcfiles dir="src/as" includes="**/*.as"/>
          <srcfiles dir="../dist" includes="*.swc"/>
        </uptodate>
        <uptodate targetfile="${deploy.dir}/${lib.name}.abc">
          <srcfiles dir="src/as" includes="**/*.as"/>
          <srcfiles dir="../dist" includes="*.swc"/>
        </uptodate>
      </and>
    </or></condition>
    <available property="extdep.suffix" value="-0.0-SNAPSHOT"
      filepath="../dist/lib" file="thane-0.0-SNAPSHOT.swc"/>
  </target>

  <!-- builds our Flash library -->
  <target name="aslib" unless="no_build_aslib" depends="checkaslib">
    <!-- Generate aslib-config.xml for Flash Player and general compilation -->
    <copy file="etc/thane-config.xml.in" tofile="${deploy.dir}/thane-config.xml">
      <filterset>
        <filter token="flex_sdk_dir" value="${flexsdk.dir}"/>
        <filter token="lib_name" value="${lib.name}"/>
      </filterset>
    </copy>
    <!-- Build Narya tests -->
    <java jar="${flexsdk.dir}/lib/compc.jar" fork="true" failonerror="true">
      <arg value="-load-config"/>
      <arg value="${deploy.dir}/thane-config.xml"/>
      <arg value="-compiler.optimize"/>
      <arg value="-compiler.source-path=src/as/"/>
      <arg value="-compiler.external-library-path"/>
      <arg value="../dist/lib/thane${extdep.suffix}.swc"/>
      <arg value="-compiler.library-path"/>
      <arg value="../dist/thane-env.swc"/>
      <arg value="../dist/naryalib.swc"/>
      <arg value="-output"/>
      <arg value="${deploy.dir}/${lib.name}.swc"/>
      <arg value="-compiler.source-path"/>
      <arg value="src/as/"/>
      <arg value="-include-sources=src/as/com/threerings/bureau/client/TestClient.as"/>
    </java>
    <delete file="${deploy.dir}/aslib-config.xml"/>

    <echo message="Turning .swc into .abc..."/>
    <java outputproperty="dump" classpathref="classpath"
          classname="flash.swf.tools.SwfxPrinter" fork="true" failonerror="true">
      <arg value="-dump"/>
      <arg value="${deploy.dir}/${lib.name}.abc"/>
      <arg value="${deploy.dir}/${lib.name}.swc"/>
    </java>
  </target>

  <!-- run the tests -->
  <property name="test" value=""/>
  <target name="tests" depends="compile" description="Run the tests.">
    <junit printsummary="no" haltonfailure="yes" fork="${junit.fork}">
      <classpath refid="classpath"/>
      <sysproperty key="test_dir" value="${test.dir}"/>
      <sysproperty key="resource_dir" value="${test.dir}/rsrc"/>
      <formatter type="brief" usefile="false"/>
      <batchtest>
        <fileset dir="${src.dir}">
          <include name="**/*${test}Test.java"/>
        </fileset>
      </batchtest>
    </junit>
  </target>

  <!-- checks whether the bureau client needs to be compiled -->
  <target name="bureau-check-thane-client">
    <condition property="no_build_thane_client">
      <uptodate targetfile="${deploy.dir}/BureauTestClient.abc">
        <srcfiles dir="dist" includes="*.swc"/>
        <srcfiles dir="src/thane" includes="**/*.as"/>
      </uptodate>
    </condition>
  </target>

  <!-- builds the bureau thane test client -->
  <target name="bureau-compile-thane-client"
    unless="no_build_thane_client" depends="aslib,bureau-check-thane-client"
    description="Compiles the thane client for testing the bureau library">

    <dirname property="abs.flexsdk.dir" file="${flexsdk.dir}/somefile.txt"/>
    <copy file="etc/thane-config.xml.in" tofile="${deploy.dir}/thane-config.xml">
      <filterset>
        <filter token="flex_sdk_dir" value="${abs.flexsdk.dir}"/>
      </filterset>
    </copy>

    <!-- link the executable -->
    <java jar="${flexsdk.dir}/lib/mxmlc.jar" fork="true" failonerror="true">
      <arg value="-load-config"/>
      <arg value="${deploy.dir}/thane-config.xml"/>
      <arg value="-compiler.external-library-path"/>
      <arg value="../dist/lib/thane${extdep.suffix}.swc"/>
      <arg value="dist/${lib.name}.swc"/>
      <arg value="-compiler.source-path=src/thane"/>
      <arg value="-output"/>
      <arg value="${deploy.dir}/BureauTestClient.swf"/>
      <arg value="src/thane/BureauTestClient.as"/>
    </java>
    <delete file="${deploy.dir}/thane-config.xml"/>

    <echo message="Turning .swf into .abc..."/>
    <java outputproperty="dump" classpathref="classpath"
          classname="flash.swf.tools.SwfxPrinter" fork="true" failonerror="true">
      <arg value="-dump"/>
      <arg value="${deploy.dir}/BureauTestClient.abc"/>
      <arg value="${deploy.dir}/BureauTestClient.swf"/>
    </java>

  </target>

  <target name="bureau-runserver" depends="compile"
    description="Run the bureau test server.">
    <java fork="true" classname="com.threerings.bureau.server.TestServer">
      <classpath refid="classpath"/>
    </java>
  </target>

  <target name="bureau-testregistry" depends="compile"
    description="Run the bureau test server and tests the registry.">
    <java fork="true" classname="com.threerings.bureau.server.RegistryTester">
      <classpath refid="classpath"/>
      <sysproperty key="maxAgents" value="10"/>
      <sysproperty key="numBureaus" value="2"/>
      <sysproperty key="killBureauChance" value="2"/>
      <sysproperty key="maxOps" value="5"/>
      <sysproperty key="createChance" value="70"/>
      <sysproperty key="minDelay" value="1000"/>
      <sysproperty key="maxDelay" value="3000"/>
      <sysproperty key="seed" value="0"/>
      <sysproperty key="clientTarget" value="bureau-runclient"/>
    </java>
  </target>

  <!-- NOTE: this target is launched by the bureau-runserver target, modelling the way bureaus  -->
  <!-- work. As such it is not currently useful on its own -->
  <target name="bureau-runclient" depends="compile"
    description="Run the bureau test client.">
    <java fork="true" classname="com.threerings.bureau.client.TestClient">
      <classpath refid="classpath"/>
      <sysproperty key="serverName" value="${serverName}"/>
      <sysproperty key="serverPort" value="${serverPort}"/>
      <sysproperty key="token" value="${token}"/>
      <sysproperty key="bureauId" value="${bureauId}"/>
    </java>
  </target>

  <!-- runs the thane test client -->
  <target name="bureau-run-thane-client" depends="bureau-compile-thane-client"
	description="Runs the thane client for testing the bureau library">
    <exec executable="${avmthane}">
      <!--arg value="-Dverbose"/-->
      <arg value="dist/naryatests.abc"/>
      <arg value="dist/BureauTestClient.abc"/>
      <arg value="--"/>
      <arg value="${token}"/>
      <arg value="${bureauId}"/>
      <arg value="${serverName}"/>
      <arg value="${serverPort}"/>
    </exec>
  </target>

</project>
