<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <EnableTestPlatformV2CopyResources Condition="$(EnableTestPlatformV2CopyResources) == ''">true</EnableTestPlatformV2CopyResources>
  </PropertyGroup>

  <Target Name="GetTestPlatformV2CultureHierarchy">
    <!-- Only traversing 5 levels in the culture hierarchy. This is the maximum lenght for all cultures and should be sufficient to get to a culture name that maps to a resource folder we package. 
    The root culture name for all cultures is invariant whose name is ''(empty) and the parent for invariant culture is invariant itself.(https://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.parent(v=vs.110).aspx.) 
    So the below code should not break build in any case. -->
    <ItemGroup>
      <CurrentUICultureHierarchy Include="$([System.Globalization.CultureInfo]::CurrentUICulture.Name)" />
      <CurrentUICultureHierarchy Include="$([System.Globalization.CultureInfo]::CurrentUICulture.Parent.Name)"/>
      <CurrentUICultureHierarchy Include="$([System.Globalization.CultureInfo]::CurrentUICulture.Parent.Parent.Name)"  Condition="$([System.Globalization.CultureInfo]::CurrentUICulture.Parent.Parent.Name) != ''"/>
      <CurrentUICultureHierarchy Include="$([System.Globalization.CultureInfo]::CurrentUICulture.Parent.Parent.Parent.Name)"  Condition="$([System.Globalization.CultureInfo]::CurrentUICulture.Parent.Parent.Parent.Name) != ''"/>
      <CurrentUICultureHierarchy Include="$([System.Globalization.CultureInfo]::CurrentUICulture.Parent.Parent.Parent.Parent.Name)"  Condition="$([System.Globalization.CultureInfo]::CurrentUICulture.Parent.Parent.Parent.Parent.Name) != ''"/>
    </ItemGroup>
  </Target>

  <!-- Copy resources over to $(TargetDir) if this is a localized build. -->
  <Target Name="CopyTestPlatformV2Resources" BeforeTargets="PrepareForBuild" Condition="$(EnableTestPlatformV2CopyResources) == 'true'" DependsOnTargets="GetTestPlatformV2CultureHierarchy">

    <PropertyGroup>
      <CurrentUICultureHierarchy>%(CurrentUICultureHierarchy.Identity)</CurrentUICultureHierarchy>
    </PropertyGroup>

    <ItemGroup>
      <TestPlatformV2Files Include="$(MSBuildThisFileDirectory)*.dll" />
    </ItemGroup>

	<ItemGroup>
      <Content Include="@(TestPlatformV2Files->'%(RootDir)%(Directory)$(CurrentUICultureHierarchy)\%(FileName).resources.dll')" 
               Condition="Exists('%(RootDir)%(Directory)$(CurrentUICultureHierarchy)\%(FileName).resources.dll')">
         <Link>$(CurrentUICultureHierarchy)\%(FileName).resources.dll</Link>
         <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
         <BaseAssemblyFullPath>%(FullPath)</BaseAssemblyFullPath>
         <Visible>False</Visible>
      </Content>
    </ItemGroup>
  </Target>

  <!-- This is required because an empty resource folder is left even though the files within are cleaned up. -->
  <Target Name="CleanupTestPlatformV2ResourceFolders" AfterTargets="AfterClean" Condition="$(EnableTestPlatformV2CopyResources) == 'true'" DependsOnTargets="GetTestPlatformV2CultureHierarchy">
    <ItemGroup>
       <ResourceDirectories Include="$(TargetDir)%(CurrentUICultureHierarchy.Identity)" />
    </ItemGroup>
    <!-- RemoveDir does not throw if the folder does not exist. Continue on error - In any case do not fail build if this task fails(Warn and move on).-->
    <RemoveDir Directories="@(ResourceDirectories)" ContinueOnError="true"/>
  </Target>
</Project>