aboutsummaryrefslogtreecommitdiffstats
path: root/del-syslog
blob: 185b6ae822aeaceb843a1e11fa2928e2aadd3a9f (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
#!/usr/bin/perl
# rpm helper scriptlet to remove a syslog entry (sysklogd and syslog-ng)
# $Id$
use strict;

die <<EOF if @ARGV < 2;
usage: $0 <pkg> <nb>
EOF
my ($package, $number) = @ARGV;

# don't do anything for upgrade
exit(0) if $number == 1;

if (-f '/etc/syslog.conf') {
    del_syslog_entry($package, '/etc/syslog.conf');
    system(qw/service syslog condrestart/);
}

if (-f '/etc/syslog-ng.conf') {
    del_syslog_entry($package, '/etc/syslog-ng.conf');
    system(qw/service syslog-ng condrestart/);
}

sub del_syslog_entry {
    my ($package, $file) = @_;

    my $content;
    open(my $in, '<', $file)
        or die "Can't open $file for reading: $!";
    while (my $line = <$in>) {
        if ($line =~ /^# BEGIN: Automatically added by $package installation$/) {
            while ($line = <$in>) {
                last if $line =~ /^# END$/;
            }
        } else {
            $content .= $line;
        }
    }

    open(my $out, '>', $file)
        or die "Can't open $file for writing: $!";
    print $out $content;
    close($out);
}