<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
    <OutputType>Exe</OutputType>
    <RuntimeIdentifier>linux-x64</RuntimeIdentifier>
    <EnableTargetingPackDownload>false</EnableTargetingPackDownload>
    <PublishTrimmed>true</PublishTrimmed>
    <_TrimmerDefaultAction>link</_TrimmerDefaultAction>
  </PropertyGroup>

  <ItemGroup>
    <None Update="wwwroot/**" CopyToOutputDirectory="PreserveNewest" />
    <None Include="appsettings.json" CopyToOutputDirectory="PreserveNewest" />
    <None Include="appsettings.postgresql.json" />
    <None Include="appsettings.mysql.json" />
  </ItemGroup>

  <ItemGroup>
    <TrimmerRootDescriptor Include="Linker.xml" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="5.0.0-rc.1.20451.17" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.0-rc.1.20451.13" />

    <PackageReference Include="Dapper" Version="2.0.35" />
    <PackageReference Include="MySqlConnector" Version="1.0.1" />
    <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="5.0.0-rc1" />
    <PackageReference Include="System.IO.Pipelines" Version="5.0.0-rc.1.20451.14" />
  </ItemGroup>

  <!-- Redirect "dotnet publish" to "mono-rp" folder-->
  <Target Name="TrickRuntimePackLocation" AfterTargets="ProcessFrameworkReferences">
    <PropertyGroup>
      <MonoRuntimeVer>5.0.0-rc.2.20474.8</MonoRuntimeVer>
    </PropertyGroup>
    <DownloadFile SourceUrl="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet5/nuget/v3/flat2/microsoft.netcore.app.runtime.mono.llvm.aot.$(RuntimeIdentifier)/$(MonoRuntimeVer)/microsoft.netcore.app.runtime.mono.llvm.aot.$(RuntimeIdentifier).$(MonoRuntimeVer).nupkg" DestinationFolder="$(MSBuildThisFileDirectory)" DestinationFileName="mono-rp.zip">
    </DownloadFile>
    <Unzip SourceFiles="mono-rp.zip" DestinationFolder="mono-rp" />
    <ItemGroup>
      <RuntimePack>
        <PackageDirectory>mono-rp</PackageDirectory>
      </RuntimePack>
    </ItemGroup>
    <Message Text="New PackageDirectory: %(RuntimePack.PackageDirectory)" Importance="high" />
  </Target>

  <Target Name="AspNetCore" AfterTargets="ComputeResolvedFilesToPublishList">
    <ItemGroup>
      <AspNetCoreFiles Include="$(MSBuildBinPath)/../../shared/Microsoft.AspNetCore.App/**/*.dll" />
    </ItemGroup>
    <ItemGroup>
      <ExtraFiles Include="%(RuntimePack.PackageDirectory)/runtimes/$(RuntimeIdentifier)/lib/$(TargetFramework)/*.dll" />
    </ItemGroup>

    <Copy SourceFiles="@(AspNetCoreFiles)" DestinationFolder="$(PublishDir)" />
    <Copy SourceFiles="@(ExtraFiles)" DestinationFolder="$(PublishDir)" />
  </Target>
  
  <!-- TODO: remove once https://github.com/dotnet/runtime/pull/42729 is merged and nugets are updated -->
  <Target Name="hack" AfterTargets="Publish">
    <ItemGroup>
      <HostingFiles Include="$(MSBuildBinPath)/../../host/fxr/**/libhostfxr.*" />
      <PolicyFiles Include="$(MSBuildBinPath)/../../shared/Microsoft.NETCore.App/**/libhostpolicy.*" />
    </ItemGroup>

    <Copy SourceFiles="@(HostingFiles)" DestinationFolder="$(PublishDir)" />
    <Copy SourceFiles="@(PolicyFiles)" DestinationFolder="$(PublishDir)" />
  </Target>
</Project>
