class Bzr < Formula
  desc "CLI for Bugzilla, inspired by gh"
  homepage "https://github.com/randomparity/bzr"
  license "MIT"
  version "{{VERSION}}"

  on_macos do
    on_arm do
      url "https://github.com/randomparity/bzr/releases/download/v{{VERSION}}/bzr-v{{VERSION}}-aarch64-apple-darwin.tar.gz"
      sha256 "{{MAC_ARM_SHA}}"
    end
    on_intel do
      # No prebuilt Intel macOS binary — fall back to a source build.
      url "https://github.com/randomparity/bzr/archive/refs/tags/v{{VERSION}}.tar.gz"
      sha256 "{{SRC_SHA}}"
      depends_on "rust" => :build
    end
  end

  on_linux do
    on_arm do
      url "https://github.com/randomparity/bzr/releases/download/v{{VERSION}}/bzr-v{{VERSION}}-aarch64-unknown-linux-gnu.tar.gz"
      sha256 "{{LINUX_ARM_SHA}}"
    end
    on_intel do
      url "https://github.com/randomparity/bzr/releases/download/v{{VERSION}}/bzr-v{{VERSION}}-x86_64-unknown-linux-gnu.tar.gz"
      sha256 "{{LINUX_INTEL_SHA}}"
    end
  end

  def install
    if OS.mac? && Hardware::CPU.intel?
      ENV["CARGO_TARGET_DIR"] = buildpath/"target"
      system "cargo", "install", "--locked", "--root", prefix, "--path", "."
      system "cargo", "run", "-p", "xtask", "--no-default-features", "--release", "--",
             "man", "--out", "#{buildpath}/man/man1"
      man1.install Dir["#{buildpath}/man/man1/*.1"]
    else
      bin.install "bzr"
      man1.install Dir["man/man1/*.1"]
    end
  end

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