name: Release

on:
  push:
    tags:
      - "v*"

permissions:
  contents: write

env:
  CARGO_TERM_COLOR: always
  BINARY: llmfit

jobs:
  build:
    strategy:
      matrix:
        include:
          # Linux x86_64 (static musl)
          - target: x86_64-unknown-linux-musl
            os: ubuntu-latest
            use-cross: true

          # Linux ARM64 (static musl)
          - target: aarch64-unknown-linux-musl
            os: ubuntu-latest
            use-cross: true

          # Linux x86_64 (glibc)
          - target: x86_64-unknown-linux-gnu
            os: ubuntu-latest
            use-cross: false

          # Linux ARM64 (glibc)
          - target: aarch64-unknown-linux-gnu
            os: ubuntu-latest
            use-cross: true

          # macOS Intel (cross-compiled from ARM64 runner)
          - target: x86_64-apple-darwin
            os: macos-latest
            use-cross: false

          # macOS Apple Silicon
          - target: aarch64-apple-darwin
            os: macos-latest
            use-cross: false

    runs-on: ${{ matrix.os }}

    steps:
      - name: Checkout
        uses: actions/checkout@v4

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

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

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

      - name: Package
        shell: bash
        run: |
          TAG="${GITHUB_REF_NAME}"
          ASSET="${BINARY}-${TAG}-${{ matrix.target }}"
          STAGING="/tmp/${ASSET}"
          mkdir -p "${STAGING}"

          cp "target/${{ matrix.target }}/release/${BINARY}" "${STAGING}/"
          cp README.md LICENSE "${STAGING}/" 2>/dev/null || true

          cd /tmp
          tar czf "${ASSET}.tar.gz" "${ASSET}"
          echo "ASSET=${ASSET}.tar.gz" >> "$GITHUB_ENV"
          echo "ASSET_PATH=/tmp/${ASSET}.tar.gz" >> "$GITHUB_ENV"

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: ${{ env.ASSET }}
          path: ${{ env.ASSET_PATH }}

  release:
    needs: build
    runs-on: ubuntu-latest

    steps:
      - name: Download all artifacts
        uses: actions/download-artifact@v4
        with:
          path: artifacts

      - name: Create GitHub Release
        uses: softprops/action-gh-release@v2
        with:
          generate_release_notes: true
          files: artifacts/**/*.tar.gz

  publish-crate:
    needs: release
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4

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

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

  update-homebrew:
    needs: release
    runs-on: ubuntu-latest

    steps:
      - name: Checkout tap
        uses: actions/checkout@v4
        with:
          repository: AlexsJones/homebrew-llmfit
          token: ${{ secrets.HOMEBREW_TAP_TOKEN }}
          path: homebrew-tap

      - name: Download release assets and update formula
        env:
          TAG: ${{ github.ref_name }}
        run: |
          VERSION="${TAG#v}"

          # Download the four tarballs and compute SHA256
          declare -A SHAS
          for target in aarch64-apple-darwin x86_64-apple-darwin aarch64-unknown-linux-musl x86_64-unknown-linux-musl; do
            URL="https://github.com/AlexsJones/llmfit/releases/download/${TAG}/llmfit-${TAG}-${target}.tar.gz"
            echo "Downloading ${URL}..."
            SHA=$(curl -fsSL "$URL" | shasum -a 256 | awk '{print $1}')
            SHAS[$target]="$SHA"
            echo "${target}: ${SHA}"
          done

          # Generate the formula
          cat > homebrew-tap/Formula/llmfit.rb << RUBY
          class Llmfit < Formula
            desc "Terminal tool that right-sizes LLM models to your system hardware"
            homepage "https://github.com/AlexsJones/llmfit"
            version "${VERSION}"
            license "MIT"

            on_macos do
              if Hardware::CPU.arm?
                url "https://github.com/AlexsJones/llmfit/releases/download/v#{version}/llmfit-v#{version}-aarch64-apple-darwin.tar.gz"
                sha256 "${SHAS[aarch64-apple-darwin]}"
              else
                url "https://github.com/AlexsJones/llmfit/releases/download/v#{version}/llmfit-v#{version}-x86_64-apple-darwin.tar.gz"
                sha256 "${SHAS[x86_64-apple-darwin]}"
              end
            end

            on_linux do
              if Hardware::CPU.arm?
                url "https://github.com/AlexsJones/llmfit/releases/download/v#{version}/llmfit-v#{version}-aarch64-unknown-linux-musl.tar.gz"
                sha256 "${SHAS[aarch64-unknown-linux-musl]}"
              else
                url "https://github.com/AlexsJones/llmfit/releases/download/v#{version}/llmfit-v#{version}-x86_64-unknown-linux-musl.tar.gz"
                sha256 "${SHAS[x86_64-unknown-linux-musl]}"
              end
            end

            def install
              bin.install "llmfit"
            end

            test do
              assert_match "llmfit", shell_output("#{bin}/llmfit --help")
            end
          end
          RUBY

          # Fix heredoc indentation
          sed -i 's/^          //' homebrew-tap/Formula/llmfit.rb

      - name: Commit and push
        run: |
          cd homebrew-tap
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          git add Formula/llmfit.rb
          git commit -m "Update llmfit to ${GITHUB_REF_NAME}"
          git push
