require 'formula'

class Wxpython28 < Formula
  # For 2.8, we use this separate formula for wxPython. For 2.9 we use the combined one.
  url 'https://downloads.sourceforge.net/wxpython/wxPython-src-2.8.12.1.tar.bz2'
  sha1 '05688dc03d61631750f5904273122bb40a2115f5'
  head 'http://svn.wxwidgets.org/svn/wx/wxPython/trunk/', :using => StrictSubversionDownloadStrategy
  homepage 'http://www.wxpython.org'
end

class Wxmac28 < Formula
  url 'https://downloads.sourceforge.net/project/wxwindows/2.8.12/wxMac-2.8.12.tar.bz2'
  sha1 'cb7b7e76bb88ec4a23346cf0d99e361101b17737'

  homepage 'http://www.wxwidgets.org'

  option 'python', 'Build Python bindings'

  def test_python_arch
    # wxPython 2.8 does not yet support 64bit.
    begin
      system "arch -i386 python --version"
    rescue
      onoe "No python on path or default python does not support 32-bit."
      puts <<~EOS
        Your default python (if any) does not support 32-bit execution, which is
        required for the wxmac python bindings. You can install the Homebrew
        python with 32-bit support by running:

        brew install python --universal --framework

      EOS
      exit 99
    end
  end

  def install_wx_python
    opts = %W[
      # Reference our wx-config
      WX_CONFIG=#{bin}/wx-config
      # At this time Wxmac is installed Unicode only
      UNICODE=1
      # And thus we have no need for multiversion support
      INSTALL_MULTIVERSION=0
      # OpenGL and stuff
      BUILD_GLCANVAS=1
      BUILD_GIZMOS=1
      BUILD_STC=
    ]
    cd "wxPython" do
      # for wx 2.8 force 32-bit install with the 10.6 sdk:
      ENV.append_to_cflags '-arch i386'

      system "arch",   "-i386",
                       "python",
                       "setup.py",
                       "build_ext",
                       *opts

      system "arch",   "-i386",
                       "python",
                       "setup.py",
                       "install",
                       "--prefix=#{prefix}",
                       *opts
    end
  end

  def install
    test_python_arch if build.include? "python"

    args = %W[
      --disable-debug
      --prefix=#{prefix}
      --enable-unicode
      --enable-std_string
      --enable-display
      --with-opengl
    ]

    # Force i386 for wx 2.8
    ENV.m32

    # build will fail on Lion unless we use the 10.6 sdk (note wx 2.9 does fine)
    ENV.append_to_cflags '-isysroot /Developer/SDKs/MacOSX10.6.sdk -mmacosx-version-min=10.6' if MacOS.version == :lion

    system "./configure", *args
    system "make install"

    # erlang needs contrib/stc during configure phase.
    %w{ gizmos stc ogl }.each do |c|
     system "make", "-C", "contrib/src/#{c}", "install"
    end

    if build.include? "python"
      ENV['WXWIN'] = Dir.getwd
      # We need to download wxPython separately (see formula at top)
      Wxpython28.new.brew { install_wx_python }
    end
  end

  def caveats
    s = ''
    s += <<~EOS
      wxWidgets 2.8.x builds 32-bit only, so you probably won't be able to use it
      for other Homebrew-installed software. You can try to build with --devel to
      get the wxWidgets 2.9.x (unstable) for 64-bit and cocoa support.

    EOS
    if build.include? 'python'
      s += <<~EOS
        You will also need run python in 32-bit mode for wx. If you are on a 64-bit
        platform, you will need to run Python in 32-bit mode:

        arch -i386 python [args]

        Homebrew Python does not support this by default. So, homebrew Python must
        be built with --universal --framework.
        Alternative, you can try brew wxmac --python --devel for wx 2.9 in 64-bit.

      EOS
    end

    if build.include? 'python'
      s += <<~EOS
        Python bindings require that Python be built as a Framework; this is the
        default for Mac OS provided Python but not (yet) for Homebrew Python.
        Build python with `--framework`.

      EOS
    end

    return s
  end
end
