package LiCoM::Config;

use strict;
use warnings;

use Exporter;

@LiCoM::Config::EXPORT_OK = ('get_config');
@LiCoM::Config::ISA = ('Exporter');

return (1);

sub get_config
{
	my @files = ('/etc/licom/licom.conf');

	if (@_)
	{
		@files = @_;
	}
	elsif (defined ($ENV{'HOME'}) and (-d $ENV{'HOME'}))
	{
		push (@files, $ENV{'HOME'} . '/.licomrc');
	}

	for (@files)
	{
		my $file = $_;
		next unless (-r $file);

		read_file ($file, $config);
	}

	return ($config);
}

sub read_file
{
	my $file   = @_ ? shift : '/etc/licom/licom.conf';
	my $config = @_ ? shift : {};
	my $fh;

	open ($fh, "< $file") or die ("open ($file): $!");
	for (<$fh>)
	{
		chomp;
		my $line = $_;

		if ($line =~ m/^(\w+):\s*"(.+)"\s*$/)
		{
			my $key = lc ($1);
			my $val = $2;

			$config->{$key} = $val;
		}
	}

	close ($fh);

	return ($config);
}
