name: Release

on:
  push:
    tags: ['v*']

permissions:
  contents: write

env:
  CARGO_TERM_COLOR: always

jobs:
  build:
    name: Build ${{ matrix.target }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - target: x86_64-unknown-linux-gnu
            os: ubuntu-latest
            use_cross: false
            archive: tar.gz
          - target: aarch64-unknown-linux-gnu
            os: ubuntu-latest
            use_cross: true
            archive: tar.gz
          - target: powerpc64le-unknown-linux-gnu
            os: ubuntu-latest
            use_cross: false
            use_qemu: true
            archive: tar.gz
          - target: s390x-unknown-linux-gnu
            os: ubuntu-latest
            use_cross: true
            archive: tar.gz
          - target: aarch64-apple-darwin
            os: macos-14
            use_cross: false
            archive: tar.gz
          - target: x86_64-pc-windows-msvc
            os: windows-latest
            use_cross: false
            archive: zip
          - target: aarch64-pc-windows-msvc
            os: windows-latest
            use_cross: false
            archive: zip
    steps:
      - uses: actions/checkout@v6

      - name: Install libdbus-1-dev (native Linux)
        if: matrix.target == 'x86_64-unknown-linux-gnu'
        run: sudo apt-get update && sudo apt-get install -y libdbus-1-dev pkg-config

      - uses: dtolnay/rust-toolchain@stable
        with:
          targets: ${{ matrix.target }}

      - uses: Swatinem/rust-cache@v2
        with:
          key: ${{ matrix.target }}

      - name: Set up QEMU
        if: matrix.use_qemu
        uses: docker/setup-qemu-action@v4

      - name: Install cross
        if: matrix.use_cross
        run: cargo install cross --locked

      - name: Static CRT link (Windows)
        if: contains(matrix.target, 'windows-msvc')
        shell: bash
        run: echo "RUSTFLAGS=-C target-feature=+crt-static" >> "$GITHUB_ENV"

      - name: Build
        env:
          # pkg-config refuses to cross-compile by default; Cross.toml
          # installs target-arch libdbus via multiarch inside the cross
          # images, and this tells pkg-config to honour the resulting
          # target `.pc` files.
          PKG_CONFIG_ALLOW_CROSS: "1"
        run: |
          if [ "${{ matrix.use_qemu }}" = "true" ]; then
            docker run --rm --platform linux/ppc64le \
              -v "${{ github.workspace }}:/workspace" -w /workspace \
              rust:1.85-bookworm \
              bash -c "apt-get update && apt-get install -y libdbus-1-dev pkg-config && cargo build --release --locked --target ${{ matrix.target }}"
          elif [ "${{ matrix.use_cross }}" = "true" ]; then
            cross build --release --locked --target ${{ matrix.target }}
          else
            cargo build --release --locked --target ${{ matrix.target }}
          fi
        shell: bash

      - name: Package (unix)
        if: matrix.archive == 'tar.gz'
        run: |
          STAGING="bzr-${{ github.ref_name }}-${{ matrix.target }}"
          mkdir "$STAGING"
          cp "target/${{ matrix.target }}/release/bzr" "$STAGING/"
          cp LICENSE README.md "$STAGING/"
          tar czf "$STAGING.tar.gz" "$STAGING"
        shell: bash

      - name: Package (windows)
        if: matrix.archive == 'zip'
        run: |
          $STAGING = "bzr-${{ github.ref_name }}-${{ matrix.target }}"
          New-Item -ItemType Directory -Path $STAGING
          Copy-Item "target\${{ matrix.target }}\release\bzr.exe" "$STAGING\"
          Copy-Item LICENSE, README.md "$STAGING\"
          Compress-Archive -Path "$STAGING\*" -DestinationPath "$STAGING.zip"
        shell: pwsh

      - uses: actions/upload-artifact@v7
        with:
          name: bzr-${{ matrix.target }}
          path: bzr-${{ github.ref_name }}-${{ matrix.target }}.*

  release:
    name: Create Release
    needs: build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6

      - uses: actions/download-artifact@v8
        with:
          path: artifacts
          merge-multiple: true

      - name: Create GitHub Release
        env:
          GH_TOKEN: ${{ github.token }}
        run: |
          PRERELEASE=""
          if [[ "${{ github.ref_name }}" == *-* ]]; then
            PRERELEASE="--prerelease"
          fi
          gh release create "${{ github.ref_name }}" \
            --title "bzr ${{ github.ref_name }}" \
            --generate-notes \
            $PRERELEASE \
            artifacts/*
