require 'formula'

class Bobcat < Formula
  url 'http://downloads.sourceforge.net/project/bobcat/bobcat/2.18.00/bobcat_2.18.00.orig.tar.gz'
  homepage 'http://bobcat.sourceforge.net/'
  md5 '232c11dd91048ae8618db84b43a5d706'

  depends_on 'gcc'
  depends_on 'icmake'
  depends_on 'yodl'

  def install
	  inreplace 'INSTALL.im' do |s|
		  s.gsub! /"\/usr"/, "\"#{prefix}\""
		  s.gsub! /g\+\+ /, 'g++-4.6 -DBOBCAT_DIY_CLOEXEC_ '
		  s.gsub! /CPOPTS\s+"-d"/, 'CPOPTS "-pPR"'
	  end

	  icmake = Formula.factory 'icmake'
	  inreplace 'build', /\/usr\/bin\/icmake/, "#{icmake.prefix}/bin/icmake"

	  # Better as a patch?
	  inreplace 'icmake/libraries' do |s|
		  # prevent asking wether to install classes like milter etc.
		  s.gsub! /special\(1, all\);/, 'special(0, all); g_nClasses = sizeof(g_classes);'

		  # building shared libraries is different on mac os x
		  s.gsub! /run\(GPP \+   " -shared -Wl,--as-needed,"/, 'run(GPP +   " -dynamiclib "'
		  s.gsub! /"-z,defs,-soname," \+/, '"-Wl,-single_module"'
		  s.gsub! /libsomajor \+/, '//'
	  end

	  # yodl no longer has the option --no-warnings
	  inreplace 'icmake/man', /--no-warnings /, ''

	  system "./build", "libraries"
	  system "./build", "man"
	  system "./build", "install"
  end
end
