# Build (GitHub) — pipeline CI primaria
#
# Strategia hosting:
#   - Repo primario su Gitea (push iniziale)
#   - Push mirror automatico Gitea -> GitHub
#   - GitHub esegue TUTTA la CI (Gitea non esegue Actions)
#
# Trigger:
#   - push su main (incluso quello propagato dal mirror Gitea)
#   - PR aperte su GitHub
#   - run manuale via UI

name: Build

on:
  push:
    branches: [main]
    paths-ignore:
      - '**.md'
      - 'docs/**'
      - 'examples/**'
  pull_request:
    branches: [main]
  workflow_dispatch:

jobs:
  build:
    runs-on: windows-latest
    strategy:
      fail-fast: false
      matrix:
        config: [x86-ansi, x86-unicode, x64-unicode]
    steps:
      - name: Checkout
        uses: actions/checkout@v6
        with:
          submodules: recursive

      - name: Setup Python
        uses: actions/setup-python@v6
        with:
          python-version: '3.11'

      - name: Setup MSBuild
        uses: microsoft/setup-msbuild@v3

      - name: Read VERSION
        id: version
        shell: pwsh
        run: |
          $v = (Get-Content VERSION -Raw).Trim()
          "version=$v" | Out-File -FilePath $env:GITHUB_OUTPUT -Append

      - name: Build ${{ matrix.config }}
        run: python build_plugin.py --configs ${{ matrix.config }} --verbosity normal

      - name: Collect DLL
        shell: pwsh
        run: |
          $map = @{ 'x86-ansi' = 'x86-ansi'; 'x86-unicode' = 'x86-unicode'; 'x64-unicode' = 'amd64-unicode' }
          $src = "plugins\$($map['${{ matrix.config }}'])\nsis7z.dll"
          $dst = "dist\${{ matrix.config }}"
          New-Item -ItemType Directory -Force -Path $dst | Out-Null
          Copy-Item $src $dst

      - name: Upload DLL
        uses: actions/upload-artifact@v7
        with:
          name: ${{ github.event.repository.name }}-${{ steps.version.outputs.version }}-${{ matrix.config }}
          path: dist/**
          if-no-files-found: error
          retention-days: 30

  build-linux:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        config: [x86-ansi, x86-unicode, x64-unicode]
    steps:
      - name: Checkout
        uses: actions/checkout@v6
        with:
          submodules: recursive

      - name: Setup Python
        uses: actions/setup-python@v6
        with:
          python-version: '3.11'

      - name: Install MinGW toolchain
        run: |
          sudo apt-get update
          sudo apt-get install -y \
            make \
            binutils-mingw-w64-i686 \
            binutils-mingw-w64-x86-64 \
            g++-mingw-w64-i686 \
            g++-mingw-w64-x86-64

      - name: Read VERSION
        id: version_linux
        run: |
          v="$(tr -d '\r\n' < VERSION)"
          echo "version=$v" >> "$GITHUB_OUTPUT"

      - name: Build ${{ matrix.config }} on Linux
        run: python build_plugin.py --host linux --configs ${{ matrix.config }} --verbose

      - name: Collect DLL
        run: |
          case "${{ matrix.config }}" in
            x86-ansi) dir="x86-ansi" ;;
            x86-unicode) dir="x86-unicode" ;;
            x64-unicode) dir="amd64-unicode" ;;
          esac
          dst="dist/${{ matrix.config }}"
          mkdir -p "$dst"
          cp "plugins/$dir/nsis7z.dll" "$dst/"

      - name: Upload DLL
        uses: actions/upload-artifact@v7
        with:
          name: ${{ github.event.repository.name }}-${{ steps.version_linux.outputs.version }}-${{ matrix.config }}-linux
          path: dist/**
          if-no-files-found: error
          retention-days: 30
