]> git.donarmstrong.com Git - debhelper.git/blob - dh_installman
r493: * dh_installman: documented translated man page support, and made it work
[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   dh_installman [debhelper options] [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 and their filename
23 extention. It also supports translated man pages, by looking for extentions
24 like .ll.8 and .ll_LL.8
25
26 Any man page filenames specified as parameters will be installed into the
27 first package dh_installman is told to act on. By default, this is the
28 first binary package in debian/control, but if you use -p, -i, or -a flags,
29 it will be the first package specified by those flags.
30
31 Files named debian/package.manpages can list other man pages to be
32 installed.
33
34 After the man page installation step, dh_installman will check to see if
35 any of the man pages in the temporary directories of any of the packages it
36 is acting on contain ".so" links. If so, it changes them to symlinks.
37
38 =head1 OPTIONS
39
40 =over 4
41
42 =item B<-A>, B<--all>
43
44 Install all files specified by command line parameters in ALL packages
45 acted on.
46
47 =item I<manpage ...>
48
49 Install these man pages into the first package acted on. (Or in all
50 packages if -A is specified).
51
52 =back
53
54 =head1 NOTES
55
56 An older version of this program, L<dh_installmanpages(1)>, is still used
57 by some packages, and so is still included in debhelper.
58 It is, however, deprecated, due to its counterintuitive and inconsistent
59 interface. Use this program instead.
60
61 =cut
62
63 init();
64
65 my @sofiles;
66 my @sodests;
67
68 foreach my $package (@{$dh{DOPACKAGES}}) {
69         my $tmp=tmpdir($package);
70         my $file=pkgfile($package,"manpages");
71         my @manpages;
72
73         if ($file) {
74                 @manpages=filearray($file, ".");
75         }
76
77         if (($package eq $dh{FIRSTPACKAGE} || $dh{PARAMS_ALL}) && @ARGV) {
78                 push @manpages, @ARGV;
79         }
80
81         foreach my $page (@manpages) {
82                 my $basename=Debian::Debhelper::Dh_Lib::basename($page);
83
84                 # Support compressed pages.
85                 my $gz='';
86                 if ($basename=~m/(.*)(\.gz)/) {
87                         $basename=$1;
88                         $gz=$2;
89                 }
90
91                 my $section;
92                 # See if there is a .TH entry in the man page. If so,
93                 # we'll pull the section field from that.
94                 if ($gz) {
95                         open (IN, "zcat $page|") or die "$page: $!";
96                 }
97                 else {
98                         open (IN, $page) or die "$page: $!";
99                 }
100                 while (<IN>) {
101                         if (/^\.TH\s+\S+\s+(\d+\S*)\s/) {
102                                 $section=$1;
103                                 last;
104                         }
105                 }
106                 # Failing that, we can try to get it from the filename.
107                 if (! $section) {
108                         ($section)=$basename=~m/.*\.([1-9]\S*)/;
109                 }
110
111                 # Now get the numeric component of the section.
112                 my ($realsection)=$section=~m/^(\d)/ if defined $section;
113                 
114                 # If there is no numeric section, bail.
115                 if (! $realsection) {
116                         error("Could not determine section for $page");
117                 }
118                 
119                 # Get the man page's name -- everything up to the last dot.
120                 my ($instname)=$basename=~m/^(.*)\./;
121         
122                 my $destdir="$tmp/usr/share/man/man$realsection/";
123                 # Translated man pages are typically specified by adding the
124                 # language code to the filename, so detect that and
125                 # redirect to appropriate directory, stripping the code.
126                 my ($langcode)=$basename=~m/.*\.([a-z][a-z](?:_[A-Z][A-Z])?)\.(?:[1-9]|man)/;
127                 if (defined $langcode && $langcode ne '') {
128                         $destdir="$tmp/usr/share/man/$langcode/man$section/";
129                         # Strip the language code from the instname.
130                         $instname=~s/\.$langcode$//;
131                 }
132                 $destdir=~tr:/:/:s; # just for looks
133
134                 if (! -e "$destdir/$instname.$section" && 
135                     ! -l "$destdir/$instname.$section") {
136                         if (! -d $destdir) {
137                                 doit "install","-d",$destdir;
138                         }
139                         doit "install","-p","-m644",$page,
140                                 "$destdir$instname.$section$gz";
141                 }
142                 
143         }
144
145         # Now the .so conversion.
146         @sofiles=@sodests=();
147         foreach my $dir (qw{usr/share/man usr/X11R6/man}) {
148                 if (-e "$tmp/$dir") {
149                         find(\&find_so_man, "$tmp/$dir");
150                 }
151         }
152         foreach my $sofile (@sofiles) {
153                 my $sodest=shift(@sodests);
154                 doit "rm","-f",$sofile;
155                 doit "ln","-sf",$sodest,$sofile;
156         }
157 }
158
159 # Check if a file is a .so man page, for use by File::Find.
160 sub find_so_man {
161         # The -s test is becuase a .so file tends to be small. We don't want
162         # to open every man page. 1024 is arbitrary.
163         if (! -f $_ || -s $_ > 1024) {
164                 return;
165         }
166
167         # Test first line of file for the .so thing.
168         open (SOTEST,$_);
169         my $l=<SOTEST>;
170         close SOTEST;
171         if ($l=~m/\.so\s+(.*)/) {
172                 my $solink=$1;
173                 # This test is here to prevent links like ... man8/../man8/foo.8
174                 if (Debian::Debhelper::Dh_Lib::basename($File::Find::dir) eq
175                     Debian::Debhelper::Dh_Lib::dirname($solink)) {
176                         $solink=Debian::Debhelper::Dh_Lib::basename($solink);
177                 }
178                 else {
179                         $solink="../$solink";
180                 }
181         
182                 push @sofiles,"$File::Find::dir/$_";
183                 push @sodests,$solink;
184         }
185 }
186
187 =head1 SEE ALSO
188
189 L<debhelper(1)>
190
191 This program is a part of debhelper.
192
193 =head1 AUTHOR
194
195 Joey Hess <joeyh@debian.org>
196
197 =cut