###
###  Copyright 2000-2007 University of Illinois Board of Trustees
###  All rights reserved. 
###
###  PSGConf::DataStore::AppConfig - config file data store for psgconf
###
###  Campus Information Technologies and Educational Services
###  University of Illinois at Urbana-Champaign
###


package PSGConf::DataStore::AppConfig;

use PSGConf::DataStore;
use AppConfig;

use strict;

our @ISA = qw(PSGConf::DataStore);

###############################################################################
###  read config
###############################################################################

sub read_config
{
	my ($self, $psgconf) = @_;
	my ($file, $cfg, %vars, $href);

	$cfg = AppConfig->new({CASE=>1});

	### Need to define each type of directive, so AppConfig
	### creates the correct type.
	$href = $psgconf->get_all_data();
	map {
		if ( $href->{$_}->[0] eq 'PSGConf::Data::Enum' ) {
			$cfg->define($_, { ARGCOUNT => AppConfig::ARGCOUNT_NONE } );
		} elsif ( $href->{$_}->[0] eq 'PSGConf::Data::List' ) {
			$cfg->define($_, { ARGCOUNT => AppConfig::ARGCOUNT_LIST } );
		} elsif ( $href->{$_}->[0] eq 'PSGConf::Data::Hash' ) {
			$cfg->define($_, { ARGCOUNT => AppConfig::ARGCOUNT_HASH } );
		} else {
			$cfg->define($_, { ARGCOUNT => AppConfig::ARGCOUNT_ONE } );
		}
	} keys %$href;

	###
	### This looks backwards, but using $psgconf first is so command
	### line options for a config file get processed before a default
	### from the class creation.
	###
	$file = defined($psgconf->{'config_file'})
		 		? $psgconf->{'config_file'}
		 		: $self->{'config_file'};

	$cfg->file($file)
		|| die $0 . ": cannot read config file '$file'\n";

	%vars = $cfg->varlist('.');
	$psgconf->{'data'} = \%vars;
}


###############################################################################
###  documentation
###############################################################################

1;

__END__

=head1 NAME

PSGConf::DataStore::AppConfig - config file data store class for psgconf

=head1 SYNOPSIS

In F<psgconf_modules>:

  DataStore PSGConf::DataStore::AppConfig config_file=/etc/psg.ini

=head1 DESCRIPTION

The B<PSGConf::DataStore::AppConfig> module provides a data store
implementation that reads configuration information from a local config
file (typically F</etc/psg.ini>).  It supports the following methods:

=over 4

=item new()

The constructor.  Its arguments are a reference to the B<PSGConf>
object and an optional list of attribute settings in the form
"attribute=value".  The following attributes are supported:

=over 4

=item I<config_file>

The absolute path to the default config file location.  This value is
overriden by the I<config_file> attribute of the B<PSGConf> object.

=back

=item read_config()

Reads the config file.  Its argument is a reference to the B<PSGConf>
object.

=back

The config file parser is implemented using B<AppConfig>.
See L<AppConfig> for details on the config file format.

=head1 SEE ALSO

L<perl>

L<PSGConf>

L<psgconf-intro>

L<AppConfig>

=cut



syntax highlighted by Code2HTML, v. 0.9.1