# Modified for Goblin Skiff on 2026-09-19.
class GoblinSkiff < Formula
  desc "Fork of Mosh optimized for bandwidth-constrained links"
  homepage "https://skiff.goblinreactor.com/"
  url "https://brew.goblinreactor.com/sources/goblin-skiff-1.4.0-goblin20260919.2.tar.gz"
  version "1.4.0-goblin20260919.2"
  sha256 "b3db69c56ad12c1ac672aba0c171732658a4841b42e5de232e3f2c6f850c5924"
  license all_of: ["GPL-3.0-or-later", "ISC"]

  bottle do
    root_url "https://brew.goblinreactor.com/bottles"
    sha256 arm64_tahoe: "187a5c9ecd6242c0e918e4c058cd10ceb1b780545c89d1cf0354118e05461628"
  end

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

  depends_on "autoconf" => :build
  depends_on "automake" => :build
  depends_on "pkgconf" => :build
  depends_on "python@3.14" => :test
  depends_on "abseil"
  depends_on "djvulibre"
  depends_on "libpng"
  depends_on "librsync"
  depends_on :macos
  depends_on "ncurses"
  depends_on "openssl@3"
  depends_on "protobuf"
  depends_on "webp"
  depends_on "zstd"

  # Core tests open Unix sockets; the installed test also uses loopback UDP.
  # Homebrew counts both as network access. No test contacts an external host.
  allow_network_access! [:build, :test]

  def install
    system "autoreconf", "-fi"
    system "./configure", *std_configure_args,
           "--docdir=#{doc}",
           "--disable-completion", "--disable-ufw", "--without-utempter",
           "--with-crypto-library=apple-common-crypto",
           "--with-zstd", "--with-librsync", "--with-fips-crypto",
           "--without-libraptorq", "--enable-compile-warnings=yes"
    system "make"
    system "sh", "packaging/check-prebuilt-fec.sh", "./src/moshcp/goblin-skiffcp"
    # Keep Unix socket paths short on macOS and never share a live session's
    # runtime directory. Tests use local Unix sockets and loopback UDP.
    Dir.mktmpdir("gc-", ENV.fetch("TMPDIR", "/tmp")) do |runtime|
      ENV["XDG_RUNTIME_DIR"] = runtime
      tests = %w[
        ocb-aes encrypt-decrypt fips-crypto base64 nonce-incr fec-codecs
        bulk-datagram skiffcp-protocol bulk-loss-sim transport-compression session-version
        state-samples osc52-parse kitty-graphics terminal-geometry
        terminal-display tmux-control control-panel mascot file-transfer
        link-budget sixel-state terminal-extensions download download-forward
        udp-relay udp-jump-wrapper.test connect-timeout.test
      ]
      system "make", "check", "TESTS=#{tests.join(" ")}"
    end
    system "make", "install"
    bash_completion.install "conf/bash-completion/completions/goblin-skiff"
    doc.install "README.md", "GOBLIN_DOWNLOAD_PROTOCOL.md", "SIXEL_STATE.md",
                "AUDIO.md", "UDP_RELAY.md", "debian/copyright"
    (pkgshare/"tests").install "src/tests/control-panel-integration.py"
    (pkgshare/"tests").install "packaging/check-prebuilt-fec.sh"
    (pkgshare/"verification").install "src/tests/test-suite.log"
  end

  def caveats
    <<~EOS
      Install goblin-skiff on both endpoints, then run:
        goblin-skiff user@server
      For UDP jump relays, install this release on each jump host too:
        goblin-skiff -J user@jump user@server
      Open the file/control menu with Ctrl-^ (usually Ctrl-Shift-6), then 0.
      This package does not replace stock mosh or a manual /usr/local install.
      Existing sessions keep running; open a new session after an upgrade.
      --fips-crypto requires a configured FIPS provider; this is not a FIPS
      certification. Source, licenses and setup: https://brew.goblinreactor.com/
    EOS
  end

  test do
    system "sh", pkgshare/"tests/check-prebuilt-fec.sh", bin/"goblin-skiffcp"
    assert_match "goblin-skiff-client", shell_output("#{bin}/goblin-skiff-client --version 2>&1")
    assert_match "goblin-skiff-server", shell_output("#{bin}/goblin-skiff-server --version 2>&1")
    assert_path_exists man1/"goblin-skiff.1"
    ENV["GOBLIN_SKIFF_TEST_CLIENT"] = bin/"goblin-skiff-client"
    ENV["GOBLIN_SKIFF_TEST_SERVER"] = bin/"goblin-skiff-server"
    system formula_opt_bin("python@3.14")/"python3.14",
           pkgshare/"tests/control-panel-integration.py", "--files"
    system formula_opt_bin("python@3.14")/"python3.14",
           pkgshare/"tests/control-panel-integration.py", "--udp-relay"
  end
end
