# File lib/multiruby.rb, line 49
  def self.build_and_install
    root_dir = self.root_dir
    versions = []

    Dir.chdir root_dir do
      self.setup_dirs

      rubygems = Dir["versions/rubygems*.tgz"]
      abort "You should delete all but one rubygem tarball" if rubygems.size > 1
      rubygem_tarball = File.expand_path rubygems.last rescue nil

      Dir.chdir "build" do
        Dir["../versions/*"].each do |tarball|
          next if tarball =~ /rubygems/

          build_dir = File.basename tarball, ".tar.gz"
          version = build_dir.sub(/^ruby-?/, '')
          versions << version
          inst_dir = "#{root_dir}/install/#{version}"

          unless test ?d, inst_dir then
            unless test ?d, build_dir then
              if test ?d, tarball then
                dir = File.basename tarball
                FileUtils.ln_sf "../versions/#{dir}", "../build/#{dir}"
              else
                puts "creating #{inst_dir}"
                Dir.mkdir inst_dir
                run "tar zxf #{tarball}"
              end
            end
            Dir.chdir build_dir do
              puts "building and installing #{version}"
              if test ?f, "configure.in" then
                gnu_utils_build inst_dir
              elsif test ?f, "Rakefile" then
                rake_build inst_dir
              else
                raise "dunno how to build"
              end

              if rubygem_tarball and version !~ /1[._-]9|mri_trunk|rubinius/ then
                rubygems = File.basename rubygem_tarball, ".tgz"
                run "tar zxf #{rubygem_tarball}" unless test ?d, rubygems

                Dir.chdir rubygems do
                  run "../ruby ./setup.rb --no-rdoc --no-ri &> ../log.rubygems"
                end
              end
            end
          end
        end
      end
    end

    versions
  end