class Openvdb < Formula
  desc "Sparse volume processing toolkit"
  homepage "http://www.openvdb.org/"
  url "https://github.com/dreamworksanimation/openvdb/archive/v3.0.0.tar.gz"
  sha256 "6c90cfda032c54876b321031717c13ea56a6b7b15c911d3edfbb2ad7af49700e"
  head "https://github.com/dreamworksanimation/openvdb.git"

  bottle do
    sha256 "9001f9f2bf882a7186ff6dd0e676c1947bdcfdfe0f10cd1ff21b114bcef47722" => :yosemite
    sha256 "572cc54af2939f50de34174540d26947037b7e055e38c21d31794186f2663a3a" => :mavericks
    sha256 "70a2321ed4e82b098b9a9befd4cc513529583881496793815089d463ee5744fd" => :mountain_lion
  end

  option "with-viewer", "Installs the command-line tool to view OpenVDB files"
  option "with-tests", "Installs the unit tests for the OpenVDB library"
  option "with-logging", "Requires log4cplus"
  option "with-docs", "Installs documentation"

  depends_on "openexr"
  depends_on "ilmbase"
  depends_on "tbb"
  depends_on "jemalloc" => :recommended

  if MacOS.version < :mavericks
    depends_on "boost" => "c++11"
  else
    depends_on "boost"
  end

  depends_on "homebrew/versions/glfw3" if build.with? "viewer"
  depends_on "cppunit" if build.with? "tests"
  depends_on "doxygen" if build.with? "docs"
  depends_on "log4cplus" if build.with? "logging"
  needs :cxx11

  resource "test_file" do
    url "http://www.openvdb.org/download/models/cube.vdb.zip"
    sha256 "05476e84e91c0214ad7593850e6e7c28f777aa4ff0a1d88d91168a7dd050f922"
  end

  def install
    ENV.cxx11
    # Adjust hard coded paths in Makefile
    args = [
      "DESTDIR=#{prefix}",
      "BOOST_INCL_DIR=#{Formula["boost"].opt_lib}/include",
      "BOOST_LIB_DIR=#{Formula["boost"].opt_lib}",
      "BOOST_THREAD_LIB=-lboost_thread-mt",
      "TBB_INCL_DIR=#{Formula["tbb"].opt_lib}/include",
      "TBB_LIB_DIR=#{Formula["tbb"].opt_lib}/lib",
      "EXR_INCL_DIR=#{Formula["openexr"].opt_lib}/include",
      "EXR_LIB_DIR=#{Formula["openexr"].opt_lib}/lib",
      "BLOSC_INCL_DIR=", # Blosc is not yet supported.
      "PYTHON_VERSION=",
      "NUMPY_INCL_DIR="
    ]

    if build.with? "jemalloc"
      args << "CONCURRENT_MALLOC_LIB_DIR=#{Formula["jemalloc"].opt_lib}/lib"
    else
      args << "CONCURRENT_MALLOC_LIB="
    end

    if build.with? "viewer"
      args << "GLFW_INCL_DIR=#{Formula["homebrew/versions/glfw3"].opt_lib}/include"
      args << "GLFW_LIB_DIR=#{Formula["homebrew/versions/glfw3"].opt_lib}/lib"
      args << "GLFW_LIB=-lglfw3"
    else
      args << "GLFW_INCL_DIR="
      args << "GLFW_LIB_DIR="
      args << "GLFW_LIB="
    end

    if build.with? "docs"
      args << "DOXYGEN=doxygen"
    else
      args << "DOXYGEN="
    end

    if build.with? "tests"
      args << "CPPUNIT_INCL_DIR=#{Formula["cppunit"].opt_lib}/include"
      args << "CPPUNIT_LIB_DIR=#{Formula["cppunit"].opt_lib}/lib"
    else
      args << "CPPUNIT_INCL_DIR=" << "CPPUNIT_LIB_DIR="
    end

    if build.with? "logging"
      args << "LOG4CPLUS_INCL_DIR=#{Formula["log4cplus"].opt_lib}/include"
      args << "LOG4CPLUS_LIB_DIR=#{Formula["log4cplus"].opt_lib}/lib"
    else
      args << "LOG4CPLUS_INCL_DIR=" << "LOG4CPLUS_LIB_DIR="
    end

    ENV.append_to_cflags "-I #{buildpath}"

    cd "openvdb" do
      system "make", "install", *args
    end
  end

  test do
    resource("test_file").stage testpath
    system "#{bin}/vdb_print", "-m", "cube.vdb"
  end
end
