File: editconfig.pl

package info (click to toggle)
localization-config 1.05
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 668 kB
  • ctags: 44
  • sloc: perl: 3,170; sh: 67; makefile: 38
file content (93 lines) | stat: -rwxr-xr-x 2,385 bytes parent folder | download | duplicates (4)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/perl

#
# editconfig.pl: Holds routines used by some scripts
# to manipulate config files.
#

require '/usr/lib/localization-config/common/log.pl';

# Return 0 if the file wasn't changed and 1 if the file was updated
sub AppendIfMissingLine {
    my ($filename, $line) = @_;
    my $updated = 0;

    my @lines;
    if (open(FILE, "<$filename")) {
        @lines = <FILE>;
        close(FILE);
    }

    $line .= "\n";

    if ( ! grep /^$line$/, @lines ) {
        $updated = 1;
        push(@lines, $line);
    }

    if ($updated) {
        log_msg("$0: Updating $filename");
        open(FILE, ">$filename.new") || log_die("$0: Unable to write $filename.new");
        print FILE @lines;
        close(FILE);
        rename "$filename.new", $filename;
    }
    return $updated;
}

# Return 0 if nothing changed, and 1 if the file was updated
sub UpdateOrAppendVariable {
    my ($filename, $variable, $content, $delimeter, $section, $prefix) = @_;
    my $updated = 0;
    my $newline;
    if (defined($prefix)) {
        $newline = "\t";
    }
    $newline .= "$variable$delimeter$content\n";

    my @lines;
    if (open(FILE, "<$filename")) {
        @lines = <FILE>;
        close(FILE);
    }

    if (defined($section)) {
        if (! grep /\[$section\]/, @lines ) {
            $updated = 1;
            push(@lines, "\[$section\]\n");
        }
    }
    
    if (grep /$variable.*$delimeter/, @lines ) {
        @lines = map { if ($_ =~ m/$variable.*$delimeter/ && ! ($_ =~ /$variable.*$content/) )
                       { $updated = 1;
                         $newline; }
                       else
                       { $_; }
                   } @lines;
    } else {
        if (defined($section)) {
            $newline = "\[$section\]\n".$newline;
            @lines = map { if ($_ =~ m/^\[$section\]/ )
                        { $updated = 1;
                            $newline; }
                        else
                        { $_; }
                    } @lines;
        } else {
            $updated = 1;
            push(@lines, $newline);
        }
    }
    
    if ($updated) {
        log_msg("$0: Updating $filename");
        open(FILE, ">$filename.new") || log_die("$0: Unable to write $filename.new");
        print FILE @lines;
        close(FILE);
        rename "$filename.new", $filename;
    }
    return $updated;
}

1;