name: Release

on:
  push:
    tags: ["v*"]

permissions:
  contents: write

env:
  CARGO_TERM_COLOR: always
  FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: true

jobs:
  build:
    name: Build ${{ matrix.target }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - target: x86_64-apple-darwin
            os: macos-latest
          - target: aarch64-apple-darwin
            os: macos-latest
          - target: x86_64-unknown-linux-gnu
            os: ubuntu-latest
          - target: aarch64-unknown-linux-gnu
            os: ubuntu-latest

    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-compilation tools
        if: matrix.target == 'aarch64-unknown-linux-gnu'
        run: |
          sudo apt-get update
          sudo apt-get install -y gcc-aarch64-linux-gnu
          echo "CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER=aarch64-linux-gnu-gcc" >> "$GITHUB_ENV"

      - name: Build
        run: cargo build --release --target ${{ matrix.target }}

      - name: Package
        shell: bash
        run: |
          tag="${GITHUB_REF_NAME}"
          archive_target="${{ matrix.target }}"
          archive_target="${archive_target/unknown-/}"
          name="rivulet-${tag}-${archive_target}"
          mkdir "$name"
          cp target/${{ matrix.target }}/release/rivulet "$name/"
          cp README.md LICENSE "$name/"
          tar czf "${name}.tar.gz" "$name"
          echo "ASSET=${name}.tar.gz" >> "$GITHUB_ENV"

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

  release:
    name: Create Release
    needs: build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

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

      - name: Generate checksums
        run: |
          tag="${GITHUB_REF_NAME}"
          cd artifacts
          find . -name '*.tar.gz' -exec mv {} . \;
          sha256sum *.tar.gz > sha256sums.txt

          echo "SHA_AARCH64_DARWIN=$(sha256sum rivulet-${tag}-aarch64-apple-darwin.tar.gz | cut -d' ' -f1)" >> "$GITHUB_ENV"
          echo "SHA_X86_64_DARWIN=$(sha256sum rivulet-${tag}-x86_64-apple-darwin.tar.gz | cut -d' ' -f1)" >> "$GITHUB_ENV"
          echo "SHA_AARCH64_LINUX=$(sha256sum rivulet-${tag}-aarch64-linux-gnu.tar.gz | cut -d' ' -f1)" >> "$GITHUB_ENV"
          echo "SHA_X86_64_LINUX=$(sha256sum rivulet-${tag}-x86_64-linux-gnu.tar.gz | cut -d' ' -f1)" >> "$GITHUB_ENV"

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

      - name: Update Homebrew formula
        run: |
          tag="${GITHUB_REF_NAME}"
          version="${tag#v}"
          formula="Formula/rivulet.rb"

          sed -i "s/version \".*\"/version \"${version}\"/" "$formula"

          # Update SHA256 for each platform+arch block
          sed -i "/aarch64-apple-darwin/{n;s/sha256 \"[^\"]*\"/sha256 \"${SHA_AARCH64_DARWIN}\"/;}" "$formula"
          sed -i "/x86_64-apple-darwin/{n;s/sha256 \"[^\"]*\"/sha256 \"${SHA_X86_64_DARWIN}\"/;}" "$formula"
          sed -i "/aarch64-linux-gnu/{n;s/sha256 \"[^\"]*\"/sha256 \"${SHA_AARCH64_LINUX}\"/;}" "$formula"
          sed -i "/x86_64-linux-gnu/{n;s/sha256 \"[^\"]*\"/sha256 \"${SHA_X86_64_LINUX}\"/;}" "$formula"

      - name: Update AUR PKGBUILD
        run: |
          tag="${GITHUB_REF_NAME}"
          version="${tag#v}"
          pkgbuild="dist/aur/PKGBUILD"

          sed -i "s/^pkgver=.*/pkgver=${version}/" "$pkgbuild"
          sed -i "s/^sha256sums_x86_64=.*/sha256sums_x86_64=('${SHA_X86_64_LINUX}')/" "$pkgbuild"
          sed -i "s/^sha256sums_aarch64=.*/sha256sums_aarch64=('${SHA_AARCH64_LINUX}')/" "$pkgbuild"

      - name: Commit updated packages
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          git fetch origin main
          git checkout main
          git add Formula/rivulet.rb dist/aur/PKGBUILD
          git commit -m "chore: update packages for ${GITHUB_REF_NAME}"
          git push origin main
