name: main

concurrency:
  group: ${{ github.ref }}
  cancel-in-progress: true

on:
  push:
  pull_request:

jobs:
  build:
    if: ${{ github.event_name != 'push' || github.ref != 'refs/heads/master'}}
    strategy:
      fail-fast: false
      matrix:
        include: [
          { msystem: MSYS, runner: windows-2022 },
        ]
    name: ${{ matrix.msystem }}
    runs-on: ${{ matrix.runner }}
    steps:
      - name: Gather Facts
        id: facts
        run : |
          Get-PSDrive -PSProvider FileSystem | Format-Table -AutoSize -Wrap
          Get-CIMInstance -Class Win32_Processor | Select-Object -Property Name
          Get-CIMInstance Win32_PhysicalMemory | Measure-Object -Property Capacity -Sum | Format-Table @{n="TotalPhysicalMemory";e={$_.Sum}}
          $BUILD_DRIVE = Split-Path $env:GITHUB_WORKSPACE -Qualifier
          echo "drive=$BUILD_DRIVE" >> $env:GITHUB_OUTPUT

      - name: Configure Pagefile
        if: ${{ matrix.msystem != 'CLANGARM64' }}
        # https://github.com/al-cheb/configure-pagefile-action/issues/16
        continue-on-error: true
        uses: al-cheb/configure-pagefile-action@v1.4
        with:
          minimum-size: 4GB
          maximum-size: 16GB
          # Needs to be C:, see https://github.com/al-cheb/configure-pagefile-action/issues/18
          disk-root: "C:"

      - uses: actions/checkout@v4
        with:
          path: temp
          fetch-depth: 0
          filter: blob:none
          persist-credentials: false

      - uses: msys2/setup-msys2@v2
        with:
          msystem: ${{ matrix.msystem }}
          install: git python base-devel
          update: true
          location: '${{ steps.facts.outputs.drive }}\M'

      - name: Add staging repo
        shell: msys2 {0}
        run: |
          cp /etc/pacman.conf /etc/pacman.conf.bak
          grep -qFx '[staging]' /etc/pacman.conf || sed -i '/^# \[staging\]/,/^$/ s|^# ||g' /etc/pacman.conf

      - name: Update using staging
        run: |
          msys2 -c 'pacman --noconfirm -Suuy'
          msys2 -c 'pacman --noconfirm -Suu'

      - name: Move Checkout
        run: |
          $path = "${{ steps.facts.outputs.drive }}\_"
          If (Test-Path "$path") { rm -r -fo "$path" }
          Move-Item -Path ".\temp" -Destination "$path"

      - name: CI-Build
        shell: msys2 {0}
        id: build
        run: |
          cd "$(cygpath '${{ steps.facts.outputs.drive }}/_')"
          ./.ci/ci-build.sh

      - name: "Upload binaries"
        if: ${{ !cancelled() }}
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.msystem }}-packages
          path: ${{ steps.facts.outputs.drive }}/_/artifacts/*.pkg.tar.*
          if-no-files-found: ignore

  package-grokker:
    runs-on: ubuntu-latest
    needs: build
    name: package-grokker-${{ matrix.msystem }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - msystem: MSYS
            repo: msys

    steps:
      - uses: actions/download-artifact@v4
        id: artifacts
        continue-on-error: true
        with:
          name: ${{ matrix.msystem }}-packages
          path: artifacts
      - name: Grok packages
        uses: jeremyd2019/package-grokker/grok-artifacts@main
        if: steps.artifacts.outcome == 'success'
        with:
          repo: ${{ matrix.repo }}
          path: artifacts
