name: Release

on:
  push:
    tags:
      - 'v*'

env:
  CARGO_TERM_COLOR: always

jobs:
  build:
    name: Build ${{ matrix.target }}
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        include:
          - target: x86_64-apple-darwin
            os: macos-15-intel
            name: lore-x86_64-apple-darwin
          - target: aarch64-apple-darwin
            os: macos-15
            name: lore-aarch64-apple-darwin
          - target: x86_64-unknown-linux-gnu
            os: ubuntu-latest
            name: lore-x86_64-unknown-linux-gnu
          - target: aarch64-unknown-linux-gnu
            os: ubuntu-24.04-arm
            name: lore-aarch64-unknown-linux-gnu

    steps:
      - uses: actions/checkout@v4

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

      - name: Install OpenSSL (macOS)
        if: runner.os == 'macOS'
        run: |
          brew list openssl@3 >/dev/null 2>&1 || brew install openssl@3
          echo "OPENSSL_DIR=$(brew --prefix openssl@3)" >> "$GITHUB_ENV"
          echo "PKG_CONFIG_PATH=$(brew --prefix openssl@3)/lib/pkgconfig" >> "$GITHUB_ENV"

      - name: Install dbus (Linux)
        if: runner.os == 'Linux'
        run: sudo apt-get update && sudo apt-get install -y libdbus-1-dev pkg-config

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

      - name: Package
        shell: bash
        run: |
          cd target/${{ matrix.target }}/release
          tar czvf ../../../${{ matrix.name }}.tar.gz lore
          cd ../../..

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

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

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

      - name: Create release
        uses: softprops/action-gh-release@v1
        with:
          files: artifacts/**/*.tar.gz
          generate_release_notes: true
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

  homebrew:
    name: Update Homebrew Formula
    needs: [build, release]
    runs-on: ubuntu-latest
    steps:
      - name: Download all artifacts
        uses: actions/download-artifact@v4
        with:
          path: artifacts

      - name: Calculate SHAs and update formula
        env:
          COMMITTER_TOKEN: ${{ secrets.HOMEBREW_TAP_TOKEN }}
          VERSION: ${{ github.ref_name }}
        run: |
          # Calculate SHA256 for each artifact
          SHA_MACOS_ARM=$(sha256sum artifacts/lore-aarch64-apple-darwin/lore-aarch64-apple-darwin.tar.gz | cut -d' ' -f1)
          SHA_MACOS_INTEL=$(sha256sum artifacts/lore-x86_64-apple-darwin/lore-x86_64-apple-darwin.tar.gz | cut -d' ' -f1)
          SHA_LINUX_ARM=$(sha256sum artifacts/lore-aarch64-unknown-linux-gnu/lore-aarch64-unknown-linux-gnu.tar.gz | cut -d' ' -f1)
          SHA_LINUX_INTEL=$(sha256sum artifacts/lore-x86_64-unknown-linux-gnu/lore-x86_64-unknown-linux-gnu.tar.gz | cut -d' ' -f1)

          # Strip 'v' prefix from version
          VERSION_NUM=${VERSION#v}

          # Generate formula
          cat > lore.rb << 'FORMULA'
          class Lore < Formula
            desc "Capture AI coding sessions and link them to git commits"
            homepage "https://github.com/varalys/lore"
            version "VERSION_PLACEHOLDER"
            license "Apache-2.0"

            on_macos do
              on_arm do
                url "https://github.com/varalys/lore/releases/download/VERSION_TAG_PLACEHOLDER/lore-aarch64-apple-darwin.tar.gz"
                sha256 "SHA_MACOS_ARM_PLACEHOLDER"
              end
              on_intel do
                url "https://github.com/varalys/lore/releases/download/VERSION_TAG_PLACEHOLDER/lore-x86_64-apple-darwin.tar.gz"
                sha256 "SHA_MACOS_INTEL_PLACEHOLDER"
              end
            end

            on_linux do
              on_arm do
                url "https://github.com/varalys/lore/releases/download/VERSION_TAG_PLACEHOLDER/lore-aarch64-unknown-linux-gnu.tar.gz"
                sha256 "SHA_LINUX_ARM_PLACEHOLDER"
              end
              on_intel do
                url "https://github.com/varalys/lore/releases/download/VERSION_TAG_PLACEHOLDER/lore-x86_64-unknown-linux-gnu.tar.gz"
                sha256 "SHA_LINUX_INTEL_PLACEHOLDER"
              end
            end

            def install
              bin.install "lore"
            end

            service do
              run [opt_bin/"lore", "daemon", "start", "--foreground"]
              keep_alive crashed: true
              log_path var/"log/lore.log"
              error_log_path var/"log/lore.log"
              working_dir HOMEBREW_PREFIX
            end

            def caveats
              <<~EOS
                To get started, run:
                  lore init

                The Homebrew service won't start until init completes.
              EOS
            end

            test do
              assert_match "lore", shell_output("#{bin}/lore --version")
            end
          end
          FORMULA

          # Remove leading spaces from heredoc (caused by YAML indentation)
          sed -i 's/^          //' lore.rb

          # Replace placeholders
          sed -i "s/VERSION_PLACEHOLDER/${VERSION_NUM}/g" lore.rb
          sed -i "s/VERSION_TAG_PLACEHOLDER/${VERSION}/g" lore.rb
          sed -i "s/SHA_MACOS_ARM_PLACEHOLDER/${SHA_MACOS_ARM}/g" lore.rb
          sed -i "s/SHA_MACOS_INTEL_PLACEHOLDER/${SHA_MACOS_INTEL}/g" lore.rb
          sed -i "s/SHA_LINUX_ARM_PLACEHOLDER/${SHA_LINUX_ARM}/g" lore.rb
          sed -i "s/SHA_LINUX_INTEL_PLACEHOLDER/${SHA_LINUX_INTEL}/g" lore.rb

          # Clone tap repo and update formula
          git clone https://x-access-token:${COMMITTER_TOKEN}@github.com/varalys/homebrew-tap.git
          cp lore.rb homebrew-tap/Formula/lore.rb
          cd homebrew-tap
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          git add Formula/lore.rb
          git commit -m "lore ${VERSION_NUM}"
          git push
