name: Release

on:
  push:
    tags:
      - "v*"

permissions:
  contents: write

env:
  CARGO_TERM_COLOR: always

jobs:
  build:
    name: Build ${{ matrix.target }}
    runs-on: ${{ matrix.runner }}
    strategy:
      matrix:
        include:
          - target: x86_64-unknown-linux-gnu
            runner: ubuntu-latest
            artifact: ciabatta
            archive: ciabatta-linux-x86_64.tar.gz

          - target: aarch64-unknown-linux-gnu
            runner: ubuntu-latest
            artifact: ciabatta
            archive: ciabatta-linux-aarch64.tar.gz

          - target: x86_64-apple-darwin
            runner: macos-latest
            artifact: ciabatta
            archive: ciabatta-macos-x86_64.tar.gz

          - target: aarch64-apple-darwin
            runner: macos-latest
            artifact: ciabatta
            archive: ciabatta-macos-aarch64.tar.gz

          - target: x86_64-pc-windows-msvc
            runner: windows-latest
            artifact: ciabatta.exe
            archive: ciabatta-windows-x86_64.zip

    steps:
      - uses: actions/checkout@v4

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

      - name: Install cross-compilation tools (Linux aarch64)
        if: matrix.target == 'aarch64-unknown-linux-gnu'
        run: |
          sudo apt-get update
          sudo apt-get install -y gcc-aarch64-linux-gnu
          echo "CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER=aarch64-linux-gnu-gcc" >> $GITHUB_ENV

      - name: Cache cargo dependencies
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target
          key: ${{ runner.os }}-${{ matrix.target }}-cargo-${{ hashFiles('**/Cargo.lock') }}

      - name: Build release binary
        run: cargo build --release --target ${{ matrix.target }}

      - name: Package (Unix)
        if: runner.os != 'Windows'
        run: |
          cp target/${{ matrix.target }}/release/${{ matrix.artifact }} .
          tar czf ${{ matrix.archive }} ${{ matrix.artifact }}

      - name: Package (Windows)
        if: runner.os == 'Windows'
        shell: pwsh
        run: |
          Copy-Item target/${{ matrix.target }}/release/${{ matrix.artifact }} .
          Compress-Archive -Path ${{ matrix.artifact }} -DestinationPath ${{ matrix.archive }}

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.archive }}
          path: ${{ matrix.archive }}

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

      - name: Download all artifacts
        uses: actions/download-artifact@v4
        with:
          path: artifacts
          merge-multiple: true

      - name: Create release
        uses: softprops/action-gh-release@v2
        with:
          generate_release_notes: true
          files: artifacts/*

  publish:
    name: Publish to crates.io
    needs: release
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable

      - name: Set version from tag
        run: |
          VERSION="${GITHUB_REF_NAME#v}"
          echo "Setting crate version to $VERSION"
          cargo set-version "$VERSION" 2>/dev/null \
            || sed -i -E "0,/^version = \".*\"/s//version = \"$VERSION\"/" Cargo.toml

      - name: Publish to crates.io
        run: cargo publish --allow-dirty
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
