<?xml version="1.0"?>
<project name="NAnt Examples" default="rebuild">

	<property name="root.folder" value="${directory::get-current-directory()}" />
	<property name="build.folder" value="${root.folder}/build" />
	<property name="solution.folder" value="${root.folder}/main" />
	<property name="tools.folder" value="${root.folder}/tools" />
	<property name="schema.folder" value="${solution.folder}/schema" />
	<property name="program.files.x86" value="${environment::get-variable('ProgramFiles(x86)')}" if="${environment::variable-exists('ProgramFiles(x86)')}"/>
	<property name="program.files.x86" value="${environment::get-variable('ProgramFiles')}" unless="${property::exists('program.files.x86')}" />
	<property name="program.files" value="${environment::get-variable('ProgramFiles')}" />
	<property name="netfx.tools.folder" value="${program.files.x86}/Microsoft SDKs\Windows\v8.0A\Bin\NETFX 4.0 Tools" />
	<property name="netfx.folder" value="${environment::get-variable('SystemRoot')}/Microsoft.NET" />
	<property name="netfx.framework.folder" value="${netfx.folder}/Framework" if="${directory::exists(netfx.folder + '/Framework')}" />
	<property name="netfx.framework.folder" value="${netfx.folder}/Framework64" if="${directory::exists(netfx.folder + '/Framework64')}" />
	<property name="netfx4.folder" value="${netfx.framework.folder}/v4.0.30319" />
	<property name="ci.buildNumber" value="${environment::get-variable('APPVEYOR_BUILD_NUMBER')}" if="${environment::variable-exists('APPVEYOR_BUILD_NUMBER')}"/>
	<property name="ci.buildNumber" value="0" unless="${property::exists('ci.buildNumber')}" />
	<property name="coverity.folder" value="C:\Projects\cov-analysis-win64-7.7.0.4\cov-analysis-win64-7.7.0.4\bin" if="${directory::exists('C:\Projects\cov-analysis-win64-7.7.0.4\cov-analysis-win64-7.7.0.4\bin')}" />
	
	<property name="coverity.exe" value="${coverity.folder}/cov-build.exe" if="${property::exists('coverity.folder')}" />
	<property name="coverity.exe" value="cov-build.exe" unless="${property::exists('coverity.exe')}" />
  
  <property name="coverity.token" value="" />
  <property name="coverity.token" value="${environment::get-variable('COVERITY_TOKEN')}" if="${environment::variable-exists('COVERITY_TOKEN')}" />
  <property name="coverity.email" value="" />
  <property name="coverity.email" value="${environment::get-variable('COVERITY_EMAIL')}" if="${environment::variable-exists('COVERITY_EMAIL')}" />
  
  <property name="buildnumber.major" value="4" />
  <property name="buildnumber.minor" value="6" />
  <property name="buildnumber.build" value="${ci.buildNumber}" />
  <property name="buildnumber.revision" value="0" />
  <property name="buildnumber.version" value="${buildnumber.major}.${buildnumber.minor}.${buildnumber.build}.${buildnumber.revision}" />
  
	<loadtasks assembly="${build.folder}/nantcontrib-0.85/bin/NAnt.Contrib.Tasks.dll" />

	<include buildfile="${build.folder}/version.build" />
	<include buildfile="${build.folder}/opencover.build" />
	<include buildfile="${build.folder}/environment.build" />
	<include buildfile="${build.folder}/installer.build" />
	<include buildfile="${build.folder}/nuget_package.build" />
	<include buildfile="${build.folder}/metrics.build" />

	<target name="build-environment" depends="get-os-version, create-snk, prepare-environment, get-version-number, generate-release-notes, prepare-sample-project" />

	<target name="rebuild" depends="build-environment, clean, build, test" >
		<call target="test-cpp" if="${version::get-major(version::parse(machine.version))==6}" />
	</target>

	<target name="teamcity-build" depends="build-environment, clean" >
		<echo>##teamcity[buildNumber '${buildnumber.version}']</echo>
		<call target="build-all-platforms" />
	</target>
	
  <target name="copy-artefacts" >
    <move file="${solution.folder}/OpenCover.Installer/bin/Release/opencover.installer.msi" 
        tofile="${solution.folder}/bin/installer/opencover.${buildnumber.major}.${buildnumber.minor}.${buildnumber.build}.msi" />
    <move file="${solution.folder}/bin/zip/opencover.zip" 
        tofile="${solution.folder}/bin/zip/opencover.${buildnumber.major}.${buildnumber.minor}.${buildnumber.build}.zip" />
  </target>
  
	<target name="create-release" depends="clean-all, build-environment, build-release-platforms, test, test-cpp, create-zip-package">
		<call target="nuget-package" />
		<call target="set-rc-property" />
		<call target="nuget-package" />
		<call target="copy-artefacts" />
		<call target="test-bdd" />
	</target>
  
  <target name="create-coverity-release" depends="clean-all, build-environment" if="${property::exists('coverity.exe')}">
    
    <exec program="${coverity.exe}" commandline="--dir cov-int --encoding=UTF-8 build.bat build-release-platforms-x64" />

    <exec program="${tools.folder}/7-Zip/7za.exe">
			<arg value="a" />
			<arg value="coverity.zip" />
			<arg value="cov-int" />
		</exec>
        
    <exec program="${curl.exe}" 
      commandline='--form token=${coverity.token} --insecure --form email=${coverity.email} --form file=@coverity.zip --form version="${ci.buildNumber}" --form description="${ci.buildNumber}" https://scan.coverity.com/builds?project=OpenCover%2Fopencover' />

  </target>
  
  <target name="set-rc-property" >
    <property name="buildnumber.ci" value="-rc" />
  </target>
  	
  <target name="generate-release-notes" depends="get-version-number" >
    <delete file="ReleaseNotes.txt" />
    <copy file="ReleaseNotes.tmp" tofile="ReleaseNotes.txt" >
      <filterchain>
        <replacestring from="[[version]]" to="${buildnumber.major}.${buildnumber.minor}.${buildnumber.build}" />
      </filterchain>
    </copy>
	</target>

  <target name="prepare-sample-project" depends="get-version-number" >
    <delete file="samples\SampleSln\coverage.bat" />
    <copy file="samples\SampleSln\coverage.bat.tmp" tofile="samples\SampleSln\coverage.bat" >
      <filterchain>
        <replacestring from="[[version]]" to="${buildnumber.major}.${buildnumber.minor}.${buildnumber.build}" />
      </filterchain>
    </copy>
    <delete file="samples\SampleSln\.nuget\packages.config" />
    <copy file="samples\SampleSln\.nuget\packages.config.tmp" tofile="samples\SampleSln\.nuget\packages.config" >
      <filterchain>
        <replacestring from="[[version]]" to="${buildnumber.major}.${buildnumber.minor}.${buildnumber.build}" />
      </filterchain>
    </copy>
    <delete file="samples\SampleSln\nuget\packages.config" />
    <copy file="samples\SampleSln\.nuget\packages.config.tmp" tofile="samples\SampleSln\nuget\packages.config" >
      <filterchain>
        <replacestring from="[[version]]" to="${buildnumber.major}.${buildnumber.minor}.${buildnumber.build}" />
      </filterchain>
    </copy>
  </target>
	
</project>