name: Release
on:
  push:
    branches: [main]
  workflow_dispatch:
env:
  CARGO_TERM_COLOR: always
jobs:
  check-version:
    name: Check version
    runs-on: ubuntu-latest
    outputs:
      version: ${{ steps.check.outputs.version }}
      release: ${{ steps.check.outputs.release }}
    steps:
      - uses: actions/checkout@v5
      - uses: dtolnay/rust-toolchain@stable
      - id: check
        run: |
          LOCAL=$(cargo metadata --no-deps --format-version 1 | jq -r '.packages[0].version')
          echo "version=$LOCAL" >> "$GITHUB_OUTPUT"
          PUBLISHED=$(cargo search beyond-slipstream --limit 1 2>/dev/null | head -1 | sed -n 's/.*= "\(.*\)".*/\1/p')
          echo "local=$LOCAL published=$PUBLISHED"
          if [ "$LOCAL" = "$PUBLISHED" ]; then
            echo "release=false" >> "$GITHUB_OUTPUT"
          else
            echo "release=true" >> "$GITHUB_OUTPUT"
          fi
  publish:
    name: Publish to crates.io
    needs: check-version
    if: needs.check-version.outputs.release == 'true'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v5
      - uses: dtolnay/rust-toolchain@stable
      - run: cargo publish
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
