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:
      matrix:
        include:
          - target: x86_64-unknown-linux-musl
            os: ubuntu-latest
          - target: aarch64-unknown-linux-musl
            os: ubuntu-latest
          - target: x86_64-apple-darwin
            os: macos-latest
          - target: aarch64-apple-darwin
            os: macos-latest
    steps:
      - uses: actions/checkout@v4

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

      - name: Install cross (Linux)
        if: contains(matrix.target, 'linux')
        run: cargo install cross --locked

      - name: Build (Linux via cross)
        if: contains(matrix.target, 'linux')
        run: cross build --release --target ${{ matrix.target }}

      - name: Build (macOS native)
        if: contains(matrix.target, 'apple')
        run: cargo build --release --target ${{ matrix.target }}

      - name: Strip binary
        run: |
          BINARY="target/${{ matrix.target }}/release/muxtop"
          if [[ "${{ matrix.target }}" == *"linux"* ]]; then
            docker run --rm -v "$PWD:/workspace" -w /workspace \
              messense/rust-musl-cross:${{ matrix.target }} \
              strip "$BINARY" 2>/dev/null || true
          else
            strip "$BINARY" 2>/dev/null || true
          fi

      - name: Create archive
        run: |
          BINARY="target/${{ matrix.target }}/release/muxtop"
          ARCHIVE="muxtop-${{ matrix.target }}.tar.gz"
          tar czf "$ARCHIVE" -C "target/${{ matrix.target }}/release" muxtop
          shasum -a 256 "$ARCHIVE" > "$ARCHIVE.sha256"

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: muxtop-${{ matrix.target }}
          path: |
            muxtop-${{ matrix.target }}.tar.gz
            muxtop-${{ matrix.target }}.tar.gz.sha256

  release:
    name: Create 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 GitHub Release
        uses: softprops/action-gh-release@v2
        with:
          generate_release_notes: true
          files: |
            artifacts/*.tar.gz
            artifacts/*.sha256
            scripts/install.sh

  publish:
    name: Publish to crates.io
    needs: release
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - run: cargo publish --package muxtop-core
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
      - run: cargo publish --package muxtop-tui
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
      - run: cargo publish
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
