# File common/lib/symbiosis/domains.rb, line 27
    def self.find(domain, prefix="/srv")
      #
      # make capital letters lower-case.
      #
      domain = domain.to_s.downcase

      #
      # Sanity check name.
      #
      return nil unless domain =~ Symbiosis::Domain::NAME_REGEXP

      #
      # Search all domains.  This returns a maximum of two results -- one with
      # www. and one without, assuming /srv/www.domain and /srv/domain both
      # exist.
      #
      # Check for domain, and (random.prefix.)?www.domain.
      #
      possibles = [domain, domain.sub(/^(.*\.)?www\./,"")].collect do |possible|
        dir = File.join(prefix, possible)
        next unless File.directory?(dir)

        begin
          Domain.from_directory(dir)
        rescue ArgumentError => err
          warn err.to_s
        end

      end.compact

      #
      # Nothing found, return nil
      #
      return nil if possibles.length == 0

      #
      # Return the one and only result.
      #
      return possibles.first if possibles.length == 1

      #
      # OK now match the nearest domain, breaking the domain down by dots.
      #
      until domain.nil?
        match = possibles.find{|d| d.name == domain}
        return match unless match.nil?

        #
        # Split the domain into a prefix, and the remainder.
        #
        prefix, domain = domain.split(".",2)
      end

      return nil
    end