name: Windows native build

on:
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]


jobs:
  build:

    runs-on: windows-2019
    permissions:
      contents: write

    steps:
      - uses: actions/checkout@v4
        with:
          submodules: true
          ref: ${{ github.head_ref }}

      - name: Check for submodule update
        id: check_submodule
        run: |
          Set-Location whisper.cpp
          $SubmoduleCommitId = git rev-parse HEAD
          Write-Host "Submodule commit ID: $SubmoduleCommitId"
          Set-Location $Env:GITHUB_WORKSPACE
          $StoredCommitId = Get-Content native-commit-id-windows
          Write-Host "Stored commit ID: $StoredCommitId"
          if ($SubmoduleCommitId -ne $StoredCommitId) {
            Write-Host "Submodule has new commits. Triggering build."
            Write-Output "trigger_build=true" | Out-File -Append $Env:GITHUB_OUTPUT
            Set-Content -Path native-commit-id-windows -Value $SubmoduleCommitId
          } else {
            Write-Host "No new commits in submodule."
            Write-Output "trigger_build=false" | Out-File -Append $Env:GITHUB_OUTPUT
          }
        shell: pwsh

      - name: Add msbuild to PATH
        if: steps.check_submodule.outputs.trigger_build == 'true'
        uses: microsoft/setup-msbuild@v1
        
      - name: Install OpenCl with vcpkg
        if: steps.check_submodule.outputs.trigger_build == 'true'
        run: |
           vcpkg --triplet=x64-windows install opencl

      - name: Add Clblast to path
        if: steps.check_submodule.outputs.trigger_build == 'true'
        run: |
          New-Item -ItemType Directory -Force -Path build
          Invoke-WebRequest -Uri "https://github.com/CNugteren/CLBlast/releases/download/1.6.1/CLBlast-1.6.1-windows-x64.zip" -OutFile "build\clblast.zip"
          Expand-Archive -LiteralPath "build\clblast.zip" -DestinationPath "build/clblast" -Force
          7z x "build/clblast/CLBlast-1.6.1-windows-x64.7z" -o"build/clblast" -y
          Remove-Item "./build/clblast.zip"
          Remove-Item "./build/clblast/CLBlast-1.6.1-windows-x64.7z"
          $currentPath = (Get-Location).Path
          $clblastPath = $currentPath + "\build\clblast\CLBlast-1.6.1-windows-x64"
          $env:Path = $env:Path + ";" + $clblastPath
          Write-Output "PATH=$env:Path" | Out-File -Append -FilePath $env:GITHUB_ENV
        shell: pwsh

      - name: Install CUDA Toolkit
        if: steps.check_submodule.outputs.trigger_build == 'true'
        uses: Jimver/cuda-toolkit@v0.2.11
        with:
          cuda: '12.1.0'

      - name: Run Build
        if: steps.check_submodule.outputs.trigger_build == 'true'
        run: | 
          Import-Module ./windows-scripts.ps1
          BuildWindowsAll
        shell: pwsh

      - name: Remove CUDA installer
        if: steps.check_submodule.outputs.trigger_build == 'true'
        run: Remove-Item -Path 'cuda_installer-*' -Recurse -Force -ErrorAction Ignore

      - name: Pull latest commits
        if: steps.check_submodule.outputs.trigger_build == 'true'
        run: git pull origin ${{ github.head_ref }}

      - name: Commit and Push
        if: steps.check_submodule.outputs.trigger_build == 'true'
        uses: stefanzweifel/git-auto-commit-action@v4
        with:
          commit_message: Add native changes windows
