on:
  workflow_call:
    inputs:
      publish:
        required: true
        type: boolean

jobs:
  build:
    permissions:
      contents: read
    runs-on: ubuntu-latest
    needs: library
    strategy:
      fail-fast: false
      matrix:
        os: [fedora, centos, debian-x86_64, arch, debian-i386, ubuntu-x86_64]
    steps:
      - uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
      - name: Docker login
        run: docker login docker.pkg.github.com -u $GITHUB_ACTOR -p $GITHUB_TOKEN
        env:
          GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
      - name: Download tarball
        if: matrix.os == 'fedora' || matrix.os == 'centos'
        uses: actions/download-artifact@018cc2cf5baa6db3ef3c5f8a56943fffe632ef53 # v6.0.0
        id: download
        with:
          name: tarball
      - name: Build in container
        env:
          CI_NETWORK: true
          CI: true
          VERSION: ${{ needs.library.outputs.version }}
        run: |
          docker run --privileged -e CI_NETWORK=$CI_NETWORK -e CI=$CI -e VERSION=$VERSION -t \
              -v $GITHUB_WORKSPACE:/github/workspace \
              docker.pkg.github.com/fwupd/fwupd/fwupd-${{matrix.os}}:latest
      - name: Save any applicable artifacts
        uses: actions/upload-artifact@330a01c490aca151604b8cf639adc76d48f6c5d4 # v5.0.0
        with:
          name: ${{ matrix.os }}
          path: ${{ github.workspace }}/dist/*
          if-no-files-found: ignore
      - name: Check if tests supported
        id: supported
        run: |
          if [ -f contrib/ci/${{matrix.os}}-test.sh ]; then
            echo "supported=true" >> $GITHUB_OUTPUT
          else
            echo "supported=false" >> $GITHUB_OUTPUT
          fi
      - name: Test in container
        if: steps.supported.outputs.supported == 'true'
        env:
          CI_NETWORK: true
          CI: true
        run: |
          docker run --privileged -e CI_NETWORK=$CI_NETWORK -e CI=$CI -t \
              -v $GITHUB_WORKSPACE:/github/workspace \
              docker.pkg.github.com/fwupd/fwupd/fwupd-${{matrix.os}}:latest \
              contrib/ci/${{matrix.os}}-test.sh
      - name: Save any coverage data
        uses: actions/upload-artifact@330a01c490aca151604b8cf639adc76d48f6c5d4 # v5.0.0
        with:
          name: coverage-${{ join(matrix.*, '-') }}
          path: ${{ github.workspace }}/coverage.xml
      - name: Upload to codecov
        if: matrix.os == 'debian-x86_64' || matrix.os == 'debian-i386' || matrix.os == 'arch'
        uses: codecov/codecov-action@v5
        env:
          CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

  openbmc:
    permissions:
      contents: read
    runs-on: ubuntu-22.04
    if: ${{ !inputs.publish }}
    steps:
      - uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
      - name: Refresh dependencies
        run: |
          sudo rm -f /etc/apt/sources.list.d/microsoft-prod.list
          sudo apt update
      - name: Install dependencies
        run: |
          sudo ./contrib/ci/fwupd_setup_helpers.py install-dependencies -o ubuntu --yes
          sudo ./contrib/ci/fwupd_setup_helpers.py test-meson
      - name: Build
        run: |
          ./contrib/build-openbmc.sh --prefix=/home/runner/.root

  library:
    permissions:
      contents: read
    runs-on: ubuntu-latest
    outputs:
      version: ${{ steps.version.outputs.version }}
    steps:
      - uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
      - name: Refresh dependencies
        run: |
          sudo rm -f /etc/apt/sources.list.d/microsoft-prod.list
          sudo apt update
      - name: Install dependencies
        run: |
          sudo ./contrib/ci/fwupd_setup_helpers.py install-dependencies -o ubuntu --yes
          sudo ./contrib/ci/fwupd_setup_helpers.py test-meson
      - name: Build
        run: |
          meson setup build \
                      -Dbuild=library \
                      -Ddocs=disabled \
                      -Dman=false \
                      -Dauto_features=disabled \
                      -Dtests=false
          ninja -C build dist
      - name: Save version
        id: version
        run: |
          echo "version=$(meson introspect build --projectinfo | jq -r .version)" >> $GITHUB_OUTPUT
      - name: Save tarball
        uses: actions/upload-artifact@330a01c490aca151604b8cf639adc76d48f6c5d4 # v5.0.0
        with:
          name: tarball
          path: ${{ github.workspace }}/build/meson-dist/*xz

  macos:
    permissions:
      contents: read
    runs-on: macos-latest
    if: ${{ !inputs.publish }}
    steps:
    - name: install dependencies
      run: |
        brew install meson usb.ids gobject-introspection libarchive json-glib protobuf-c vala gi-docgen
        python3 -m pip install --user jinja2 --break-system-packages
    - uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
    - name: configure
      run: ./contrib/ci/build_macos.sh
    - name: build
      run: ninja -C build-macos

  build-windows:
    permissions:
      contents: read
    runs-on: ubuntu-latest
    container:
      image: fedora:42
    steps:
    - uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
    - name: configure
      run: ./contrib/ci/build_windows.sh
    - name: upload-artifact
      uses: actions/upload-artifact@330a01c490aca151604b8cf639adc76d48f6c5d4 # v5.0.0
      with:
        name: windows
        path: |
          ${{ github.workspace }}/dist/setup/*.msi

  build-freebsd:
    permissions:
      contents: read
    runs-on: ubuntu-latest
    if: ${{ !inputs.publish }}
    steps:
    - uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
    - name: Build
      id: test
      uses: vmactions/freebsd-vm@v1
      env:
        CI: true
        CC: gcc
      with:
        usesh: true
        mem: 8192
        sync: rsync
        envs: 'CI CC'
        prepare: |
          pkg install -y bash python3 git cmake gcc
        run: |
          ./contrib/ci/fwupd_setup_helpers.py install-dependencies -o freebsd --yes
          ./contrib/setup
          bash -c "source ./venv/bin/activate && build-fwupd"

  publish-windows:
    permissions:
      contents: write
    name: Publish Windows binaries
    runs-on: ubuntu-latest
    if: ${{ inputs.publish }}
    needs: build-windows
    steps:
      - uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
        with:
          fetch-depth: 0
      - name: Download artifact
        uses: actions/download-artifact@018cc2cf5baa6db3ef3c5f8a56943fffe632ef53 # v6.0.0
        id: download
        with:
          name: windows
      - name: Upload Binaries to Release
        uses: svenstaro/upload-release-action@v2
        with:
          repo_token: ${{ secrets.GITHUB_TOKEN }}
          tag: ${{ github.ref }}
          file_glob: true
          file: fwupd*.msi
