# File lib/rubygems/server.rb, line 387
  def run
    WEBrick::Daemon.start if @daemon

    spec_dir = File.join @gemdir, "specifications"

    s = WEBrick::HTTPServer.new :Port => @port

    s.mount_proc("/yaml") do |req, res|
      res['content-type'] = 'text/plain'
      res['date'] = File.stat(spec_dir).mtime
      res.body << Gem::SourceIndex.from_gems_in(spec_dir).to_yaml
    end

    s.mount_proc("/rdoc-style.css") do |req, res|
      res['content-type'] = 'text/css'
      res['date'] = File.stat(spec_dir).mtime
      res.body << RDOC_CSS
    end

    s.mount_proc("/") do |req, res|
      specs = []
      total_file_count = 0

      Gem::SourceIndex.from_gems_in(spec_dir).each do |path, spec|
        total_file_count += spec.files.size
        deps = spec.dependencies.collect { |dep|
          { "name"    => dep.name, 
            "version" => dep.version_requirements.to_s, }
        }
        deps = deps.sort_by { |dep| [dep["name"].downcase, dep["version"]] }
        deps.last["is_last"] = true unless deps.empty?

        # executables
        executables = spec.executables.sort.collect { |exec| {"executable" => exec} }
        executables = nil if executables.empty?
        executables.last["is_last"] = true if executables

        specs << {
          "authors"        => spec.authors.sort.join(", "),
          "date"           => spec.date.to_s,
          "dependencies"   => deps,
          "doc_path"       => ('/doc_root/' + spec.full_name + '/rdoc/index.html'),
          "executables"    => executables,
          "only_one_executable" => (executables && executables.size==1),
          "full_name"      => spec.full_name,
          "has_deps"       => !deps.empty?,
          "homepage"       => spec.homepage,
          "name"           => spec.name,
          "rdoc_installed" => Gem::DocManager.new(spec).rdoc_installed?,
          "summary"        => spec.summary,
          "version"        => spec.version.to_s,
        }
      end

      specs << {
        "authors" => "Chad Fowler, Rich Kilmer, Jim Weirich, Eric Hodel and others",
        "dependencies" => [],
        "doc_path" => "/doc_root/rubygems-#{Gem::RubyGemsVersion}/rdoc/index.html",
        "executables" => [{"executable" => 'gem', "is_last" => true}],
        "only_one_executable" => true,
        "full_name" => "rubygems-#{Gem::RubyGemsVersion}",
        "has_deps" => false,
        "homepage" => "http://rubygems.org/",
        "name" => 'rubygems',
        "rdoc_installed" => true,
        "summary" => "RubyGems itself",
        "version" => Gem::RubyGemsVersion,
      }

      specs = specs.sort_by { |spec| [spec["name"].downcase, spec["version"]] }
      specs.last["is_last"] = true

      # tag all specs with first_name_entry 
      last_spec = nil
      specs.each do |spec|
        is_first = last_spec.nil? || (last_spec["name"].downcase != spec["name"].downcase)
        spec["first_name_entry"] = is_first
        last_spec = spec
      end

      # create page from template
      template = TemplatePage.new(DOC_TEMPLATE)
      res['content-type'] = 'text/html'
      template.write_html_on res.body,
                             "gem_count" => specs.size.to_s, "specs" => specs,
                             "total_file_count" => total_file_count.to_s
    end

    paths = { "/gems" => "/cache/", "/doc_root" => "/doc/" }
    paths.each do |mount_point, mount_dir|
      s.mount(mount_point, WEBrick::HTTPServlet::FileHandler,
              File.join(@gemdir, mount_dir), true)
    end

    trap("INT") { s.shutdown; exit! }
    trap("TERM") { s.shutdown; exit! }

    s.start
  end