<?xml version="1.0"?>
<project>

  <property name="project.base" value="${project::get-base-directory()}" />

  <property name="samples.base" value="${path::get-full-path('../..')}" />
  <!-- Duplicate the following if more levels are added -->te
  <property name="samples.base" value="${path::get-full-path('../../..')}"
            unless="${path::get-file-name(samples.base)=='samples'}" />

  <property name="output.dir" value="${samples.base}/bin" />

  <property name="nunit.bin.dir"
            value="${path::combine(path::get-directory-name(samples.base), 'bin')}" />
  <property name="nunit.framework.dll"
            value="${path::combine(nunit.bin.dir,'net-1.1/framework/nunit.framework.dll')}" />
  <property name="nunit.core.dll"
            value="${path::combine(nunit.bin.dir,'net-1.1/nunit.core.dll')}" />
  <property name="nunit.core.interfaces.dll"
            value="${path::combine(nunit.bin.dir,'net-1.1/nunit.core.interfaces.dll')}" />

  <property name="sample" value="${project::get-name()}"
            unless="${property::exists('sample')}"/>
  <property name="sample.dll" value="${sample}.dll" />

  <property name="sample.type"
          value="${path::get-file-name(path::get-directory-name(project.base))}" />
  <property name="sample.type" value="addin" if="${sample.type=='Core'}" />

  <if test="${directory::exists(path::combine(project.base, 'Tests'))}" >
    <property name="tests" value="${sample}Tests"
          unless="${property::exists('tests')}" />
    <property name="test.dll" value="${tests}.dll" />
  </if>
  
  <property name="nunit.build" value="false"
    unless="${property::exists('project.package.dir')}"/>
  <property name="nunit.build" value="true" 
    if="${property::exists('project.package.dir')}"/>

  <property name="build.debug" value="true"
            unless="${property::exists('build.debug')}" />

  <if test="${not property::exists('build.config')}">
    <property name="build.config" value="Debug"
              if="${build.debug}" />
    <property name="build.config" value="Release"
              unless="${build.debug}" />
  </if>


  <target name="clean" description="Remove files created by build">

    <delete file="${output.dir}/${sample.dll}" />
    <delete file="${output.dir}/${sample}.pdb" />

    <if test="${property::exists('test.dll')}">
      <delete file="${output.dir}/${test.dll}" />
      <delete file="${output.dir}/${path::change-extension(test.dll, '.pdb')}" />
    </if>

  </target>

  <target name="init">

    <mkdir dir="${output.dir}" unless="${directory::exists(output.dir)}" />

    <copy file="${nunit.framework.dll}" todir="${output.dir}"
          if="${not nunit.build and file::exists(nunit.framework.dll)}" />

  </target>

  <target name="init-addin">

    <mkdir dir="${output.dir}" unless="${directory::exists(output.dir)}" />

    <copy file="${nunit.core.dll}" todir="${output.dir}"
          if="${not nunit.build and file::exists(nunit.core.dll)}" />
    <copy file="${nunit.core.interfaces.dll}" todir="${output.dir}"
          if="${not nunit.build and file::exists(nunit.core.interfaces.dll)}" />

  </target>

  <target name="build" Description="Build the sample">
    <call target="build-${sample.type}"/>
  </target>
  
  <target name="build-csharp" depends="init">

    <csc target="library" output="${output.dir}/${sample.dll}" debug="${build.debug}">
      <sources>
        <patternset refid="source-files"/>
      </sources>
      <references basedir="${output.dir}">
        <include name="nunit.framework.dll" />
      </references>
    </csc>

  </target>

  <target name="build-fsharp" depends="init">

    <property name="fsc.path" value="C:/Program Files/Microsoft SDKs/F#/3.0/Framework/v4.0/fsc.exe"/>

    <echo message="F# Build not yet supported"/>

  </target>

  <target name="build-addin" depends="init-addin">

    <csc target="library" output="${output.dir}/${sample}.dll" debug="${build.debug}">
      <sources>
        <patternset refid="source-files"/>
      </sources>
      <references basedir="${output.dir}">
        <include name="nunit.core.interfaces.dll" />
        <include name="nunit.core.dll" />
      </references>
    </csc>

    <call target="build-addin-test" if="${property::exists('test.dll')}" />

  </target>

  <target name="build-addin-test">

    <csc target="library" output="${output.dir}/${test.dll}" debug="${build.debug}">
      <sources basedir="Tests">
        <patternset refid="test-files"/>
      </sources>
      <references basedir="${output.dir}">
        <include name="nunit.framework.dll" />
        <include name="${sample}.dll" />
      </references>
    </csc>

  </target>

  <target name="build-vb" depends="init">

    <vbc target="library"
        output="${output.dir}/${sample.dll}" debug="${build.debug}">
      <imports>
        <import namespace="System"/>
        <import namespace="System.Collections"/>
      </imports>
      <sources>
        <patternset refid="source-files"/>
      </sources>
      <references basedir="${output.dir}">
        <include name="System.dll" />
        <include name="nunit.framework.dll" />
      </references>
    </vbc>

  </target>

  <target name="build-cpp" depends="init,set-vs-path">

    <fail message="Visual Studio 2010 must be installed to build this sample"
      unless="${property::exists( 'vs.2010.path' )}"/>

    <exec program="devenv.exe"
      basedir="${vs.2010.path}" 
      workingdir="."
      commandline="${sample}.vcxproj /build ${build.config} /out ${output.dir}/${sample.dll}"/>

  </target>

  <target name="set-vs-path">

    <readregistry property="vs.2010.path"
      key="Software\Microsoft\VisualStudio\10.0\InstallDir"
      hive="LocalMachine" failonerror="false"
      unless="${property::exists( 'vs.2010.path' )}"/>

  </target>

  <!-- ************************************************************* -->
  <!-- Package targets are only used by the NUnit build script in    -->
  <!-- order to package the samples for distribution.                -->
  <!-- ************************************************************* -->
  
  <target name="package">

    <fail message="Can't use package target directly - it must be called from the NUnit build script."
          unless="${nunit.build}"/>

    <property name="sample.path"
              value="${string::replace(project.base, samples.base, package.samples.dir)}" />

    <call target="package-${sample.type}" />

  </target>

  <target name="package-csharp">

    <property name="sample.proj" value="${sample}.csproj" />

    <call target="copy-source-files" />

  </target>

  <target name="package-fsharp">

    <property name="sample.proj" value="${sample}.fsproj" />

    <call target="copy-source-files" />

  </target>

  <target name="package-vb">
    
    <property name="sample.proj" value="${sample}.vbproj" />

    <call target="copy-source-files" />

  </target>

  <target name="package-cpp">
    
    <property name="sample.proj" value="${sample}.vcxproj" />

    <call target="copy-source-files" />

  </target>

  <target name="package-addin">
    
    <property name="sample.proj" value="${sample}.csproj" />

    <call target="copy-source-files" />
    <call target="copy-test-files" 
      if="${property::exists('test.dll')}"/>

  </target>

  <target name="copy-source-files">

    <copy todir="${sample.path}" includeemptydirs="false">
      <fileset basedir=".">
        <include name="${sample.proj}" />
        <include name="${sample}.build" />
        <include name="Readme.txt" />
        <patternset refid="source-files" />
      </fileset>
    </copy>

  </target>

  <target name="copy-test-files">

    <copy todir="${sample.path}/Tests" includeemptydirs="false">
      <fileset basedir="Tests">
        <include name="${sample}Tests.csproj" />
        <include name="${sample}Tests.build" />
        <patternset refid="test-files" />
      </fileset>
    </copy>

  </target>

</project>