]> git.donarmstrong.com Git - debhelper.git/blob - dh_installmanpages
r314: * Added support for translated man pages, with a patch from
[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 BEGIN { push @INC, "debian", "/usr/share/debhelper" }
11 use 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.
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 $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 (Dh_Lib::basename($File::Find::dir) eq Dh_Lib::dirname($solink)) {
64                         $solink=Dh_Lib::basename($solink);
65                 }
66                 else {
67                         $solink="../$solink";
68                 }
69         
70                 push @sofiles,"$File::Find::dir/$_";
71                 push @sodests,$solink;
72         }
73 }
74
75 foreach $PACKAGE (@{$dh{DOPACKAGES}}) {
76         $TMP=tmpdir($PACKAGE);
77
78         # Find all filenames that look like man pages.
79         @manpages=();
80         @allpackages=GetPackages('');
81         find(\&find_man,'.'); # populates @manpages
82         
83         foreach $page (@manpages) {
84                 $page=~s:^\./::; # just for looks
85                 
86                 $basename=Dh_Lib::basename($page);
87                 
88                 # Skip all files listed on command line.
89                 my $install=1;
90                 foreach $skip (@ARGV) {
91                         # Look at basename of what's on connect line
92                         # for backwards compatability.
93                         if ($basename eq Dh_Lib::basename($skip)) {
94                                 $install=undef;
95                                 last;
96                         }
97                 }
98                 
99                 if ($install) {
100                         my $extdir="share";
101                         # Handle X man pages specially.
102                         if ($basename=~/x$/) {
103                                 $extdir="X11R6";
104                         }
105                         
106                         my ($section)=$basename=~m/.*\.([1-9])/;
107                         
108                         my $destdir="$TMP/usr/$extdir/man/man$section/";
109                         
110                         # Handle translated man pages.
111                         my $instname=$basename;
112                         my ($langcode)=$basename=~m/.*\.([a-z][a-z])\.([1-9])/;
113                         if (defined $langcode && $langcode ne '') {
114                                 $destdir="$TMP/usr/$extdir/man/$langcode/man$section/";
115                                 $instname=~s/\.$langcode\./\./;
116                         }
117                         
118                         $destdir=~tr:/:/:s; # just for looks
119                         
120                         if (! -e "$destdir/$basename" && !-l "$destdir/$basename") {
121                                 if (! -d $destdir) {
122                                         doit "install","-d",$destdir;
123                                 }
124                                 doit "install","-p","-m644",$page,$destdir.$instname;
125                         }
126                 }
127         }
128         
129         # Now the .so conversion.
130         @sofiles=@sodests=();
131         foreach $dir (qw{usr/share/man usr/X11R6/man}) {
132                 if (-e "$TMP/$dir") {
133                         find(\&find_so_man, "$TMP/$dir");
134                 }
135         }
136         foreach $sofile (@sofiles) {
137                 my $sodest=shift(@sodests);
138                 doit "rm","-f",$sofile;
139                 doit "ln","-sf",$sodest,$sofile;
140         }
141 }