# File lib/automateit/package_manager/portage.rb, line 13
  def installed?(*packages)
    return _installed_helper?(*packages) do |list, opts|
      # Emerge throws an error when called with invalid packages, so it's
      # necessary to find the invalid packages and re-run the command without
      # them to find out what is actually installed.
      missing = []
      available = []
      while true
        cmd = "emerge --color n --nospinner --tree --usepkg --quiet --pretend " + \
          (list-missing).join(' ') + " < /dev/null 2>&1"
        log.debug(PEXEC+cmd)
        output = `#{cmd}`

        if output.match(/no ebuilds to satisfy "(.+)"/)
          invalid = $1
          log.debug(PNOTE+"PackageManager::Portage.installed? skipping invalid package '#{invalid}'")
          missing << invalid
          break if (list-missing).size.zero?
        else
          matches = output.scan(%r{^\[\w+\s+R\s*\] .+/(\w+?)-.+$}).flatten
          available = list & matches
          break
        end
      end

      available
    end
  end