]> git.donarmstrong.com Git - debhelper.git/blob - dh_installman
r420: big monsta changes
[debhelper.git] / dh_installman
1 #!/usr/bin/perl -w
2 #
3 # Reads debian/manpages, installs all man pages there into appropriate
4 # man page directory tree.
5
6 use strict;
7 use File::Find;
8 use Debian::Debhelper::Dh_Lib;
9 init();
10
11 foreach my $package (@{$dh{DOPACKAGES}}) {
12         my $tmp=tmpdir($package);
13         my $file=pkgfile($package,"manpages");
14         my @manpages;
15
16         if ($file) {
17                 @manpages=filearray($file, ".");
18         }
19
20         if (($package eq $dh{FIRSTPACKAGE} || $dh{PARAMS_ALL}) && @ARGV) {
21                 push @manpages, @ARGV;
22         }
23
24         foreach my $page (@manpages) {
25                 my $basename=Debian::Debhelper::Dh_Lib::basename($page);
26
27                 # Support compressed pages.
28                 my $gz='';
29                 if ($basename=~m/(.*)(\.gz)/) {
30                         $basename=$1;
31                         $gz=$2;
32                 }
33
34                 my $section;
35                 # See if there is a .TH entry in the man page. If so,
36                 # we'll pull the section field from that.
37                 if ($gz) {
38                         open (IN, "zcat $page|") or die "$page: $!";
39                 }
40                 else {
41                         open (IN, $page) or die "$page: $!";
42                 }
43                 while (<IN>) {
44                         if (/^\.TH\s+[^         ]+\s+(\d+[^     ]*)\s/) {
45                                 $section=$1;
46                                 last;
47                         }
48                 }
49                 # Failing that, we can try to get it from the filename.
50                 if (! $section) {
51                         ($section)=$basename=~m/.*\.([1-9][^ ]*)/;
52                 }
53
54                 # Now get the numeric component of the section.
55                 my ($realsection)=$section=~m/^(\d)/ if defined $section;
56                 
57                 # If there is no numeric section, bail.
58                 if (! $realsection) {
59                         error("Could not determine section for $page");
60                 }
61                 
62                 my $destdir="$tmp/usr/share/man/man$realsection/";
63                 # Translated man pages are typically specified by adding the
64                 # language code to the filename, so detect that and
65                 # redirect to appropriate directory.
66                 my ($langcode)=$basename=~m/.*\.([a-z][a-z](?:_[A-Z][A-Z])?)\.(?:[1-9]|man)/;
67                 if (defined $langcode && $langcode ne '') {
68                         $destdir="$tmp/usr/share/man/$langcode/man$section/";
69                 }
70                 $destdir=~tr:/:/:s; # just for looks
71
72                 # Get the man page's name -- everything up to the last dot.
73                 my ($instname)=$basename=~m/^(.*)\./;
74         
75                 if (! -e "$destdir/$instname.$section" && 
76                     ! -l "$destdir/$instname.$section") {
77                         if (! -d $destdir) {
78                                 doit "install","-d",$destdir;
79                         }
80                         doit "install","-p","-m644",$page,
81                                 "$destdir$instname.$section$gz";
82                 }
83                 
84         }
85
86         # Now the .so conversion.
87         my @sofiles;
88         my @sodests;
89         foreach my $dir (qw{usr/share/man usr/X11R6/man}) {
90                 if (-e "$tmp/$dir") {
91                         find(\&find_so_man, "$tmp/$dir");
92                 }
93         }
94         foreach my $sofile (@sofiles) {
95                 my $sodest=shift(@sodests);
96                 doit "rm","-f",$sofile;
97                 doit "ln","-sf",$sodest,$sofile;
98         }
99 }
100
101 # Check if a file is a .so man page, for use by File::Find.
102 my @sofiles;
103 my @sodests;
104 sub find_so_man {
105         # The -s test is becuase a .so file tends to be small. We don't want
106         # to open every man page. 1024 is arbitrary.
107         if (! -f $_ || -s $_ > 1024) {
108                 return;
109         }
110
111         # Test first line of file for the .so thing.
112         open (SOTEST,$_);
113         my $l=<SOTEST>;
114         close SOTEST;
115         if ($l=~m/\.so\s+(.*)/) {
116                 my $solink=$1;
117                 # This test is here to prevent links like ... man8/../man8/foo.8
118                 if (Debian::Debhelper::Dh_Lib::basename($File::Find::dir) eq
119                     Debian::Debhelper::Dh_Lib::dirname($solink)) {
120                         $solink=Debian::Debhelper::Dh_Lib::basename($solink);
121                 }
122                 else {
123                         $solink="../$solink";
124                 }
125         
126                 push @sofiles,"$File::Find::dir/$_";
127                 push @sodests,$solink;
128         }
129 }