summaryrefslogtreecommitdiffstats
path: root/perl-install/standalone/drakups
blob: bb65aae02515ae9460b10d293a3bc1d518c268ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/perl

use strict;
use lib qw(/usr/lib/libDrakX);
use common;
use detect_devices;

#------------------------------------------------------------------
# to be moved in MDK::Common as a generalisation of (read|update)_gnomekderc
# once cooker is unfrozen and major releases are allowed again.
#
# but there's a "bug" that prevent factorization: current
# (read|update)_gnomekderc keep comments ... and minimize intrusions

sub read_all_gnomekderc {
    my ($file) = @_;
    my (@a, %h);
    my ($section, @section);
    foreach (MDK::Common::File::cat_($file)) {
	s/#.*//;
	if (/^\s*\[(.*)\]/) {
	    # remove all "if $section" tests if options out of sections is needed
	    push @a, [ $section, [ @section ] ] if $section;
	    $section = $1;
	    $h{$section} = { };
	    @section = ();
	} elsif (/^\s*(.*?)=(.*)/) {
	    push @section, $1;
	    $h{$section}{$1} = $2;
	}
    }
    push @a, [ $section, [ @section ] ] if $section;
    \@a, \%h;
}

# lost comment but keep sections & options order:
sub update_all_gnomekderc {
    my ($file, $list, $hvalues) = @_;
    output($file, map {
	my ($section, $items) = @$_;
	"\n[$section]\n", map { $_ . "=" . $hvalues->{$section}{$_} . "\n" } @$items;
    } @$list);
}
#------------------------------------------------------------------

#------------------------------------------------------------------
# UPS autoconfig:

my $file = "/etc/ups/ups.conf";
my ($sections, $sec_contents) = read_all_gnomekderc($file);

my @ups_devices = sort values %$sec_contents;

foreach my $ups_device (detect_devices::getUPS()) {
    my $str = $ups_device->{description};
    if (!find { $str eq $_->{port} } @ups_devices) {
        $str =~ s/ /_/g;
        push @$sections, [ $str, [ qw(driver port) ] ];
        $sec_contents->{$str} = {
                                 port => $ups_device->{bus} eq 'USB' ? "/dev/usb/hid/hiddev0" : $ups_device->{device},
                                 driver => $ups_device->{bus} eq 'USB' ? "hidups" : "",
                                };
    }
}

update_all_gnomekderc($file, $sections, $sec_contents);