<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <PropertyGroup Condition="'$(ResolveMatchingContract)' == 'true'">
    <ResolveReferencesDependsOn>
      ResolveMatchingContract;
      $(ResolveReferencesDependsOn)
    </ResolveReferencesDependsOn>
    <CleanDependsOn>
      ResolveMatchingContract;
      $(CleanDependsOn);
    </CleanDependsOn>
  </PropertyGroup>
  
  <Target Name="ResolveMatchingContract">
    <PropertyGroup Condition="'$(ContractProject)' == ''">
      <ContractProject>$(SourceDir)/$(AssemblyName)/ref/$(APIVersion)/$(AssemblyName).csproj</ContractProject>
      <!-- fall back to 'current' version if specific version does not exist -->
      <ContractProject Condition="!(Exists('$(ContractProject)'))">$(SourceDir)/$(AssemblyName)/ref/$(AssemblyName).csproj</ContractProject>
      <!-- don't add a project if one can't be found-->
      <ContractProject Condition="!(Exists('$(ContractProject)'))"></ContractProject>
    </PropertyGroup>
    <ItemGroup>
      <ProjectReference Include="$(ContractProject)" Condition="'$(ContractProject)' != ''">
        <ReferenceOutputAssembly>false</ReferenceOutputAssembly>
        <OutputItemType>ResolvedMatchingContract</OutputItemType>
        <UndefineProperties>OSGroup;TargetGroup</UndefineProperties>
      </ProjectReference>
    </ItemGroup>
  </Target>
</Project>
