]> git.donarmstrong.com Git - debhelper.git/blob - dh_installmanpages
r421: use strict
[debhelper.git] / dh_installmanpages
1 #!/usr/bin/perl -w
2 #
3 # Automatically find and install man pages. However, do not install any man 
4 # pages listed on the command line.
5 # Also change man pages with .so commands in them into symlinks.
6 #
7 # This is a little bit (hah!) DWIMish, but still very handy.
8
9 use strict;
10 use File::Find;
11 use Debian::Debhelper::Dh_Lib;
12 init();
13
14 # Check if a file is a man page, for use by File::Find.
15 my @manpages;
16 my @allpackages;
17 sub find_man {
18         # Does its filename look like a man page?
19         # .ex files are examples installed by deb-make,
20         # we don't want those, or .in files, which are
21         # from configure, nor do we want CVS .#* files.
22         if (! (-f $_ && /^.*\.[1-9].*$/ && ! /\.(ex|in)$/ && ! /^\.#/)) {
23                 return;
24         }
25         
26         # It's not in a tmp directory is it?
27         if ($File::Find::dir=~m:debian/.*tmp.*:) {
28                 return;
29         }
30         foreach my $dir (@allpackages) {
31                 if ($File::Find::dir=~m:debian/\Q$dir\E:) {
32                         return;
33                 }
34         }
35         
36         # And file does think it's a real man page?
37         my $type=`file -z $_`;
38         if ($type !~ m/:.*roff/) {
39                 return;
40         }
41
42         # Good enough.
43         push @manpages,"$File::Find::dir/$_";
44 }
45
46 # Check if a file is a .so man page, for use by File::Find.
47 my @sofiles;
48 my @sodests;
49 sub find_so_man {
50         # The -s test is becuase a .so file tends to be small. We don't want
51         # to open every man page. 1024 is arbitrary.
52         if (! -f $_ || -s $_ > 1024) {
53                 return;
54         }
55
56         # Test first line of file for the .so thing.
57         open (SOTEST,$_);
58         my $l=<SOTEST>;
59         close SOTEST;
60         if ($l=~m/\.so\s+(.*)/) {
61                 my $solink=$1;
62                 # This test is here to prevent links like ... man8/../man8/foo.8
63                 if (Debian::Debhelper::Dh_Lib::basename($File::Find::dir) eq
64                     Debian::Debhelper::Dh_Lib::dirname($solink)) {
65                         $solink=Debian::Debhelper::Dh_Lib::basename($solink);
66                 }
67                 else {
68                         $solink="../$solink";
69                 }
70         
71                 push @sofiles,"$File::Find::dir/$_";
72                 push @sodests,$solink;
73         }
74 }
75
76 foreach my $package (@{$dh{DOPACKAGES}}) {
77         my $tmp=tmpdir($package);
78
79         # Find all filenames that look like man pages.
80         @manpages=();
81         @allpackages=GetPackages('');
82         find(\&find_man,'.'); # populates @manpages
83         
84         foreach my $page (@manpages) {
85                 $page=~s:^\./::; # just for looks
86                 
87                 my $basename=basename($page);
88                 
89                 # Skip all files listed on command line.
90                 my $install=1;
91                 foreach my $skip (@ARGV) {
92                         # Look at basename of what's on connect line
93                         # for backwards compatability.
94                         if ($basename eq basename($skip)) {
95                                 $install=undef;
96                                 last;
97                         }
98                 }
99                 
100                 if ($install) {
101                         my $extdir="share";
102                         # Handle X man pages specially.
103                         if ($basename=~/x$/) {
104                                 $extdir="X11R6";
105                         }
106                         
107                         my ($section)=$basename=~m/.*\.([1-9])/;
108                         
109                         my $destdir="$tmp/usr/$extdir/man/man$section/";
110                         
111                         # Handle translated man pages.
112                         my $instname=$basename;
113                         my ($langcode)=$basename=~m/.*\.([a-z][a-z])\.([1-9])/;
114                         if (defined $langcode && $langcode ne '') {
115                                 $destdir="$tmp/usr/$extdir/man/$langcode/man$section/";
116                                 $instname=~s/\.$langcode\./\./;
117                         }
118                         
119                         $destdir=~tr:/:/:s; # just for looks
120                         
121                         if (! -e "$destdir/$basename" && !-l "$destdir/$basename") {
122                                 if (! -d $destdir) {
123                                         doit "install","-d",$destdir;
124                                 }
125                                 doit "install","-p","-m644",$page,$destdir.$instname;
126                         }
127                 }
128         }
129         
130         # Now the .so conversion.
131         @sofiles=@sodests=();
132         foreach my $dir (qw{usr/share/man usr/X11R6/man}) {
133                 if (-e "$tmp/$dir") {
134                         find(\&find_so_man, "$tmp/$dir");
135                 }
136         }
137         foreach my $sofile (@sofiles) {
138                 my $sodest=shift(@sodests);
139                 doit "rm","-f",$sofile;
140                 doit "ln","-sf",$sodest,$sofile;
141         }
142 }