1 package SL::LxOfficeConf;
 
   6 use SL::System::Process;
 
   8 my $environment_initialized;
 
  11   my ($class, $may_fail);
 
  13   my $failed = !eval { require Config::Std; };
 
  30   my ($class, $file_name, $may_fail) = @_;
 
  32   return unless $class->safe_require($may_fail);
 
  34   # Backwards compatibility: read lx_office.conf.default if
 
  35   # kivitendo.conf.default does't exist.
 
  36   my $dir            = SL::System::Process->exe_dir;
 
  37   my $default_config = -f "${dir}/config/kivitendo.conf.default" ? 'kivitendo' : 'lx_office';
 
  38   read_config("${dir}/config/${default_config}.conf.default" => \%::lx_office_conf);
 
  39   _decode_recursively(\%::lx_office_conf);
 
  41   $file_name ||= -f "${dir}/config/kivitendo.conf" ? "${dir}/config/kivitendo.conf" : "${dir}/config/lx_office.conf";
 
  44     read_config($file_name => \ my %local_conf);
 
  45     _decode_recursively(\%local_conf);
 
  46     _flat_merge(\%::lx_office_conf, \%local_conf);
 
  50   _determine_application_paths();
 
  55 sub _decode_recursively {
 
  58   while (my ($key, $value) = each %{ $obj }) {
 
  59     if (ref($value) eq 'HASH') {
 
  60       _decode_recursively($value);
 
  62       $obj->{$key} = decode('UTF-8', $value);
 
  70   while (my ($key, $value) = each %{ $src }) {
 
  71     if (!exists $dst->{$key}) {
 
  72       $dst->{$key} = $value;
 
  75       map { $dst->{$key}->{$_} = $value->{$_} } keys %{ $value };
 
  80 sub _init_environment {
 
  81   return if $environment_initialized;
 
  83   my %key_map = ( lib  => { name => 'PERL5LIB', append_path => 1 },
 
  84                   path => { name => 'PATH',     append_path => 1 },
 
  86   my $cfg     = $::lx_office_conf{environment} || {};
 
  88   while (my ($key, $value) = each %{ $cfg }) {
 
  91     my $info = $key_map{$key} || {};
 
  92     $key     = $info->{name}  || $key;
 
  94     if ($info->{append_path}) {
 
  95       $value = ':' . $value unless $value =~ m/^:/ || !$ENV{$key};
 
  96       $value = $ENV{$key} . $value if $ENV{$key};
 
 102   $environment_initialized = 1;
 
 105 sub _determine_application_paths {
 
 106   my @paths = grep { $_ } split m/:/, $ENV{PATH};
 
 108   foreach my $key (keys %{ $::lx_office_conf{applications} }) {
 
 109     my ($program) = split m/\s+/, $::lx_office_conf{applications}->{$key};
 
 110     next if $program =~ m|/|;
 
 112     foreach my $path (@paths) {
 
 113       next unless -f "${path}/${program}";
 
 114       $::lx_office_conf{applications}->{$key} = "${path}/" . $::lx_office_conf{applications}->{$key};