name: Release

on:
  workflow_dispatch:
  push:
    branches: [main]
    paths:
      - 'Cargo.toml'

permissions:
  contents: write

jobs:
  check-version:
    name: Check Version
    runs-on: ubuntu-latest
    outputs:
      should_release: ${{ steps.check.outputs.should_release }}
      version: ${{ steps.version.outputs.version }}
      tag: ${{ steps.version.outputs.tag }}
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - name: Get version from Cargo.toml
        id: version
        run: |
          VERSION=$(grep -m1 '^version' Cargo.toml | sed 's/version = "\(.*\)"/\1/')
          echo "version=$VERSION" >> "$GITHUB_OUTPUT"
          echo "tag=v$VERSION" >> "$GITHUB_OUTPUT"
      - name: Skip if the tag already exists
        id: check
        run: |
          if git rev-parse "v${{ steps.version.outputs.version }}" >/dev/null 2>&1; then
            echo "Tag v${{ steps.version.outputs.version }} exists — nothing to release"
            echo "should_release=false" >> "$GITHUB_OUTPUT"
          else
            echo "New version — will release"
            echo "should_release=true" >> "$GITHUB_OUTPUT"
          fi

  publish:
    name: Publish to crates.io
    needs: check-version
    if: needs.check-version.outputs.should_release == 'true'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2
      - name: cargo publish (idempotent)
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
        run: |
          # Treat "already uploaded" as success so a re-run / a version that was
          # published out of band doesn't fail the release.
          if cargo publish 2>&1 | tee /tmp/publish.log; then
            echo "published"
          elif grep -qiE "already (exists|uploaded)|crate version .* is already" /tmp/publish.log; then
            echo "already on crates.io — skipping"
          else
            exit 1
          fi

  build:
    name: Build ${{ matrix.target }}
    needs: check-version
    if: needs.check-version.outputs.should_release == 'true'
    strategy:
      fail-fast: false
      matrix:
        include:
          - target: aarch64-apple-darwin
            os: macos-14
            asset: looop-aarch64-darwin
          - target: x86_64-apple-darwin
            os: macos-15-intel
            asset: looop-x86_64-darwin
          - target: x86_64-unknown-linux-musl
            os: ubuntu-latest
            asset: looop-x86_64-linux
          - target: aarch64-unknown-linux-musl
            os: ubuntu-latest
            asset: looop-aarch64-linux
            use_cross: true
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
        with:
          targets: ${{ matrix.target }}
      - name: Install musl-tools (Linux x86_64)
        if: matrix.target == 'x86_64-unknown-linux-musl'
        run: sudo apt-get update && sudo apt-get install -y musl-tools
      - name: Install cross (for cross-compilation)
        if: matrix.use_cross
        run: cargo install cross --git https://github.com/cross-rs/cross
      - uses: Swatinem/rust-cache@v2
        with:
          key: ${{ matrix.target }}
      - name: Build (native)
        if: ${{ !matrix.use_cross }}
        run: cargo build --release --target ${{ matrix.target }}
      - name: Build (cross)
        if: matrix.use_cross
        run: cross build --release --target ${{ matrix.target }}
      - name: Rename binary
        run: cp target/${{ matrix.target }}/release/looop ${{ matrix.asset }}
      - name: Generate SHA256 checksum
        run: |
          if [[ "$RUNNER_OS" == "macOS" ]]; then
            shasum -a 256 ${{ matrix.asset }} > ${{ matrix.asset }}.sha256
          else
            sha256sum ${{ matrix.asset }} > ${{ matrix.asset }}.sha256
          fi
      - uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.asset }}
          path: |
            ${{ matrix.asset }}
            ${{ matrix.asset }}.sha256

  release:
    name: Create Release
    needs: [check-version, build]
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - name: Create tag
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          git tag -a "${{ needs.check-version.outputs.tag }}" -m "Release ${{ needs.check-version.outputs.tag }}"
          git push origin "${{ needs.check-version.outputs.tag }}"
      - name: Download all artifacts
        uses: actions/download-artifact@v4
        with:
          path: artifacts
      - name: Prepare release files
        run: |
          mkdir -p release
          for dir in artifacts/*/; do
            cp "$dir"* release/
          done
          ls -la release/
      - name: Create Release
        uses: softprops/action-gh-release@v2
        with:
          tag_name: ${{ needs.check-version.outputs.tag }}
          files: release/*
          generate_release_notes: true
          draft: false
          prerelease: ${{ contains(needs.check-version.outputs.version, '-') }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
