name: 'Install Windows Dependencies with CUDA'
description: 'Installs PCL, Qt, VTK, g2o and others'
runs:
  using: "composite"
  steps:
    - name: Set up MSVC Developer Command Prompt
      uses: ilammy/msvc-dev-cmd@v1
      with:
        arch: x64 
  
    - name: Install CUDA
      uses: Jimver/cuda-toolkit@v0.2.30
      id: cuda-toolkit
      with:
        cuda: '13.0.0'
        use-github-cache: True

    - name: Verify CUDA
      shell: bash
      run: |
        nvcc --version
        echo "CUDA Path: $CUDA_PATH"
  
    - name: Cache vcpkg
      id: cache-vcpkg
      uses: actions/cache@v4
      with:
        path: ${{ runner.workspace }}/vcpkg_installed
        key: ${{ runner.os }}-vcpkg-export-66c0373d-x64-vs2022-cuda130_v4

    - name: Download and Install vcpkg
      if: steps.cache-vcpkg.outputs.cache-hit != 'true'
      shell: pwsh
      run: |
        $install_dir = "${{ runner.workspace }}\vcpkg_installed"
        $archivePath = "${{ runner.workspace }}\vcpkg-export.7z"

        # The 7z is built locally with bundle_windows_deps_cuda.bat and uploaded to a
        # GitHub release. Resolve it by filename via the API (authenticated with the
        # built-in token) so it works even from a *draft* release and regardless of
        # the (changing) asset id. Requires the workflow to run in-repo (github.token
        # can't read introlab drafts from a fork PR).
        $repo = "introlab/rtabmap"
        $releaseTag = "0.23.8"
        $assetName = "vcpkg-export-66c0373d-x64-vs2022-cuda130.7z"
        $apiHeaders = @{ Authorization = "Bearer ${{ github.token }}"; "User-Agent" = "rtabmap-ci"; Accept = "application/vnd.github+json" }
        # Select the release by tag_name (populated on drafts too, and unique), then
        # take the asset from THAT release so concurrent releases can't be confused.
        $rel = (Invoke-RestMethod -Uri "https://api.github.com/repos/$repo/releases?per_page=100" -Headers $apiHeaders) |
               Where-Object { $_.tag_name -eq $releaseTag } | Select-Object -First 1
        if (-not $rel) { throw "Release tagged $releaseTag not found (including drafts)" }
        $asset = $rel.assets | Where-Object { $_.name -eq $assetName } | Select-Object -First 1
        if (-not $asset) { throw "Asset $assetName not found in release $releaseTag" }
        Write-Host "Downloading $assetName (asset id $($asset.id)) ..."
        $dlHeaders = @{ Authorization = "Bearer ${{ github.token }}"; "User-Agent" = "rtabmap-ci"; Accept = "application/octet-stream" }
        Invoke-WebRequest -Uri $asset.url -Headers $dlHeaders -OutFile $archivePath
        & 7z x $archivePath "-o$install_dir" -y

    - name: Add vcpkg to PATH and env variable
      shell: pwsh
      run: |
        $vcpkg_path = "${{ runner.workspace }}\vcpkg_installed"
        echo "VCPKG_EXPORT_PATH=$vcpkg_path" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
        echo "$vcpkg_path\installed\x64-windows-release\bin" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append
        # K4A runtime DLLs (k4a.dll, k4arecord.dll) live in the SDK bin; needed on PATH so fixup_bundle resolves them at package time.
        echo "$vcpkg_path\installed\x64-windows-release\sdk\windows-desktop\amd64\release\bin" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append
        echo "${{env.CUDA_PATH}}\bin\x64" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append
        echo "${{env.CUDA_PATH}}\extras\CUPTI\lib64" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append
        echo "$vcpkg_path\installed\x64-windows-release\tools\python3\Lib\site-packages\torch\lib" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append
        echo "$vcpkg_path\installed\x64-windows-release\tools\python3\Lib\site-packages\numpy.libs" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append
