1 package SL::LxOfficeConf;
7 my $environment_initialized;
10 my ($class, $may_fail);
12 my $failed = !eval { require Config::Std; };
29 my ($class, $file_name, $may_fail) = @_;
31 return unless $class->safe_require($may_fail);
33 # Backwards compatibility: read lx_office.conf.default if
34 # kivitendo.conf.default does't exist.
35 my $default_config = -f "config/kivitendo.conf.default" ? 'kivitendo' : 'lx_office';
36 read_config("config/${default_config}.conf.default" => \%::lx_office_conf);
37 _decode_recursively(\%::lx_office_conf);
39 $file_name ||= -f 'config/kivitendo.conf' ? 'config/kivitendo.conf' : 'config/lx_office.conf';
42 read_config($file_name => \ my %local_conf);
43 _decode_recursively(\%local_conf);
44 _flat_merge(\%::lx_office_conf, \%local_conf);
48 _determine_application_paths();
53 sub _decode_recursively {
56 while (my ($key, $value) = each %{ $obj }) {
57 if (ref($value) eq 'HASH') {
58 _decode_recursively($value);
60 $obj->{$key} = decode('UTF-8', $value);
68 while (my ($key, $value) = each %{ $src }) {
69 if (!exists $dst->{$key}) {
70 $dst->{$key} = $value;
73 map { $dst->{$key}->{$_} = $value->{$_} } keys %{ $value };
78 sub _init_environment {
79 return if $environment_initialized;
81 my %key_map = ( lib => { name => 'PERL5LIB', append_path => 1 },
82 path => { name => 'PATH', append_path => 1 },
84 my $cfg = $::lx_office_conf{environment} || {};
86 while (my ($key, $value) = each %{ $cfg }) {
89 my $info = $key_map{$key} || {};
90 $key = $info->{name} || $key;
92 if ($info->{append_path}) {
93 $value = ':' . $value unless $value =~ m/^:/ || !$ENV{$key};
94 $value = $ENV{$key} . $value if $ENV{$key};
100 $environment_initialized = 1;
103 sub _determine_application_paths {
104 my @paths = grep { $_ } split m/:/, $ENV{PATH};
106 foreach my $key (keys %{ $::lx_office_conf{applications} }) {
107 my ($program) = split m/\s+/, $::lx_office_conf{applications}->{$key};
108 next if $program =~ m|/|;
110 foreach my $path (@paths) {
111 next unless -f "${path}/${program}";
112 $::lx_office_conf{applications}->{$key} = "${path}/" . $::lx_office_conf{applications}->{$key};