aboutsummaryrefslogtreecommitdiffstats
path: root/normalize_man_pages
blob: 2a93927e25c6c7294ccb288679e5388f1255987f (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
#!/usr/bin/perl
# $Id: fix_eol 242516 2008-05-14 08:54:21Z guillomovitch $
# ensure all man pages contains a comment line

use strict;
use warnings;
use File::Find;
use File::Temp;

my $buildroot = $ENV{RPM_BUILD_ROOT};
die "No build root defined" unless $buildroot;
die "Invalid build root" unless -d $buildroot;
# normalize build root
$buildroot =~ s|/$||;

my $mandir=`rpm --eval %{_mandir}`;
chomp $mandir;

my $exclude_string = join('|',
    map { '(:?' . quotemeta($_) . ')' }
    $ENV{EXCLUDE_FROM_MAN_PAGE_NORMALIZATION} ?
        split(' ', $ENV{EXCLUDE_FROM_MAN_PAGE_NORMALIZATION}) : ()
);
my $exclude_pattern = qr/$exclude_string/;

find(\&normalize, $buildroot . $mandir);

sub normalize {
    # skip everything but files
    return unless -f $_;
    # skip symlinks
    return if -l $_;
    # skip excluded files
    return if $exclude_string && $File::Find::name =~ $exclude_pattern;

    # check if first line begin with a comment
    open(my $in, '<', $_) or die "Unable to open file $_: $!";
    my $line =  <$in>;
    if (defined $line && $line !~ /^\.\\"/) {
        # process all file
        my $out = File::Temp->new(DIR => '.', UNLINK => 0);
        print $out <<'EOF';
.\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
.\" make the file command recognize this file as a roff text
.\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
EOF
        print $out $line;
        while (defined ($line = <$in>)) {
            print $out $line;
        }
        my $tmp = $out->filename;
        $out = undef;

        # rename file, taking care to keep original permissions
        my $perms = (stat $_)[2] & 07777;
        rename($tmp, $_) or die "Unable to rename $tmp to $_: $!";
        chmod($perms, $_);
    }
    close($in);
}