More and more group management changes.. This is going to be messy..
[licom.git] / lib / LiCoM / Config.pm
1 package LiCoM::Config;
2
3 use strict;
4 use warnings;
5
6 use Exporter;
7
8 @LiCoM::Config::EXPORT_OK = ('get_config');
9 @LiCoM::Config::ISA = ('Exporter');
10
11 return (1);
12
13 sub get_config
14 {
15         my @files = ('/etc/licom/licom.conf');
16
17         if (@_)
18         {
19                 @files = @_;
20         }
21         elsif (defined ($ENV{'HOME'}) and (-d $ENV{'HOME'}))
22         {
23                 push (@files, $ENV{'HOME'} . '/.licomrc');
24         }
25
26         for (@files)
27         {
28                 my $file = $_;
29                 next unless (-r $file);
30
31                 read_file ($file, $config);
32         }
33
34         return ($config);
35 }
36
37 sub read_file
38 {
39         my $file   = @_ ? shift : '/etc/licom/licom.conf';
40         my $config = @_ ? shift : {};
41         my $fh;
42
43         open ($fh, "< $file") or die ("open ($file): $!");
44         for (<$fh>)
45         {
46                 chomp;
47                 my $line = $_;
48
49                 if ($line =~ m/^(\w+):\s*"(.+)"\s*$/)
50                 {
51                         my $key = lc ($1);
52                         my $val = $2;
53
54                         $config->{$key} = $val;
55                 }
56         }
57
58         close ($fh);
59
60         return ($config);
61 }