]> git.donarmstrong.com Git - debhelper.git/blob - dh_installman
releasing version 6.0.9
[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, or by use of the --language switch.
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 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. Use --language=C to avoid this.
40
41 Any man page filenames specified as parameters will be installed into the
42 first package dh_installman is told to act on. By default, this is the
43 first binary package in debian/control, but if you use -p, -i, or -a flags,
44 it will be the first package specified by those flags.
45
46 Files named debian/package.manpages can list other man pages to be
47 installed.
48
49 After the man page installation step, dh_installman will check to see if
50 any of the man pages in the temporary directories of any of the packages it
51 is acting on contain ".so" links. If so, it changes them to symlinks.
52
53 Also, dh_installman will use man to guess the character encoding of each
54 manual page and convert it to UTF-8. If the guesswork fails for some
55 reason, you can override it using an encoding declaration. See
56 L<manconv(1)> for details.
57
58 =head1 OPTIONS
59
60 =over 4
61
62 =item B<-A>, B<--all>
63
64 Install all files specified by command line parameters in ALL packages
65 acted on.
66
67 =item B<--language>=ll
68
69 Use this to specify that the man pages being acted on are written in the
70 specified language.
71
72 =item I<manpage ...>
73
74 Install these man pages into the first package acted on. (Or in all
75 packages if -A is specified).
76
77 =back
78
79 =head1 NOTES
80
81 An older version of this program, L<dh_installmanpages(1)>, is still used
82 by some packages, and so is still included in debhelper.
83 It is, however, deprecated, due to its counterintuitive and inconsistent
84 interface. Use this program instead.
85
86 =cut
87
88 init();
89
90 my @sofiles;
91 my @sodests;
92
93 foreach my $package (@{$dh{DOPACKAGES}}) {
94         next if is_udeb($package);
95
96         my $tmp=tmpdir($package);
97         my $file=pkgfile($package,"manpages");
98         my @manpages;
99
100         @manpages=filearray($file, ".") if $file;
101
102         if (($package eq $dh{FIRSTPACKAGE} || $dh{PARAMS_ALL}) && @ARGV) {
103                 push @manpages, @ARGV;
104         }
105
106         foreach my $page (@manpages) {
107                 my $basename=basename($page);
108
109                 # Support compressed pages.
110                 my $gz='';
111                 if ($basename=~m/(.*)(\.gz)/) {
112                         $basename=$1;
113                         $gz=$2;
114                 }
115
116                 my $section;
117                 # See if there is a .TH entry in the man page. If so,
118                 # we'll pull the section field from that.
119                 if ($gz) {
120                         open (IN, "zcat $page|") or die "$page: $!";
121                 }
122                 else {
123                         open (IN, $page) or die "$page: $!";
124                 }
125                 while (<IN>) {
126                         if (/^\.TH\s+\S+\s+"?(\d+[^"\s]*)"?/) {
127                                 $section=$1;
128                                 last;
129                         }
130                 }
131                 # Failing that, we can try to get it from the filename.
132                 if (! $section) {
133                         ($section)=$basename=~m/.*\.([1-9]\S*)/;
134                 }
135
136                 # Now get the numeric component of the section.
137                 my ($realsection)=$section=~m/^(\d)/ if defined $section;
138                 if (! $realsection) {
139                         error("Could not determine section for $page");
140                 }
141                 
142                 # Get the man page's name -- everything up to the last dot.
143                 my ($instname)=$basename=~m/^(.*)\./;
144         
145                 my $destdir="$tmp/usr/share/man/man$realsection/";
146                 my $langcode;
147                 if (! defined $dh{LANGUAGE} || ! exists $dh{LANGUAGE}) {
148                         # Translated man pages are typically specified by adding the
149                         # language code to the filename, so detect that and
150                         # redirect to appropriate directory, stripping the code.
151                         ($langcode)=$basename=~m/.*\.([a-z][a-z](?:_[A-Z][A-Z])?)\.(?:[1-9]|man)/;
152                 }
153                 elsif ($dh{LANGUAGE} ne 'C') {
154                         $langcode=$dh{LANGUAGE};
155                 }
156                 
157                 if (defined $langcode && $langcode ne '') {
158                         # Strip the language code from the instname.
159                         $instname=~s/\.$langcode$//;
160                 }
161                 
162                 if (defined $langcode && $langcode ne '') {
163                         $destdir="$tmp/usr/share/man/$langcode/man$realsection/";
164                 }
165                 $destdir=~tr:/:/:s; # just for looks
166                 my $instpage="$destdir/$instname.$section";
167
168                 next if -l $instpage;
169                 next if compat(5) && -e $instpage;
170
171                 if (! -d $destdir) {
172                         doit "install","-d",$destdir;
173                 }
174                 if ($gz) {
175                         complex_doit "zcat \Q$page\E > \Q$instpage\E";
176                 }
177                 else {
178                         doit "install","-p","-m644",$page,$instpage;
179                 }
180         }
181
182         # Now the .so conversion.
183         @sofiles=@sodests=();
184         foreach my $dir (qw{usr/share/man usr/X11R6/man}) {
185                 if (-e "$tmp/$dir") {
186                         find(\&find_so_man, "$tmp/$dir");
187                 }
188         }
189         foreach my $sofile (@sofiles) {
190                 my $sodest=shift(@sodests);
191                 doit "rm","-f",$sofile;
192                 doit "ln","-sf",$sodest,$sofile;
193         }
194
195         # Now utf-8 conversion.
196         foreach my $dir (qw{usr/share/man usr/X11R6/man}) {
197                 if (-e "$tmp/$dir") {
198                         find(sub {
199                                 return if ! -f $_ || -l $_;
200                                 complex_doit "man --recode UTF-8 ./\Q$_\E > \Q$_.new\E";
201                                 doit "chmod",644,"$_.new";
202                                 doit "mv","-f","$_.new",$_;
203                         }, "$tmp/$dir");
204                 }
205         }
206 }
207
208 # Check if a file is a .so man page, for use by File::Find.
209 sub find_so_man {
210         # The -s test is becuase a .so file tends to be small. We don't want
211         # to open every man page. 1024 is arbitrary.
212         if (! -f $_ || -s $_ > 1024 || -s == 0) {
213                 return;
214         }
215
216         # Test first line of file for the .so thing.
217         if (/\.gz$/) {
218                 open (SOTEST, "zcat $_|") or die "$_: $!";
219         }
220         else {
221                 open (SOTEST,$_) || die "$_: $!";
222         }
223         my $l=<SOTEST>;
224         close SOTEST;
225
226         if (! defined $l) {
227                 error("failed to read $_");
228         }
229         
230         if ($l=~m/\.so\s+(.*)\s*/) {
231                 my $solink=$1;
232                 # This test is here to prevent links like ... man8/../man8/foo.8
233                 if (basename($File::Find::dir) eq
234                     dirname($solink)) {
235                         $solink=basename($solink);
236                 }
237                 else {
238                         $solink="../$solink";
239                 }
240         
241                 push @sofiles,"$File::Find::dir/$_";
242                 push @sodests,$solink;
243         }
244 }
245
246 =head1 SEE ALSO
247
248 L<debhelper(7)>
249
250 This program is a part of debhelper.
251
252 =head1 AUTHOR
253
254 Joey Hess <joeyh@debian.org>
255
256 =cut