X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FLxOfficeConf.pm;h=3cd462cff1a743e4ec3c3bd4cc06f068f31bbf30;hb=dc3f6120f9bbacaa028e554d7fa71e481d4497b4;hp=7690eebdabc9af004b4c2d0572af7a2bf022da7c;hpb=9e0b2baff3315e53a0e0aa9e66cae85e474def7a;p=kivitendo-erp.git diff --git a/SL/LxOfficeConf.pm b/SL/LxOfficeConf.pm index 7690eebda..3cd462cff 100644 --- a/SL/LxOfficeConf.pm +++ b/SL/LxOfficeConf.pm @@ -2,22 +2,51 @@ package SL::LxOfficeConf; use strict; -use Config::Std; -use Encode; +my $environment_initialized; + +sub safe_require { + my ($class, $may_fail); + my $failed; + $failed = !eval { + require Config::Std; + require Encode; + }; + + if ($failed) { + if ($may_fail) { + warn $@; + return 0; + } else { + die $@; + } + } + + Config::Std->import; + Encode->import; + + return 1; +} sub read { - my ($class, $file_name) = @_; + my ($class, $file_name, $may_fail) = @_; + + return unless $class->safe_require($may_fail); - read_config 'config/lx_office.conf.default' => %::lx_office_conf; + read_config('config/lx_office.conf.default' => \%::lx_office_conf); _decode_recursively(\%::lx_office_conf); $file_name ||= 'config/lx_office.conf'; if (-f $file_name) { - read_config $file_name => my %local_conf; + read_config($file_name => \ my %local_conf); _decode_recursively(\%local_conf); _flat_merge(\%::lx_office_conf, \%local_conf); } + + _init_environment(); + _determine_application_paths(); + + return 1; } sub _decode_recursively { @@ -45,4 +74,44 @@ sub _flat_merge { } } +sub _init_environment { + return if $environment_initialized; + + my %key_map = ( lib => { name => 'PERL5LIB', append_path => 1 }, + path => { name => 'PATH', append_path => 1 }, + ); + my $cfg = $::lx_office_conf{environment} || {}; + + while (my ($key, $value) = each %{ $cfg }) { + next unless $value; + + my $info = $key_map{$key} || {}; + $key = $info->{name} || $key; + + if ($info->{append_path}) { + $value = ':' . $value unless $value =~ m/^:/ || !$ENV{$key}; + $value = $ENV{$key} . $value if $ENV{$key}; + } + + $ENV{$key} = $value; + } + + $environment_initialized = 1; +} + +sub _determine_application_paths { + my @paths = grep { $_ } split m/:/, $ENV{PATH}; + + foreach my $key (keys %{ $::lx_office_conf{applications} }) { + my ($program) = split m/\s+/, $::lx_office_conf{applications}->{$key}; + next if $program =~ m|/|; + + foreach my $path (@paths) { + next unless -f "${path}/${program}"; + $::lx_office_conf{applications}->{$key} = "${path}/" . $::lx_office_conf{applications}->{$key}; + last; + } + } +} + 1;