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@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
      - name: Docker login
        run: docker login ghcr.io -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@70fc10c6e5e1ce46ad2ea6f2b72d43f7d47b13c3 # v8.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 \
              ghcr.io/fwupd/fwupd/fwupd-${{matrix.os}}:latest
      - name: Save any applicable artifacts
        uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f # v7.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 \
              ghcr.io/fwupd/fwupd/fwupd-${{matrix.os}}:latest \
              contrib/ci/${{matrix.os}}-test.sh
      - name: Save any coverage data
        uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f # v7.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@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
      - 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@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
      - 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@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f # v7.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 json-glib vala gi-docgen
        python3 -m pip install --user jinja2 --break-system-packages
    - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
    - 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@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
    - name: configure
      run: ./contrib/ci/build_windows.sh
    - name: upload-artifact
      uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f # v7.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@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
    - name: Build
      id: test
      uses: vmactions/freebsd-vm@v1.4.2
      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-docs:
    permissions:
      contents: read
    name: Publish docs
    if: ${{ inputs.publish }}
    runs-on: ubuntu-latest
    needs: build
    steps:
      - uses: actions/checkout@v6.0.2
      - name: Download artifact
        uses: actions/download-artifact@70fc10c6e5e1ce46ad2ea6f2b72d43f7d47b13c3 # v8.0.0
        id: download
        with:
          name: ubuntu-x86_64
      - name: Install SSH key
        uses: shimataro/ssh-key-action@v2
        with:
          key: ${{ secrets.FWUPD_GITHUB_IO_SSH_KEY }}
          name: id_rsa # optional
          known_hosts: unnecessary
          if_key_exists: fail # replace / ignore / fail; optional (defaults to fail)
      - name: Clone docs
        run: |
          cd share/doc/fwupd
          git clone --depth 1 git@github.com:fwupd/fwupd.github.io.git
      - name: Trigger docs deployment
        run: |
          cd share/doc/fwupd/fwupd.github.io
          git config credential.helper 'cache --timeout=120'
          git config user.email "info@fwupd.org"
          git config user.name "Documentation deployment Bot"
          rm -rf *
          cp ../../libfwupd* ../*html . -R
          git add .
          git commit -a --allow-empty -m "Trigger deployment"
          git push git@github.com:fwupd/fwupd.github.io.git

  publish-windows:
    permissions:
      contents: write
    name: Publish Windows binaries
    runs-on: ubuntu-latest
    if: ${{ inputs.publish }}
    needs: build-windows
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          fetch-depth: 0
      - name: Download artifact
        uses: actions/download-artifact@70fc10c6e5e1ce46ad2ea6f2b72d43f7d47b13c3 # v8.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
