class Frescobaldi < Formula
  desc "LilyPond sheet music text editor"
  homepage "http://frescobaldi.org/"
  url "https://github.com/wbsoft/frescobaldi/releases/download/v2.19.0/frescobaldi-2.19.0.tar.gz"
  sha256 "b426bd53d54fdc4dfc16fcfbff957fdccfa319d6ac63614de81f6ada5044d3e6"
  revision 3

  depends_on "pkg-config" => :build
  depends_on "cartr/qt4/poppler-qt4" => "with-qt@4"
  depends_on "cartr/qt4/pyqt@4"
  depends_on "python@2" if MacOS.version <= :snow_leopard
  depends_on "portmidi" => :optional

  # python-poppler-qt4 dependencies

  resource "python-poppler-qt4" do
    url "https://github.com/wbsoft/python-poppler-qt4/archive/v0.24.0.tar.gz"
    sha256 "164297bcb03dc0cd943342915bf49e678db13957ebc2f1f3bd988f04145fb236"
  end

  resource "python-ly" do
    url "https://files.pythonhosted.org/packages/57/4f/889579244947368f28eda66b782331b1e75f83fd72e63f9ece93cd7a18f9/python-ly-0.9.4.tar.gz"
    sha256 "c2f87999260af3c9ea00c9997dae1e596fac40f45905d8b7e24e0f441112d63c"
  end

  def install
    ENV.prepend_create_path "PYTHONPATH", libexec/"vendor/lib/python2.7/site-packages"

    %w[python-poppler-qt4 python-ly].each do |r|
      resource(r).stage do
        inreplace "types.sip", "PyList_SET_ITEM", "PyList_SetItem" if r == "python-poppler-qt4"
        system "python", *Language::Python.setup_install_args(libexec/"vendor")
      end
    end

    ENV.prepend_create_path "PYTHONPATH", libexec/"lib/python2.7/site-packages"
    rm "setup.cfg"
    system "python", *Language::Python.setup_install_args(libexec)

    bin.install Dir[libexec/"bin/*"]
    bin.env_script_all_files(libexec/"bin", PYTHONPATH: ENV["PYTHONPATH"])
  end

  def caveats
    <<~EOS
      By default, a splash screen is shown on startup; this causes the main
      window not to show until the application icon on the dock is clicked
      (Cmd-Tab application switching does not appear to work).

      You may want to disable the splash screen in the preferences to
      solve this issue. See:
        https://github.com/wbsoft/frescobaldi/issues/428
    EOS
  end

  test do
    system bin/"frescobaldi", "--version"
  end
end
