name: release

on:
  push:
    tags:
      - '*'

permissions:
  contents: write   # needed for release upload only

concurrency:
  group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
  cancel-in-progress: true

jobs:
  linux:
    runs-on: ubuntu-latest
    container:
      image: 'registry.opensuse.org/opensuse/tumbleweed'
      env:
        CC: ${{ matrix.cc }}
        CXX: ${{ matrix.cxx }}
        CC_LD: ${{ matrix.ld }}
        CXX_LD: ${{ matrix.ld }}

    strategy:
      fail-fast: false
      matrix:
        compiler: [gcc]
        include:
          - compiler: gcc
            cc: gcc
            cxx: g++
            pkg:
              - gcc-c++
    steps:
      - name: '🛒 Install dependencies'
        run: >
          zypper -n in --no-recommends
          meson
          git-core
          autoconf
          automake
          libtool
          'pkgconfig(lept)'
          ${{ join(matrix.pkg, ' ') }}
          || [ $? -eq 107 ]

      - name: '📂 Checkout'
        uses: actions/checkout@v6
        with:
          fetch-depth: 0
          fetch-tags: true

      # checkout action UID differs from container UID
      - name: '🙄 Tell Git to ignore workspace ownership'
        run: git config --global --add safe.directory '*'

      - name: '🚧 Configure build'
        run: meson setup --buildtype release ${{ join(matrix.args, ' ') }} build

      - name: '📦 Run dist'
        run: meson dist -C build

      - name: '🚀 Upload release archive'
        uses: softprops/action-gh-release@b4309332981a82ec1c5618f44dd2e27cc8bfbfda  # v3.0.0
        with:
          draft: true
          working_directory: build/meson-dist
          files: |
            *.tar.*
            *.sha256sum

  windows:
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: ['windows-2025-vs2026', 'windows-11-arm']
    steps:
      - name: '📂 Checkout'
        uses: actions/checkout@v6
        with:
          fetch-depth: 0
          fetch-tags: true

      - name: '🛒 Install dependencies'
        run: python -m pip install meson

      - name: '🚧 Configure build'
        shell: bash
        run: >
          meson setup
          --vsenv
          --pkgconfig.relocatable
          --buildtype release
          --prefix "$(pwd)/artifact"
          --licensedir .
          build

      - name: '🛠 Run build'
        run: meson compile -C build

      - name: '🔬 Run tests'
        run: meson test -C build --print-errorlogs 'jbig2enc:'

      - name: '📦 Install build artifact'
        id: install
        shell: bash
        run: |
          meson install -C build
          echo "prjinfo=$(meson introspect --projectinfo build)" >> $GITHUB_OUTPUT

      - name: '📦 Create release archive'
        run: Compress-Archive -Path artifact/* -Destination jbig2enc-${{ fromJSON(steps.install.outputs.prjinfo)['version'] }}-${{ runner.os }}-${{ runner.arch }}-MSVC.zip

      - name: '🚀 Upload release archive'
        uses: softprops/action-gh-release@b4309332981a82ec1c5618f44dd2e27cc8bfbfda  # v3.0.0
        with:
          draft: true
          files: jbig2enc-${{ fromJSON(steps.install.outputs.prjinfo)['version'] }}-${{ runner.os }}-${{ runner.arch }}-MSVC.zip
