# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# bos720 src/bos/usr/sbin/install/ecc/lib/ECC/PropertyManager.pm 1.2.1.1 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2006,2010 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
package PropertyManager;

use constant separator => '=';

sub new {
	my ($class) = @_;
	my ($self) = {};
    bless $self, $class;
    return $self;
}

sub read {
	my ($self, $file) = @_;
	open CONFIGFILE, "< $file";
	while ($line = <CONFIGFILE>) {
		chomp $line;
		#Is this a comment
		if ($line =~ /#/) {
			$comment = $1;
		}
		else {
            my ($key, $value) = split( separator, $line);
            $self->{$key} = $value;
         }
	}
	close CONFIGFILE;
	return 0;
}

sub get {
	my ($self, $key) = @_;
    return $self->{$key};
}

sub set {
	my ($self, $key, $value) = @_;
    $self->{$key} = $value;
}

sub add {
	my ($self, $key, $value) = @_;
    $self->{$key} = $value;
}

sub remove {
	my ($self, $key) = @_;
    delete $self->{$key};
}

sub write {
    my ($self, $file) = @_;

	# read in our comments
    open (CONFIGFILE,"$file") or return 0;
    $cmt_cnt = 0;
    while ($line = <CONFIGFILE>) {
        if ($line =~ /#/) {
            @comment[$cmt_cnt++] = $line;
        }
    }
    close CONFIGFILE;

	# write out the comments first
	open CONFIGFILE, "+> $file" or return 0;

    for ($cnt=0; $cnt < $cmt_cnt; $cnt++) {
		print CONFIGFILE @comment[$cnt];
	}

	# sort and write out our properties
	foreach $key (sort (keys(%$self))) {
		print CONFIGFILE "$key".separator."$self->{$key}\n";
	}
	close CONFIGFILE;
  system("chmod g+w " . $file);

	return 0;
}

sub dump {
    my ($self) = @_;
    foreach $key (sort (keys(%$self))) {
        print STDOUT "$key".separator."$self->{$key}\n";
    }
}

1;
