~ubuntu-branches/ubuntu/oneiric/localization-config/oneiric

« back to all changes in this revision

Viewing changes to conffiles.d/common/editconfig.pl

  • Committer: Bazaar Package Importer
  • Author(s): Konstantinos Margaritis
  • Date: 2004-12-10 19:10:18 UTC
  • Revision ID: james.westby@ubuntu.com-20041210191018-wdjfbljdie570dld
Tags: 0.109
* Translations
  - Added Finnish by Tapio Lehtonen. Closes: #281040
  - Fixed Spanish translation (was too long). Closes: #284570

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/perl
 
2
 
 
3
#
 
4
# editconfig.pl: Holds routines used by some scripts
 
5
# to manipulate config files.
 
6
#
 
7
 
 
8
# Return 0 if the file wasn't changed and 1 if the file was updated
 
9
sub AppendIfMissingLine {
 
10
    my ($filename, $line) = @_;
 
11
    my $updated = 0;
 
12
 
 
13
    my @lines;
 
14
    if (open(FILE, "<$filename")) {
 
15
        @lines = <FILE>;
 
16
        close(FILE);
 
17
    }
 
18
 
 
19
    $line .= "\n";
 
20
 
 
21
    if ( ! grep /^$line$/, @lines ) {
 
22
        $updated = 1;
 
23
        push(@lines, $line);
 
24
    }
 
25
 
 
26
    if ($updated) {
 
27
        print "Updating $filename\n" if $debug;
 
28
        open(FILE, ">$filename.new") || die "Unable to write $filename.new";
 
29
        print FILE @lines;
 
30
        close(FILE);
 
31
        rename "$filename.new", $filename;
 
32
    }
 
33
    return $updated;
 
34
}
 
35
 
 
36
# Return 0 if nothing changed, and 1 if the file was updated
 
37
sub UpdateOrAppendVariable {
 
38
    my ($filename, $variable, $content) = @_;
 
39
    my $updated = 0;
 
40
    my $newline = "$variable=$content\n";
 
41
 
 
42
    my @lines;
 
43
    if (open(FILE, "<$filename")) {
 
44
        @lines = <FILE>;
 
45
        close(FILE);
 
46
    }
 
47
 
 
48
    if (grep /^$variable=/, @lines ) {
 
49
        @lines = map { if ($_ =~ m/^$variable=/ && $_ ne $newline)
 
50
                       { $updated = 1;
 
51
                         $newline; }
 
52
                       else
 
53
                       { $_; }
 
54
                   } @lines;
 
55
    } else {
 
56
        $updated = 1;
 
57
        push(@lines, $newline);
 
58
    }
 
59
    if ($updated) {
 
60
        print "Updating $filename\n" if $debug;
 
61
        open(FILE, ">$filename.new") || die "Unable to write $filename.new";
 
62
        print FILE @lines;
 
63
        close(FILE);
 
64
        rename "$filename.new", $filename;
 
65
    }
 
66
    return $updated;
 
67
}
 
68
 
 
69
1;
 
 
b'\\ No newline at end of file'