<Project Sdk="Microsoft.NET.Sdk" InitialTargets="ConfigureNative">
  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
    <AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
  </PropertyGroup>

  <Import Project="../Raylib-cs/Build.props" />

  <PropertyGroup Condition="$([MSBuild]::IsOSPlatform('Linux'))">
    <NativeExt>so</NativeExt>
    <NativePre>lib</NativePre>
  </PropertyGroup>

  <PropertyGroup Condition="$([MSBuild]::IsOSPlatform('OSX'))">
    <NativeExt>dylib</NativeExt>
    <NativePre>lib</NativePre>
  </PropertyGroup>

  <PropertyGroup Condition="$([MSBuild]::IsOSPlatform('Windows'))">
    <NativeExt>dll</NativeExt>
    <NativePre>$(Configuration)/</NativePre>
  </PropertyGroup>

  <ItemGroup>
    <Content
      Include="$(OutputPath)native/raylib/$(NativePre)raylib.$(NativeExt)"
      Link="%(Filename)%(Extension)"
      CopyToOutputDirectory="PreserveNewest"
      Condition="Exists('$(OutputPath)native/raylib/$(NativePre)raylib.$(NativeExt)')" />

    <CMakeArgs Include="-B $(OutputPath)native" />
    <CMakeArgs Include="-S $(BaseIntermediateOutputPath)raylib-$(TargetRaylibTag)" />
    <CMakeArgs Include="-D CMAKE_BUILD_TYPE=$(Configuration)" />
    <CMakeArgs Include="-D BUILD_SHARED_LIBS=ON" />
    <CMakeArgs Include="-D BUILD_EXAMPLES=OFF" />
    <!-- <CMakeArgs Include="-D GRAPHICS=GRAPHICS_API_OPENGL_43" /> -->
  </ItemGroup>

  <Target Name="ConfigureNative" Condition="$(SkipLocalBuild) != true">
    <DownloadFile
      SourceUrl="https://github.com/raysan5/raylib/archive/refs/tags/$(TargetRaylibTag).zip"
      DestinationFolder="$(BaseIntermediateOutputPath)"
      DestinationFileName="raylib.zip"
      Condition="!Exists('$(BaseIntermediateOutputPath)raylib.zip')" />

    <Unzip
      SourceFiles="$(BaseIntermediateOutputPath)raylib.zip"
      DestinationFolder="$(BaseIntermediateOutputPath)"
      OverwriteReadOnlyFiles="true"
      Condition="!Exists('$(BaseIntermediateOutputPath)raylib-$(TargetRaylibTag)')" />
    
    <Exec Command="cmake @(CMakeArgs, ' ') $(CMakeExtraArgs)"
      Condition="!Exists('$(BaseIntermediateOutputPath)native/CMakeCache.txt')" />

    <Exec Command="cmake --build $(OutputPath)native --config $(Configuration)" />
  </Target>
</Project>