class OdinSeat < Formula
  desc "Terminal multiplexer with overlapping windows and an Alt-tap menu"
  homepage "https://xn--hliskjlf-eza0m.goblinreactor.com/"
  url "https://brew.goblinreactor.com/sources/odin-seat-0.1.0-20261015.1.tar.gz"
  version "0.1.0-20261015.1"
  sha256 "ff2b2d29a043712a5158e1033e828b68ec9dac8d8af1d69a1cbb0ff45209f72c"
  license all_of: ["Apache-2.0", "MIT", "Unicode-DFS-2016"]

  # The archive is a reviewed release snapshot, not a moving branch.
  livecheck do
    skip "Release snapshots are published by the Goblin Reactor maintainer"
  end

  bottle do
    root_url "https://brew.goblinreactor.com/bottles"
    sha256 cellar: :any_skip_relocation, arm64_tahoe: "165835ad8864fe616085f9a6c5feca3ebd62ff4ac4ef0fec12bdb14028c0cae1"
  end

  uses_from_macos "zlib"

  # The unit tests open Unix sockets and POSIX shared memory on this machine
  # only; Homebrew counts that as network access. No test contacts a host.
  allow_network_access! [:build, :test]

  def install
    system "make", "all"
    # The self-contained unit tests: the terminal emulator, the host input
    # parser, the compositor, input methods, and the image codecs.
    tests = %w[test-pointer test-silkie test-desktop test-config test-host test-menu test-term test-im test-codec]
    system "make", *tests
    tests.each { |t| system "./#{t}" }
    # install recompiles with the fallback data directory baked in; a
    # relative one keeps the binary free of this prefix, so the bottle is
    # relocatable. At run time the binary looks beside itself first.
    system "make", "install", "PREFIX=#{prefix}", "DATADIR_COMPILED=../share/odin-seat"
    doc.install "README.md", "LICENSE", "NOTICE", "docs/keys.md", "docs/ghostty.md",
                "docs/CLEANROOM.md"
  end

  def caveats
    <<~EOS
      Run odin-seat to attach to a session. Tap Alt (Option)
      and let go to open the menu, then a letter: O opens a window, ? shows
      help. Ghostty on macOS needs, in ~/.config/ghostty/config:
        macos-option-as-alt = true
      Ctrl-\\ opens the menu on a terminal that swallows the Option tap.
      Manuals: man odin-seat, man odin-seat-protocols.
      A running server is upgraded in place the next time a newer
      odin-seat attaches (or run `odin-seat upgrade`).
    EOS
  end

  test do
    version_output = shell_output("#{bin}/odin-seat -V")
    assert_match "Odin Seat 0.1.0", version_output
    # The installed binary finds its dictionaries beside itself.
    assert_match "share/odin-seat", version_output
    assert_path_exists Pathname(version_output[/data directory: (.*)$/, 1])/"pinyin.dict"
    assert_path_exists man1/"odin-seat.1"
    assert_path_exists man7/"odin-seat-protocols.7"
    assert_path_exists pkgshare/"pinyin.dict"
    # A detached session and a scripted menu key, on a socket of our own.
    sock = testpath/"sock"
    system bin/"odin-seat", "-S", sock, "new-session", "-s", "t", "-d",
           "-x", "100", "-y", "30", "--", "sleep", "30"
    system bin/"odin-seat", "-S", sock, "menu", "o"
    assert_equal 2, shell_output("#{bin}/odin-seat -S #{sock} list-windows -s t").lines.count
    system bin/"odin-seat", "-S", sock, "kill-server"
  end
end
