require 'stringio'

class ConfigFile

  
  def self.file(filename)
    fh = File.open(filename)
    obj = self.new(fh)
    obj.filename = filename
    fh.close
    obj
  end

  
  def self.data(string)
    fh = StringIO.new(string)
    obj = self.new(fh)
    fh.close
    obj
  end

  def initialize(filehandle)
    @lines = filehandle.readlines
    @filename = nil
    tidy_file
  end
  attr :filename

  def save()
    if @filename
      File.open(@filename, ) {|f| f.write(self)}
    end
  end

  def tidy_file()
    @lines.map! do |line|
      
      line.lstrip!

      if line.match(/^
        
        
        line
      else
        
        line.sub!(/^;+\s+/, )

        if line.match(/^; \s*$/) 
          
          
          line = 
        else
          
          line = line.rstrip + 

          
          
          if m = line.match(/^(; )?([[:upper:]]+)\s+(.*)/)
            line = (m[1].nil?? : m[1]) + format_line(m[2], m[3])
          end
        end

        line
      end
    end
  end

  def format_line(option, value)
     % [option.upcase.strip, value.nil?? :  + value.to_s.strip]
  end

  
  def find_option(option)
    @lines.find_index {|line| line.match(/^
  end

  
  def enable_option(option)
    if idx = find_option( + option)
      @lines[idx][/^; /] = 
    end
  end

  
  def disable_option(option)
    if idx = find_option(option)
      @lines[idx][/^/] = 
    end
  end

  
  
  def set_value(option, value)
    if idx = find_option(option)
      @lines[idx] = format_line(option, value)
    else
      @lines << format_line(option, value) 
    end
  end

  def to_s
    @lines.join('')
  end
end


config = ConfigFile.data(DATA.read)
config.disable_option('needspeeling')
config.enable_option('seedsremoved')
config.set_value('numberofbananas', 1024)
config.set_value('numberofstrawberries', 62000)
puts config


__END__














FAVOURITEFRUIT 		banana


  NEEDSPEELING


;;; SEEDSREMOVED
;;; 


NUMBEROFBANANAS 48