on:
  workflow_dispatch:

name: Release .NET Native NuGet Packages

jobs:
  release-runtimes:
    name: release-dotnet-native
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          fetch-depth: 0
          filter: tree:0

      - name: Setup .NET Core SDK
        uses: actions/setup-dotnet@v3.0.3
        with:
           dotnet-version: 7.x
      - uses: dawidd6/action-download-artifact@v2
        with:
          workflow: release.yml
          name: release-artifacts
      - name: Extract Archive
        run: |      
          extract_archive() {
              # Check if both pattern and destination are provided
              if [ -z "$1" ] || [ -z "$2" ]; then
                  echo "Usage: $0 <filename_pattern> <destination_directory>"
                  return 1
              fi
          
              # Set the filename pattern and destination directory
              filename_pattern="$1"
              destination_directory="$2"
          
              # Find the archive file with the specified pattern
              archive_file=$(ls $filename_pattern 2>/dev/null)
          
              # Check if an archive file is found
              if [ -n "$archive_file" ]; then
                  echo "Found archive file: $archive_file"
          
                  # Create the destination directory if it doesn't exist
                  mkdir -p "$destination_directory"
          
                  # Extract the archive to the specified destination
                  tar -xzvf "$archive_file" -C "$destination_directory"
          
                  echo "Extraction complete. Contents placed in: $destination_directory"
              else
                  echo "No matching archive file found with the pattern: $filename_pattern"
              fi
          }
          
          extract_archive "libextism-x86_64-pc-windows-msvc-*.tar.gz" "dotnet/nuget/runtimes/win-x64/native/"
          extract_archive "libextism-aarch64-apple-darwin-*.tar.gz" "dotnet/nuget/runtimes/osx-arm64/native/"
          extract_archive "libextism-x86_64-apple-darwin-*.tar.gz" "dotnet/nuget/runtimes/osx-x64/native/"
          extract_archive "libextism-x86_64-unknown-linux-gnu-*.tar.gz" "dotnet/nuget/runtimes/linux-x64/native/"
          extract_archive "libextism-aarch64-unknown-linux-gnu-*.tar.gz" "dotnet/nuget/runtimes/linux-arm64/native/"
          extract_archive "libextism-aarch64-unknown-linux-musl-*.tar.gz" "dotnet/nuget/runtimes/linux-musl-arm64/native/"

      - name: Pack NuGet packages
        run: |
          find ./dotnet/nuget -type f -name "*.csproj" -exec dotnet pack {} -o release-artifacts \;

      - name: Publish NuGet packages
        run: |
          dotnet nuget push --source https://api.nuget.org/v3/index.json ./release-artifacts/*.nupkg --api-key ${{ secrets.NUGET_API_KEY }}
