# File lib/rubygems/installer.rb, line 94
  def initialize(gem, options={})
    @gem = gem

    options = {
      :bin_dir      => nil,
      :env_shebang  => false,
      :exec_format  => false,
      :force        => false,
      :install_dir  => Gem.dir,
      :source_index => Gem.source_index,
    }.merge options

    @env_shebang         = options[:env_shebang]
    @force               = options[:force]
    gem_home             = options[:install_dir]
    @gem_home            = Pathname.new(gem_home).expand_path
    @ignore_dependencies = options[:ignore_dependencies]
    @format_executable   = options[:format_executable]
    @security_policy     = options[:security_policy]
    @wrappers            = options[:wrappers]
    @bin_dir             = options[:bin_dir]
    @development         = options[:development]
    @source_index        = options[:source_index]

    begin
      @format = Gem::Format.from_file_by_path @gem, @security_policy
    rescue Gem::Package::FormatError
      raise Gem::InstallError, "invalid gem format for #{@gem}"
    end

    begin
      FileUtils.mkdir_p @gem_home
    rescue Errno::EACCES, Errno::ENOTDIR
      # We'll divert to ~/.gems below
    end

    if not File.writable? @gem_home or
        # TODO: Shouldn't have to test for existence of bindir; tests need it.
        (@gem_home.to_s == Gem.dir and File.exist? Gem.bindir and
         not File.writable? Gem.bindir) then
      if options[:user_install] == false then # You don't want to use ~
        raise Gem::FilePermissionError, @gem_home
      elsif options[:user_install].nil? then
        unless self.class.home_install_warning or options[:unpack] then
          alert_warning "Installing to ~/.gem since #{@gem_home} and\n\t  #{Gem.bindir} aren't both writable."
          self.class.home_install_warning = true
        end
      end
      options[:user_install] = true
    end

    if options[:user_install] and not options[:unpack] then
      @gem_home = Gem.user_dir

      user_bin_dir = File.join(@gem_home, 'bin')
      unless ENV['PATH'].split(File::PATH_SEPARATOR).include? user_bin_dir then
        unless self.class.path_warning then
          alert_warning "You don't have #{user_bin_dir} in your PATH,\n\t  gem executables will not run."
          self.class.path_warning = true
        end
      end

      FileUtils.mkdir_p @gem_home unless File.directory? @gem_home
      # If it's still not writable, you've got issues.
      raise Gem::FilePermissionError, @gem_home unless File.writable? @gem_home
    end

    @spec = @format.spec

    @gem_dir = File.join(@gem_home, "gems", @spec.full_name).untaint
  end