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