name: Windows PGO Builds

permissions:
  contents: read

on:
  workflow_call:
    inputs:
      build-version:
        description: "The version to build"
        required: true
        type: string
      profile-data-path-archive:
        description: "The path to the zip archive containing the profile data"
        required: false
        type: string
      release-branch:
        description: "The branch to build"
        required: true
        type: string

jobs:
  windows-profile-build:
    name: |
      Windows Profile Build - ${{ matrix.arch }}
    strategy:
      fail-fast: false
      matrix:
        arch: [x86_64, aarch64]

    runs-on: ${{ inputs.release-branch == 'release' && 'windows-latest' || 'windows-latest' }}

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version-file: ".nvmrc"

      - name: Setup Git
        run: |
          git config --global user.name "github-actions[bot]"
          git config --global user.email "github-actions[bot]@users.noreply.github.com"

      - name: Install Surfer
        run: npm i -g @zen-browser/surfer

      - name: Load Surfer CI setup
        run: npm run surfer -- ci --brand ${{ inputs.release-branch }} --display-version ${{ inputs.build-version }}

      - name: Download artifact
        if: ${{ matrix.arch == 'x86_64' }}
        uses: actions/download-artifact@v4
        with:
          path: C:\artifact
          name: ${{ matrix.arch == 'aarch64' && 'arm64' || matrix.arch }}-zen-windows-profile-data-and-jarlog.zip

      - name: Unpack artifact
        if: ${{ matrix.arch == 'x86_64' }}
        run: |
          cd C:\artifact
          ls
          Expand-Archive -Path .\${{ inputs.profile-data-path-archive }} -DestinationPath C:\artifact
          ls

      - name: Setup
        run: |
          (New-Object System.Net.WebClient).DownloadFile("https://ftp.mozilla.org/pub/mozilla/libraries/win32/MozillaBuildSetup-Latest.exe", "C:\MozillaBuildSetup-Latest.exe")
          C:\MozillaBuildSetup-Latest.exe /S | out-null

      - name: Download Firefox and dependencies
        if: ${{ matrix.arch == 'x86_64' }}
        run: |
          git config --global core.safecrlf false
          npm run download

      - name: Import patches
        if: ${{ matrix.arch == 'x86_64' }}
        env:
          SURFER_NO_BRANDING_PATCH: true
        run: npm run import

      - name: Generate
        if: ${{ matrix.arch == 'x86_64' }}
        run: |
          ls
          $Env:USE_MINTTY = "0"
          $workspace_dir_current = [regex]::replace($env:GITHUB_WORKSPACE, "^([A-Z]):", { "/" + $args.value.Substring(0, 1).toLower() }) -replace "\\", "/"
          $workspace_dir = $workspace_dir_current + "/engine"
          echo $workspace_dir
          echo "cd $workspace_dir" '' >> mozilla-build-run.sh
          echo 'export PATH=/c/mozilla-build/msys2/usr/bin:$PATH' '' >> mozilla-build-run.sh
          echo './mach --no-interactive bootstrap --application-choice browser' '' >> mozilla-build-run.sh
          echo 'LLVM_PROFDATA=/c/Users/runneradmin/.mozbuild/clang/bin/llvm-profdata.exe JARLOG_FILE=en-US.log ./mach python build/pgo/profileserver.py --binary /c/artifact/zen/zen.exe' '' >> mozilla-build-run.sh
          C:\mozilla-build\start-shell.bat $workspace_dir_current\mozilla-build-run.sh

      - name: Move profile data
        if: ${{ matrix.arch == 'x86_64' }}
        run: |
          dir
          dir engine
          mv engine/merged.profdata merged.profdata
          mv engine/en-US.log en-US.log

      - name: Upload artifacts
        if: ${{ matrix.arch == 'x86_64' }}
        uses: actions/upload-artifact@v4
        with:
          retention-days: 1
          path: |
            merged.profdata
            en-US.log
          name: windows-profdata-${{ matrix.arch == 'aarch64' && 'arm64' || matrix.arch }}
