# File common/lib/symbiosis/domain.rb, line 40
    def initialize( name = nil, prefix = "/srv" )
      #
      # Make sure our prefix exists
      #
      @prefix = File.expand_path(prefix)
      raise Errno::ENOENT, @prefix unless File.directory?(@prefix)

      #
      # If no name is set, asssign a random new one.
      #
      if name.nil?
        @name = random_string(10).downcase+".test" 
      else
        @name = name
      end

      #
      # Make sure the name is a valid domain name.
      #
      unless @name =~ NAME_REGEXP
        raise ArgumentError, "Bad name '#{@name.inspect}'"
      end

      #
      # Determine where the directory is.
      #
      @directory = File.join(@prefix, @name)

      #
      # If @directory (above) is a symlink, its original location is recorded
      # in @symlink.
      #
      @symlink   = nil

      #
      # If the directory exists, then check that we're not following a symlink.
      #
      if File.directory?(@directory)
        
        directory_stat = File.stat(@directory)

        #
        # Redirect elsewhere if we have a symlink.  Expand it up relative to
        # @prefix.
        #
        if File.lstat(@directory).ino != directory_stat.ino
          #
          # Deal with multiple layers of indirection with an inode comparison
          # would work better.  This will only work within the prefix
          # directory!
          #
          #
          # Work out which inode we're pointed at.  Use stat so we follow the link.
          #
          target_inode = directory_stat.ino

          #
          # Now find a matching entry inode.
          #
          new_directory = Dir.glob( File.join(@prefix,"*") ).find do |entry|
            #
            # Check the inodes -- use lstat so we stat actual links without
            # following.
            #
            File.lstat(entry).ino == target_inode
          end

          #
          # If we've found a directory, record it.
          #
          unless new_directory.nil?
            #
            # Seems OK :)  Record our results.
            #
            @symlink   = @directory
            @directory = new_directory
            directory_stat = File.stat(@directory)
          end
        end

        #
        # Set the uid/gid for the domain.
        #
        @uid = directory_stat.uid 
        @gid = directory_stat.gid
      else
        #
        # If this is a system proces, use the prefix owner, if poss, admin
        # otherwise.
        #
        if Process.uid < 1000
          prefix_stat = File.stat(@prefix)

          if prefix_stat.uid < 1000
            @uid = Etc.getpwnam("admin").uid
            @gid = Etc.getpwnam("admin").gid
          else
            @uid = prefix_stat.uid
            @gid = prefix_stat.gid
          end

        else
          #
          # This is good for testing.
          #
          @uid   = Process.uid
          @gid   = Etc.getpwuid(@uid).gid
        end
      end

      @user = Etc.getpwuid(@uid).name
      raise ArgumentError, "#{@directory} owned by a system user (UID less than 1000)" if @uid < 1000

      @group = Etc.getgrgid(@gid).name
      raise ArgumentError, "#{@directory} owned by a system group (GID less than 1000)" if @gid < 1000
    end