]> git.donarmstrong.com Git - debhelper.git/blob - dh_installman
r1655: * Added udeb support, as pioneered by di-packages-build. Understands
[debhelper.git] / dh_installman
1 #!/usr/bin/perl -w
2
3 =head1 NAME
4
5 dh_installman - install man pages into package build directories
6
7 =cut
8
9 use strict;
10 use File::Find;
11 use Debian::Debhelper::Dh_Lib;
12
13 =head1 SYNOPSIS
14
15 B<dh_installman> [S<I<debhelper options>>] [S<I<manpage ...>>]
16
17 =head1 DESCRIPTION
18
19 dh_installman is a debhelper program that handles installing
20 man pages into the correct locations in package build directories. You tell
21 it what man pages go in your packages, and it figures out where to
22 install them based on the section field in their .TH line. If you have a
23 properly formatted .TH line, your man page will be installed into the right
24 directory, with the right name (this includes proper handling of pages
25 with a subsection, like "3perl", which are placed in man3, and given an
26 extension of ".3perl"). If your .TH line is incorrect or missing, the program
27 may guess wrong based on the file extension.
28
29 It also supports translated man pages, by looking for extensions
30 like .ll.8 and .ll_LL.8
31
32 If dh_installman seems to install a man page into the wrong section or with
33 the wrong extension, this is because the man page has the wrong section
34 listed in its .TH line. Edit the man page and correct the section, and
35 dh_installman will follow suit.  See to L<man(7)> for details about the .TH
36 section. If dh_installman seems to install a man page into a directory
37 like /usr/share/man/pl/man1/, that is because your program has a
38 name like "foo.pl", and dh_installman assumes that means it is translated
39 into Polish. There is currently no support for resolving this ambiguity;
40 programs in debian should proably not have extensions like that anyway.
41
42 Any man page filenames specified as parameters will be installed into the
43 first package dh_installman is told to act on. By default, this is the
44 first binary package in debian/control, but if you use -p, -i, or -a flags,
45 it will be the first package specified by those flags.
46
47 Files named debian/package.manpages can list other man pages to be
48 installed.
49
50 After the man page installation step, dh_installman will check to see if
51 any of the man pages in the temporary directories of any of the packages it
52 is acting on contain ".so" links. If so, it changes them to symlinks.
53
54 =head1 OPTIONS
55
56 =over 4
57
58 =item B<-A>, B<--all>
59
60 Install all files specified by command line parameters in ALL packages
61 acted on.
62
63 =item I<manpage ...>
64
65 Install these man pages into the first package acted on. (Or in all
66 packages if -A is specified).
67
68 =back
69
70 =head1 NOTES
71
72 An older version of this program, L<dh_installmanpages(1)>, is still used
73 by some packages, and so is still included in debhelper.
74 It is, however, deprecated, due to its counterintuitive and inconsistent
75 interface. Use this program instead.
76
77 =cut
78
79 init();
80
81 my @sofiles;
82 my @sodests;
83
84 foreach my $package (@{$dh{DOPACKAGES}}) {
85         next if is_udeb($package);
86
87         my $tmp=tmpdir($package);
88         my $file=pkgfile($package,"manpages");
89         my @manpages;
90
91         if ($file) {
92                 @manpages=filearray($file, ".");
93         }
94
95         if (($package eq $dh{FIRSTPACKAGE} || $dh{PARAMS_ALL}) && @ARGV) {
96                 push @manpages, @ARGV;
97         }
98
99         foreach my $page (@manpages) {
100                 my $basename=basename($page);
101
102                 # Support compressed pages.
103                 my $gz='';
104                 if ($basename=~m/(.*)(\.gz)/) {
105                         $basename=$1;
106                         $gz=$2;
107                 }
108
109                 my $section;
110                 # See if there is a .TH entry in the man page. If so,
111                 # we'll pull the section field from that.
112                 if ($gz) {
113                         open (IN, "zcat $page|") or die "$page: $!";
114                 }
115                 else {
116                         open (IN, $page) or die "$page: $!";
117                 }
118                 while (<IN>) {
119                         if (/^\.TH\s+\S+\s+"?(\d+[^"\s]*)"?/) {
120                                 $section=$1;
121                                 last;
122                         }
123                 }
124                 # Failing that, we can try to get it from the filename.
125                 if (! $section) {
126                         ($section)=$basename=~m/.*\.([1-9]\S*)/;
127                 }
128
129                 # Now get the numeric component of the section.
130                 my ($realsection)=$section=~m/^(\d)/ if defined $section;
131                 
132                 # If there is no numeric section, bail.
133                 if (! $realsection) {
134                         error("Could not determine section for $page");
135                 }
136                 
137                 # Get the man page's name -- everything up to the last dot.
138                 my ($instname)=$basename=~m/^(.*)\./;
139         
140                 my $destdir="$tmp/usr/share/man/man$realsection/";
141                 # Translated man pages are typically specified by adding the
142                 # language code to the filename, so detect that and
143                 # redirect to appropriate directory, stripping the code.
144                 my ($langcode)=$basename=~m/.*\.([a-z][a-z](?:_[A-Z][A-Z])?)\.(?:[1-9]|man)/;
145                 if (defined $langcode && $langcode ne '') {
146                         $destdir="$tmp/usr/share/man/$langcode/man$realsection/";
147                         # Strip the language code from the instname.
148                         $instname=~s/\.$langcode$//;
149                 }
150                 $destdir=~tr:/:/:s; # just for looks
151
152                 if (! -e "$destdir/$instname.$section" && 
153                     ! -l "$destdir/$instname.$section") {
154                         if (! -d $destdir) {
155                                 doit "install","-d",$destdir;
156                         }
157                         doit "install","-p","-m644",$page,
158                                 "$destdir$instname.$section$gz";
159                 }
160                 
161         }
162
163         # Now the .so conversion.
164         @sofiles=@sodests=();
165         foreach my $dir (qw{usr/share/man usr/X11R6/man}) {
166                 if (-e "$tmp/$dir") {
167                         find(\&find_so_man, "$tmp/$dir");
168                 }
169         }
170         foreach my $sofile (@sofiles) {
171                 my $sodest=shift(@sodests);
172                 doit "rm","-f",$sofile;
173                 doit "ln","-sf",$sodest,$sofile;
174         }
175 }
176
177 # Check if a file is a .so man page, for use by File::Find.
178 sub find_so_man {
179         # The -s test is becuase a .so file tends to be small. We don't want
180         # to open every man page. 1024 is arbitrary.
181         if (! -f $_ || -s $_ > 1024 || -s == 0) {
182                 return;
183         }
184
185         # Test first line of file for the .so thing.
186         if (/\.gz$/) {
187                 open (SOTEST, "zcat $_|") or die "$_: $!";
188         }
189         else {
190                 open (SOTEST,$_) || die "$_: $!";
191         }
192         my $l=<SOTEST>;
193         close SOTEST;
194         if ($l=~m/\.so\s+(.*)\s*/) {
195                 my $solink=$1;
196                 # This test is here to prevent links like ... man8/../man8/foo.8
197                 if (basename($File::Find::dir) eq
198                     dirname($solink)) {
199                         $solink=basename($solink);
200                 }
201                 else {
202                         $solink="../$solink";
203                 }
204         
205                 push @sofiles,"$File::Find::dir/$_";
206                 push @sodests,$solink;
207         }
208 }
209
210 =head1 SEE ALSO
211
212 L<debhelper(7)>
213
214 This program is a part of debhelper.
215
216 =head1 AUTHOR
217
218 Joey Hess <joeyh@debian.org>
219
220 =cut