<!-- -*- xml -*- -->
<!-- please leave the top comment for us emacs folks -->
<property name="nunitcmd" value="nunit-console" />

<!-- This target produces a source distribution of OpenSimulator -->
<!-- TODO: A few parameters still need to be tweaked after running this - need to do this automatically with sed or similar -->
<target name="distsrc">
  <copy file="bin/OpenSim.ini.example" tofile="bin/OpenSim.ini"/>
  <copy file="bin/config-include/StandaloneCommon.ini.example" tofile="bin/config-include/StandaloneCommon.ini"/>
  <copy file="bin/config-include/FlotsamCache.ini.example" tofile="bin/config-include/FlotsamCache.ini"/>
  <!-- delete files generated by runprebuild.sh which had to be run in order to generate the build file for this target-->
  <delete>
    <fileset basedir="OpenSim">
      <include name="**/*.build"/>
      <include name="**/*.csproj*"/>
      <include name="**/*.dll.build"/>
      <include name="**/*.pidb"/>
      <exclude name="Tools/OpenSim.32BitLaunch/**"/>
      <exclude name="Tools/Robust.32BitLaunch/**"/>
      <exclude name="Tools/LaunchSLClient/**"/>
    </fileset>
  </delete>
  <delete>
    <fileset>
      <include name="OpenSim.build"/>
      <include name="OpenSim.sln"/>
    </fileset>
  </delete>
</target>

<property name="distbindir" value="distbin" />
<!-- This target produces a binary directory called distbin/ in OpenSim/bin which contains everything needed for binary distribution -->
<!-- For safety/laziness sake, we're going to take the approach of deleting known extraneous files here rather than
     trying to copy across only the essential ones -->
<target name="distbin">
  <delete dir="${distbindir}"/>
  <copy todir="${distbindir}">
    <fileset>
      <include name="**"/>
    </fileset>
  </copy>
  <delete dir="${distbindir}/OpenSim"/>
  <delete dir="${distbindir}/Prebuild"/>
  <delete dir="${distbindir}/%temp%"/>
  <delete dir="${distbindir}/.nant"/>
  <delete>
    <fileset basedir="${distbindir}">
      <include name="compile.bat"/>
      <include name="BUILDING.txt"/>
      <include name="Makefile"/>
      <include name="nant-color"/>
      <include name="OpenSim.*"/>
      <include name="prebuild.xml"/>
      <include name="runprebuild*"/>
      <include name="TESTING.txt"/>
      <include name="TestResult.xml"/>
      <include name="bin/OpenSim.Server.ini"/>
      <include name="bin/Regions/Regions.ini"/>
      <include name="bin/*.db"/>
      <include name="**/.git/**"/>
      <include name=".gitignore"/>
      <include name=".hgignore"/>
    </fileset>
  </delete>
</target>

<target name="test" depends="build, find-nunit">
  <setenv name="MONO_THREADS_PER_CPU" value="100" />

  <!-- Unit Test Assembly -->
  <!-- if you want to add more unit tests it's important that you add
  the assembly here as an exec, and you add the fail clause later.
  This lets all the unit tests run and tells you if they fail at the
  end, instead of stopping short -->
  <exec program="${nunitcmd}" failonerror="true" resultproperty="testresult.opensim.tests">
    <arg value="./bin/OpenSim.Tests.dll" />
  </exec>
  <fail message="Failures reported in unit tests." unless="${int::parse(testresult.opensim.tests)==0}" /> 

  <exec program="${nunitcmd}" failonerror="true" resultproperty="testresult.opensim.framework.tests">
    <arg value="./bin/OpenSim.Framework.Tests.dll" />
  </exec>
  <fail message="Failures reported in unit tests." unless="${int::parse(testresult.opensim.framework.tests)==0}" /> 

  <exec program="${nunitcmd}" failonerror="true" resultproperty="testresult.opensim.framework.servers.tests">
    <arg value="./bin/OpenSim.Framework.Servers.Tests.dll" />
  </exec>
  <fail message="Failures reported in unit tests." unless="${int::parse(testresult.opensim.framework.servers.tests)==0}" /> 

  <exec program="${nunitcmd}" failonerror="true" resultproperty="testresult.opensim.framework.serialization.tests">
    <arg value="./bin/OpenSim.Framework.Serialization.Tests.dll" />
  </exec>
  <fail message="Failures reported in unit tests." unless="${int::parse(testresult.opensim.framework.serialization.tests)==0}" /> 

  <exec program="${nunitcmd}" failonerror="true" resultproperty="testresult.opensim.region.clientstack.lindencaps.tests">
    <arg value="./bin/OpenSim.Region.ClientStack.LindenCaps.Tests.dll" />
  </exec>
  <fail message="Failures reported in unit tests." unless="${int::parse(testresult.opensim.region.clientstack.lindencaps.tests)==0}" /> 

  <exec program="${nunitcmd}" failonerror="true" resultproperty="testresult.opensim.region.clientstack.lindenudp.tests">
    <arg value="./bin/OpenSim.Region.ClientStack.LindenUDP.Tests.dll" />
  </exec>
  <fail message="Failures reported in unit tests." unless="${int::parse(testresult.opensim.region.clientstack.lindenudp.tests)==0}" /> 

  <exec program="${nunitcmd}" failonerror="true" resultproperty="testresult.opensim.region.scriptengine.tests">
    <arg value="./bin/OpenSim.Region.ScriptEngine.Tests.dll" />
  </exec>
  <fail message="Failures reported in unit tests." unless="${int::parse(testresult.opensim.region.scriptengine.tests)==0}" /> 

  <exec program="${nunitcmd}" failonerror="true" resultproperty="testresult.opensim.region.coremodules.tests">
    <arg value="./bin/OpenSim.Region.CoreModules.Tests.dll" />
  </exec>
  <fail message="Failures reported in unit tests." unless="${int::parse(testresult.opensim.region.coremodules.tests)==0}" /> 

  <exec program="${nunitcmd}" failonerror="true" resultproperty="testresult.opensim.region.optionalmodules.tests">
    <arg value="./bin/OpenSim.Region.OptionalModules.Tests.dll" />
  </exec>
  <fail message="Failures reported in unit tests." unless="${int::parse(testresult.opensim.region.optionalmodules.tests)==0}" /> 

  <exec program="${nunitcmd}" failonerror="true" resultproperty="testresult.opensim.region.framework.tests">
    <arg value="./bin/OpenSim.Region.Framework.Tests.dll" />
  </exec>
  <fail message="Failures reported in unit tests." unless="${int::parse(testresult.opensim.region.framework.tests)==0}" /> 

  <exec program="${nunitcmd}" failonerror="true" resultproperty="testresult.opensim.data.tests">
    <arg value="./bin/OpenSim.Data.Tests.dll" />
  </exec>
  <fail message="Failures reported in unit tests." unless="${int::parse(testresult.opensim.data.tests)==0}" /> 

  <exec program="${nunitcmd}" failonerror="true" resultproperty="testresult.opensim.capabilities.handlers.tests">
    <arg value="./bin/OpenSim.Capabilities.Handlers.Tests.dll" />
  </exec>
  <fail message="Failures reported in unit tests." unless="${int::parse(testresult.opensim.capabilities.handlers.tests)==0}" /> 

  <delete dir="%temp%"/>
</target>

<target name="torture" depends="build, find-nunit">
  <setenv name="MONO_THREADS_PER_CPU" value="100" />

  <exec program="${nunitcmd}" failonerror="true" resultproperty="testresult.opensim.tests.torture">
    <arg value="./bin/OpenSim.Tests.Torture.dll" />
  </exec>

  <fail message="Failures reported in unit tests." unless="${int::parse(testresult.opensim.tests.torture)==0}" /> 
  <delete dir="%temp%"/>
</target>

<target name="find-nunit">
  <exec program="which" failonerror="false"
        resultproperty="hasnunit2">
    <arg value="nunit-console2" />
  </exec>

  <property name="nunitcmd" value="nunit-console2"
    if="${int::parse(hasnunit2)==0}" />
  <property name="nunitcmd" value="nunit-console"
    if="${int::parse(hasnunit2)==1}" />
</target>

<!-- this is used for panda test execution -->
<!-- work in progress -->

<target name="test-xml" depends="build, find-nunit">
  <mkdir dir="test-results" failonerror="false" />
  <!-- Unit Test Assembly -->
  <exec program="${nunitcmd}" failonerror="false" resultproperty="testresult.opensim.tests">
    <arg value="./bin/OpenSim.Tests.dll" />
    <arg value="-xml=test-results/OpenSim.Tests.dll-Results.xml" />
  </exec>

  <exec program="${nunitcmd}" failonerror="false" resultproperty="testresult.opensim.framework.tests">
    <arg value="./bin/OpenSim.Framework.Tests.dll" />
    <arg value="-xml=test-results/OpenSim.Framework.Tests.dll-Results.xml" />
  </exec>

 <exec program="${nunitcmd}" failonerror="false" resultproperty="testresult.opensim.framework.serialization.tests">
    <arg value="./bin/OpenSim.Framework.Serialization.Tests.dll" />
    <arg value="-xml=test-results/OpenSim.Framework.Serialization.Tests.dll-Results.xml" />
  </exec>

 <exec program="${nunitcmd}" failonerror="false" resultproperty="testresult.opensim.framework.servers.tests">
    <arg value="./bin/OpenSim.Framework.Servers.Tests.dll" />
    <arg value="-xml=test-results/OpenSim.Framework.Servers.Tests.dll-Results.xml" />
  </exec>

 <exec program="${nunitcmd}" failonerror="false" resultproperty="testresult.opensim.region.clientstack.lindencaps.tests">
    <arg value="./bin/OpenSim.Region.ClientStack.LindenCaps.Tests.dll" />
    <arg value="-xml=test-results/OpenSim.Region.ClientStack.LindenCaps.Tests.dll-Results.xml" />
  </exec>

 <exec program="${nunitcmd}" failonerror="false" resultproperty="testresult.opensim.region.clientstack.lindenudp.tests">
    <arg value="./bin/OpenSim.Region.ClientStack.LindenUDP.Tests.dll" />
    <arg value="-xml=test-results/OpenSim.Region.ClientStack.LindenUDP.Tests.dll-Results.xml" />
  </exec>

 <exec program="${nunitcmd}" failonerror="false" resultproperty="testresult.opensim.region.scriptengine.tests">
    <arg value="./bin/OpenSim.Region.ScriptEngine.Tests.dll" />
    <arg value="-xml=test-results/OpenSim.Region.ScriptEngine.Tests.dll-Results.xml" />
  </exec>

 <exec program="${nunitcmd}" failonerror="false" resultproperty="testresult.opensim.region.coremodules.tests">
    <arg value="./bin/OpenSim.Region.CoreModules.Tests.dll" />
    <arg value="-xml=test-results/OpenSim.Region.CoreModules.Tests.dll-Results.xml" />
  </exec>

 <exec program="${nunitcmd}" failonerror="false" resultproperty="testresult.opensim.region.optionalmodules.tests">
    <arg value="./bin/OpenSim.Region.OptionalModules.Tests.dll" />
    <arg value="-xml=test-results/OpenSim.Region.OptionalModules.Tests.dll-Results.xml" />
  </exec>

 <exec program="${nunitcmd}" failonerror="false" resultproperty="testresult.opensim.region.framework.tests">
    <arg value="./bin/OpenSim.Region.Framework.Tests.dll" />
    <arg value="-xml=test-results/OpenSim.Region.Framework.Tests.dll-Results.xml" />
  </exec>

 <exec program="${nunitcmd}" failonerror="false" resultproperty="testresult.opensim.data.tests">
    <arg value="./bin/OpenSim.Data.Tests.dll" />
    <arg value="-xml=test-results/OpenSim.Data.Tests.dll-Results.xml" />
  </exec>

 <exec program="${nunitcmd}" failonerror="false" resultproperty="testresult.opensim.capabilities.handlers.tests">
    <arg value="./bin/OpenSim.Capabilities.Handlers.Tests.dll" />
    <arg value="-xml=test-results/OpenSim.Capabilities.Handlers.Tests.dll-Results.xml" />
  </exec>

 <fail message="Failures reported in unit tests." unless="${int::parse(testresult.opensim.tests)==0}" /> 
 <fail message="Failures reported in unit tests." unless="${int::parse(testresult.opensim.framework.tests)==0}" /> 
 <fail message="Failures reported in unit tests." unless="${int::parse(testresult.opensim.framework.servers.tests)==0}" /> 
 <fail message="Failures reported in unit tests." unless="${int::parse(testresult.opensim.region.clientstack.lindenudp.tests)==0}" />
 <fail message="Failures reported in unit tests." unless="${int::parse(testresult.opensim.region.scriptengine.tests)==0}" /> 
 <fail message="Failures reported in unit tests." unless="${int::parse(testresult.opensim.region.coremodules.tests)==0}" /> 
 <fail message="Failures reported in unit tests." unless="${int::parse(testresult.opensim.region.optionalmodules.tests)==0}" /> 
 <fail message="Failures reported in unit tests." unless="${int::parse(testresult.opensim.region.framework.tests)==0}" /> 
 <fail message="Failures reported in unit tests." unless="${int::parse(testresult.opensim.data.tests)==0}" /> 
 <fail message="Failures reported in unit tests." unless="${int::parse(testresult.opensim.capabilities.handlers.tests)==0}" /> 
</target>

<target name="doxygen">
  <exec program="doxygen" workingdir="doc" commandline="doxygen.conf" />
</target>
