<Project>

  <!--
    When compiling without mono, but targeting mono we need to replace some assemblies with facades to make it run on mono.
    This MonoFacades.targets file should only be included if not targeting windows and targeting net4x.
  
    Warning: We ONLY support facades that reside directly in MonoFacadesPath, otherwise the joining of items becomes complicated.

    Any MonoFacade listed that doesn't exist on disk will be removed instead of replaced.

    See: https://github.com/mono/mono/blob/master/tools/nuget-hash-extractor/download.sh
    That list defines assemblies that are prohibited from being loaded from the appdir, instead loading from mono GAC.
  -->

  <PropertyGroup>
    <MonoFacadesPath>$(MSBuildThisFileDirectory)..\Libraries\Mono\</MonoFacadesPath>
    <ResolveReferencesDependsOn>
      $(ResolveReferencesDependsOn);
      SubstituteMonoFacadesBuild
    </ResolveReferencesDependsOn>
  </PropertyGroup>

  <ItemGroup>
    <MonoFacade Include="$(MonoFacadesPath)*.dll" />
    <MonoFacade Include="System.IO.Compression.dll" />
    <MonoFacade Include="System.Net.Http.dll" />

    <!-- List of MonoFacade by FileName -->
    <MonoFacade_Facade Include="@(MonoFacade->'%(Filename)%(Extension)')" />
  </ItemGroup>

  <Target Name="SubstituteMonoFacadesBuild"
          AfterTargets="ResolveAssemblyReferences"
          BeforeTargets="GenerateBindingRedirects">

    <ItemGroup>
      <!-- List of ReferenceCopyLocalPaths by FileName and filter out those without Facades -->
      <MonoFacade_Resolved Include="@(ReferenceCopyLocalPaths->'%(Filename)%(Extension)')">
        <OriginalIdentity>%(ReferenceCopyLocalPaths.Identity)</OriginalIdentity>
        <MonoFacadeIdentity>$(MonoFacadesPath)%(Filename)%(Extension)</MonoFacadeIdentity>
      </MonoFacade_Resolved>
      <MonoFacade_Unrelated Include="@(MonoFacade_Resolved)" />
      <MonoFacade_Unrelated Remove="@(MonoFacade_Facade)" />
      <MonoFacade_Resolved Remove="@(MonoFacade_Unrelated)" />

      <!-- Modify the actual copy list -->
      <ReferenceCopyLocalPaths Remove="@(MonoFacade_Resolved->'%(OriginalIdentity)')" />
      <ReferenceCopyLocalPaths Include="@(MonoFacade_Resolved->'%(MonoFacadeIdentity)')" Condition="Exists('%(MonoFacade_Resolved.MonoFacadeIdentity)')" />
    </ItemGroup>
  </Target>

</Project>
