name: Release

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

concurrency:
  group: release
  cancel-in-progress: false

permissions:
  contents: write

jobs:
  check-version:
    runs-on: ubuntu-latest
    outputs:
      version: ${{ steps.version.outputs.version }}
      changed: ${{ steps.version.outputs.changed }}
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 2

      - name: Check if version changed
        id: version
        run: |
          current=$(grep '^version' Cargo.toml | head -1 | sed 's/.*"\(.*\)"/\1/')
          previous=$(git show HEAD~1:Cargo.toml | grep '^version' | head -1 | sed 's/.*"\(.*\)"/\1/')
          echo "current=$current previous=$previous"
          if [ "$current" != "$previous" ]; then
            if git ls-remote --tags origin "refs/tags/v$current" | grep -q .; then
              echo "::error::Tag v$current already exists on remote"
              exit 1
            fi
            echo "version=$current" >> "$GITHUB_OUTPUT"
            echo "changed=true" >> "$GITHUB_OUTPUT"
          else
            echo "changed=false" >> "$GITHUB_OUTPUT"
          fi

      - name: Create and push tag
        if: steps.version.outputs.changed == 'true'
        run: |
          git tag "v${{ steps.version.outputs.version }}"
          git push origin "v${{ steps.version.outputs.version }}"

  test:
    needs: check-version
    if: needs.check-version.outputs.changed == 'true'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2
      - run: cargo test

  build:
    needs: [check-version, test]
    name: Build ${{ matrix.target }}
    runs-on: ${{ matrix.runner }}
    strategy:
      matrix:
        include:
          - runner: ubuntu-latest
            target: x86_64-unknown-linux-gnu
            artifact: fnug-linux-x86_64
          - runner: ubuntu-latest
            target: aarch64-unknown-linux-gnu
            artifact: fnug-linux-aarch64
            cross: true
          - runner: macos-15-intel
            target: x86_64-apple-darwin
            artifact: fnug-macos-x86_64
          - runner: macos-latest
            target: aarch64-apple-darwin
            artifact: fnug-macos-aarch64
          - runner: windows-latest
            target: x86_64-pc-windows-msvc
            artifact: fnug-windows-x86_64.exe
    steps:
      - uses: actions/checkout@v4

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

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

      - name: Install cross
        if: matrix.cross
        run: cargo install cross --git https://github.com/cross-rs/cross

      - name: Build
        run: |
          if [ "${{ matrix.cross }}" = "true" ]; then
            cross build --release --target ${{ matrix.target }}
          else
            cargo build --release --target ${{ matrix.target }}
          fi
        shell: bash

      - name: Package (unix)
        if: runner.os != 'Windows'
        run: |
          cp target/${{ matrix.target }}/release/fnug ${{ matrix.artifact }}
          chmod +x ${{ matrix.artifact }}

      - name: Package (windows)
        if: runner.os == 'Windows'
        run: cp target/${{ matrix.target }}/release/fnug.exe ${{ matrix.artifact }}
        shell: bash

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

  publish:
    needs: [check-version, test]
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable

      - name: Publish fnug-vt100
        run: cargo publish --manifest-path vendor/vt100/Cargo.toml || echo "fnug-vt100 already published, skipping"
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}

      - name: Wait for crates.io propagation
        run: sleep 30

      - name: Publish fnug
        run: cargo publish
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}

  release:
    needs: [check-version, build]
    runs-on: ubuntu-latest
    steps:
      - uses: actions/download-artifact@v4
        with:
          merge-multiple: true

      - name: Create release
        uses: softprops/action-gh-release@v2
        with:
          tag_name: v${{ needs.check-version.outputs.version }}
          generate_release_notes: true
          files: fnug-*

  pypi-build:
    needs: [check-version, test]
    name: PyPI Build ${{ matrix.target }}
    runs-on: ${{ matrix.runner }}
    strategy:
      matrix:
        include:
          - runner: ubuntu-latest
            target: x86_64-unknown-linux-gnu
            manylinux: manylinux_2_28
          - runner: ubuntu-latest
            target: aarch64-unknown-linux-gnu
            manylinux: manylinux_2_28
          - runner: macos-15-intel
            target: x86_64-apple-darwin
          - runner: macos-latest
            target: aarch64-apple-darwin
          - runner: windows-latest
            target: x86_64-pc-windows-msvc
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-python@v5
        with:
          python-version: "3.12"

      - name: Build wheels
        uses: PyO3/maturin-action@v1
        with:
          target: ${{ matrix.target }}
          args: --release --out dist
          manylinux: ${{ matrix.manylinux || 'auto' }}

      - uses: actions/upload-artifact@v4
        with:
          name: pypi-wheels-${{ matrix.target }}
          path: dist/*.whl

  pypi-sdist:
    needs: [check-version, test]
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Build sdist
        uses: PyO3/maturin-action@v1
        with:
          command: sdist
          args: --out dist

      - uses: actions/upload-artifact@v4
        with:
          name: pypi-sdist
          path: dist/*.tar.gz

  pypi-publish:
    needs: [pypi-build, pypi-sdist]
    runs-on: ubuntu-latest
    steps:
      - uses: actions/download-artifact@v4
        with:
          pattern: pypi-*
          merge-multiple: true
          path: dist/

      - name: Publish to PyPI
        uses: PyO3/maturin-action@v1
        with:
          command: upload
          args: --non-interactive --skip-existing dist/*
        env:
          MATURIN_PYPI_TOKEN: ${{ secrets.PYPI_API_TOKEN }}
