class Rtags < Formula
  desc "ctags-like source code cross-referencer with a clang frontend"
  homepage "https://github.com/Andersbakken/rtags"
  url "https://github.com/Andersbakken/rtags.git",
      :tag => "v2.2",
      :revision => "925a188e4038fa6e4a7c8ea4d30d682609c46578"

  head "https://github.com/Andersbakken/rtags.git"

  bottle do
    sha256 "7eba56cbea17e32c329164835d782d120a3aacacbe8d1cdf0e5761a52bf7f6e3" => :el_capitan
    sha256 "f344da9acdca6ffd14e18eff1cd2b6f1bb446b25c182328cc27c4832b46d8854" => :yosemite
    sha256 "fc16449d0ae6e8a1104833ef97839d82d9f3fab840e9e4bd069fab41bc77e36b" => :mavericks
  end

  depends_on "cmake" => :build
  depends_on "llvm" => "with-clang"
  depends_on "openssl"

  def install
    # Homebrew llvm libc++.dylib doesn't correctly reexport libc++abi
    ENV.append("LDFLAGS", "-lc++abi") if OS.mac?

    mkdir "build" do
      system "cmake", "..", *std_cmake_args
      system "make"
      system "make", "install"
    end
  end

  test do
    mkpath testpath/"src"
    (testpath/"src/foo.c").write <<-EOS.undent
        void zaphod() {
        }

        void beeblebrox() {
          zaphod();
        }
    EOS
    (testpath/"src/README").write <<-EOS.undent
        42
    EOS

    rdm = fork do
      $stdout.reopen("/dev/null")
      $stderr.reopen("/dev/null")
      exec "#{bin}/rdm", "-L", "log"
    end

    begin
      sleep 1
      pipe_output("#{bin}/rc -c", "clang -c src/foo.c", 0)
      sleep 1
      assert_match "foo.c:1:6", shell_output("#{bin}/rc -f src/foo.c:5:3")
      system "#{bin}/rc", "-q"
    ensure
      Process.kill 9, rdm
      Process.wait rdm
    end
  end
end
