use strict;
use warnings;

package SReview::Config;

use Data::Dumper;

sub new {
	my $self = {defs => {}};
	my $class = shift;

	bless $self, $class;

	my $cfile = shift;

	if (! -f $cfile) {
		warn("could not find configuration file $cfile, using defaults");
	} else {
		package SReview::Config::_private;
		my $rc = do($cfile);
		if($@) {
			die "could not compile config file $cfile: $@";
		} elsif(!defined($rc)) {
			die "could not read config file $cfile: $!";
		} elsif(!$rc) {
			die "could not process config file $cfile";
		}
	}
	return $self;
};

sub define {
	my $self = shift;
	my $name = shift;
	my $doc = shift;
	my $default = shift;
	$self->{defs}{$name}{doc} = $doc;
	$self->{defs}{$name}{default} = $default;
};

sub get {
	my $self = shift;
	my $name = shift;
	if(!exists($self->{defs}{$name})) {
		die "e: definition for config file item $name does not exist!";
	}
	if(exists($SReview::Config::_private::{$name})) {
		return ${$SReview::Config::_private::{$name}};
	} else {
		return $self->{defs}{$name}{default};
	}
};

sub dump {
	my $self = shift;
	foreach my $conf(keys %{$self->{defs}}) {
		print "# " . $self->{defs}{$conf}{doc} . "\n";
		print "#" . Data::Dumper->Dump([$self->{defs}{$conf}{default}], [$conf]) . "\n";
	}
	print "# Do not remove this, perl needs it\n1;\n";
};

1;

Create a module for supporting configuration files
Create a module for supporting configuration files

This module will only return configuration variables that were
previously defined, and will allow outputting a template configuration
file based on those definitions.

When requesting a configuration value, values that are not defined in
the configuration file are returned as their default value rather than
the configured value. At the same time, we do not complain if a
configuration value is defined that we do not know about.

This allows us to easily self-document scripts, and to merge
configuration values for multiple scripts into one file.
