class IgnitionPhysics5 < Formula
  desc "Physics library for robotics applications"
  homepage "https://github.com/gazebosim/gz-physics"
  url "https://osrf-distributions.s3.amazonaws.com/ign-physics/releases/ignition-physics5-5.3.2.tar.bz2"
  sha256 "4262512fbb6952712234c5cbeed69cdabca338931bb6c587a1ef7d487a5f262b"
  license "Apache-2.0"
  revision 24

  # head "https://github.com/gazebosim/gz-physics.git", branch: "ign-physics5"

  bottle do
    root_url "https://osrf-distributions.s3.amazonaws.com/bottles-simulation"
    sha256               arm64_sequoia: "497c0c03909398b4dc5d1113d3028bb22c5e654596275c222382ba85746b4bac"
    sha256               arm64_sonoma:  "aefef06d2bc8176305b9ca13d2d2ce8c4475f1344b97d263c02942d23237d71f"
    sha256 cellar: :any, sonoma:        "cf06798a790b27fad1461c5836f57f21ae4ff34e13b56706242b3cb3a37fb2d2"
    sha256 cellar: :any, ventura:       "92c108427a588c01ec949e90fd90923c9a0377f4f14fab3860fd49d6d16ed48f"
  end

  depends_on "cmake" => :build

  depends_on "gz-plugin2" => :test

  depends_on "bullet"
  depends_on "dartsim"
  depends_on "fmt"
  depends_on "google-benchmark"
  depends_on "ignition-cmake2"
  depends_on "ignition-common4"
  depends_on "ignition-math6"
  depends_on "ignition-plugin1"
  depends_on "ignition-utils1"
  depends_on "pkgconf"
  depends_on "sdformat12"
  depends_on "tinyxml2"
  depends_on "urdfdom"

  patch do
    # Fix for unregistering dartsim collision detector
    url "https://github.com/gazebosim/gz-physics/commit/2c238fe87b7c5ebd3d1ba37784db39ce93a6f143.patch?full_index=1"
    sha256 "396557d48ae665c9a99ea0d9f60308a9ebb08198098df88a7f8497619ffb15d2"
  end

  def install
    rpaths = [
      rpath,
      rpath(source: lib/"ign-physics-5/engine-plugins", target: lib),
    ]
    cmake_args = std_cmake_args
    cmake_args << "-DCMAKE_INSTALL_RPATH=#{rpaths.join(";")}"

    # Use a build folder
    mkdir "build" do
      system "cmake", "..", *cmake_args
      system "make", "install"
    end
  end

  test do
    require "system_command"
    extend SystemCommand::Mixin
    # test plugins in subfolders
    %w[bullet dartsim tpe].each do |engine|
      p = lib/"ign-physics-5/engine-plugins/libignition-physics-#{engine}-plugin.dylib"
      # Use gz-plugin --info command to check plugin linking
      cmd = Formula["gz-plugin2"].opt_libexec/"gz/plugin2/gz-plugin"
      args = ["--info", "--plugin"] << p
      # print command and check return code
      system cmd, *args
      # check that library was loaded properly
      _, stderr = system_command(cmd, args:)
      error_string = "Error while loading the library"
      assert stderr.exclude?(error_string), error_string
    end
    # build against API
    (testpath/"test.cpp").write <<-EOS
      #include "ignition/plugin/Loader.hh"
      #include "ignition/physics/ConstructEmpty.hh"
      #include "ignition/physics/RequestEngine.hh"
      int main()
      {
        ignition::plugin::Loader loader;
        loader.LoadLib("#{opt_lib}/libignition-physics5-dartsim-plugin.dylib");
        ignition::plugin::PluginPtr dartsim =
            loader.Instantiate("ignition::physics::dartsim::Plugin");
        using featureList = ignition::physics::FeatureList<
            ignition::physics::ConstructEmptyWorldFeature>;
        auto engine =
            ignition::physics::RequestEngine3d<featureList>::From(dartsim);
        return engine == nullptr;
      }
    EOS
    (testpath/"CMakeLists.txt").write <<-EOS
      cmake_minimum_required(VERSION 3.10.2 FATAL_ERROR)
      find_package(ignition-physics5 REQUIRED)
      find_package(ignition-plugin1 REQUIRED COMPONENTS all)
      add_executable(test_cmake test.cpp)
      target_link_libraries(test_cmake
          ignition-physics5::ignition-physics5
          ignition-plugin1::loader)
    EOS
    system "pkg-config", "ignition-physics5"
    cflags   = `pkg-config --cflags ignition-physics5`.split
    ldflags  = `pkg-config --libs ignition-physics5`.split
    system "pkg-config", "ignition-plugin1-loader"
    loader_cflags   = `pkg-config --cflags ignition-plugin1-loader`.split
    loader_ldflags  = `pkg-config --libs ignition-plugin1-loader`.split
    system ENV.cc, "test.cpp",
                   *cflags,
                   *ldflags,
                   *loader_cflags,
                   *loader_ldflags,
                   "-lc++",
                   "-o", "test"
    system "./test"
    # test building with cmake
    mkdir "build" do
      system "cmake", ".."
      system "make"
      system "./test_cmake"
    end
    # check for Xcode frameworks in bottle
    cmd_not_grep_xcode = "! grep -rnI 'Applications[/]Xcode' #{prefix}"
    system cmd_not_grep_xcode
  end
end
