1 package SL::LxOfficeConf;
 
   7 my $environment_initialized;
 
  10   my ($class, $may_fail) = @_;
 
  14     require SL::System::Process;
 
  31   my ($class, $file_name, $may_fail) = @_;
 
  33   return unless $class->safe_require($may_fail);
 
  35   # Backwards compatibility: read lx_office.conf.default if
 
  36   # kivitendo.conf.default does't exist.
 
  37   my $dir            = SL::System::Process->exe_dir;
 
  38   my $default_config = -f "${dir}/config/kivitendo.conf.default" ? 'kivitendo' : 'lx_office';
 
  39   read_config("${dir}/config/${default_config}.conf.default" => \%::lx_office_conf);
 
  40   _decode_recursively(\%::lx_office_conf);
 
  42   $file_name ||= -f "${dir}/config/kivitendo.conf" ? "${dir}/config/kivitendo.conf" : "${dir}/config/lx_office.conf";
 
  45     read_config($file_name => \ my %local_conf);
 
  46     _decode_recursively(\%local_conf);
 
  47     _flat_merge(\%::lx_office_conf, \%local_conf);
 
  51   _determine_application_paths();
 
  56 sub _decode_recursively {
 
  59   while (my ($key, $value) = each %{ $obj }) {
 
  60     if (ref($value) eq 'HASH') {
 
  61       _decode_recursively($value);
 
  63       $obj->{$key} = decode('UTF-8', $value);
 
  71   while (my ($key, $value) = each %{ $src }) {
 
  72     if (!exists $dst->{$key}) {
 
  73       $dst->{$key} = $value;
 
  76       map { $dst->{$key}->{$_} = $value->{$_} } keys %{ $value };
 
  81 sub _init_environment {
 
  82   return if $environment_initialized;
 
  84   my %key_map = ( lib  => { name => 'PERL5LIB', append_path => 1 },
 
  85                   path => { name => 'PATH',     append_path => 1 },
 
  87   my $cfg     = $::lx_office_conf{environment} || {};
 
  89   while (my ($key, $value) = each %{ $cfg }) {
 
  92     my $info = $key_map{$key} || {};
 
  93     $key     = $info->{name}  || $key;
 
  95     if ($info->{append_path}) {
 
  96       $value = ':' . $value unless $value =~ m/^:/ || !$ENV{$key};
 
  97       $value = $ENV{$key} . $value if $ENV{$key};
 
 103   $environment_initialized = 1;
 
 106 sub _determine_application_paths {
 
 107   my @paths = grep { $_ } split m/:/, $ENV{PATH};
 
 109   foreach my $key (keys %{ $::lx_office_conf{applications} }) {
 
 110     my ($program) = split m/\s+/, $::lx_office_conf{applications}->{$key};
 
 111     next if $program =~ m|/|;
 
 113     foreach my $path (@paths) {
 
 114       next unless -f "${path}/${program}";
 
 115       $::lx_office_conf{applications}->{$key} = "${path}/" . $::lx_office_conf{applications}->{$key};