This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Fix awkward wording in 'say' documentation
[perl5.git] / installman
1 #!./perl -w
2
3 BEGIN {
4     @INC = qw(lib);
5
6     # This needs to be at BEGIN time, before any use of Config
7     # install_lib itself loads and imports Config into main::
8     require './install_lib.pl';
9 }
10
11 use strict;
12
13 use Getopt::Long;
14 use ExtUtils::Packlist;
15 use Pod::Man;
16 use vars qw(%opts $packlist);
17
18 require './Porting/pod_lib.pl';
19 my %man1 = (map {($_->[0], $_->[1])} @{get_pod_metadata()->{master}});
20
21 $ENV{SHELL} = 'sh' if $^O eq 'os2';
22
23 my $patchlevel = substr($],3,2);
24 die "Patchlevel of perl ($patchlevel)",
25     "and patchlevel of config.sh ($Config{'PERL_VERSION'}) don't match\n"
26         if $patchlevel != $Config{'PERL_VERSION'};
27
28 my $usage =
29 "Usage:  installman --man1dir=/usr/wherever --man1ext=1
30                    --man3dir=/usr/wherever --man3ext=3
31                    --notify --verbose --silent --help
32         Defaults are:
33         man1dir = $Config{'installman1dir'};
34         man1ext = $Config{'man1ext'};
35         man3dir = $Config{'installman3dir'};
36         man3ext = $Config{'man3ext'};
37         --notify  (or -n) just lists commands that would be executed.
38         --verbose (or -V) report all progress.
39         --silent  (or -S) be silent. Only report errors.\n";
40
41 # --strip intentionally does nothing. By permitting installman to accept it
42 # without error, the Makefile can pass the same options to installperl and
43 # installman, which permits more simplification there than this comment costs.
44 GetOptions( \%opts,
45             qw( man1dir=s man1ext=s man3dir=s man3ext=s
46                 destdir:s notify|n help|h|? silent|S verbose|V strip))
47         || die $usage;
48 die $usage if $opts{help};
49 $opts{destdir} //= '';
50
51 foreach my $pre (qw(man1 man3)) {
52     $opts{"${pre}dir"} //= $opts{destdir} . $Config{"install${pre}dir"};
53     $opts{"${pre}ext"} //= $Config{"${pre}ext"};
54 }
55 $opts{verbose} ||= $opts{notify};
56
57 #Sanity checks
58
59 -x  "./perl$Config{exe_ext}"
60   or warn "./perl$Config{exe_ext} not found!  Have you run make?\n";
61 -d  "$opts{destdir}$Config{'installprivlib'}"
62         || warn "Perl library directory $Config{'installprivlib'} not found.
63                 Have you run make install?.  (Installing anyway.)\n";
64 -x "t/perl$Config{exe_ext}"             || warn "WARNING: You've never run 'make test'!!!",
65         "  (Installing anyway.)\n";
66
67 $packlist = ExtUtils::Packlist->new("$opts{destdir}$Config{installarchlib}/.packlist");
68
69 # Install the main pod pages.
70 pod2man(\%man1, $opts{man1dir}, $opts{man1ext}, 'pod');
71
72 # Install the pods for library modules.
73 {
74     my $found = pods_to_install();
75     pod2man($found->{$_}, $opts{man3dir}, $opts{man3ext}, 'lib')
76         foreach qw(MODULE PRAGMA);
77 }
78
79 # Install the pods embedded in the installed scripts
80 my $has_man1dir = $opts{man1dir} ne '' && -d $opts{man1dir};
81 my $fh = open_or_die('utils.lst');
82 while (<$fh>) {
83     next if /^#/;
84     chomp;
85     my ($path, $leaf) = m|^(\S*/(\S+))|;
86     # Have we already installed the manpage for this? (eg perldoc)
87     next if $man1{$leaf};
88     pod2man({$leaf, $path}, $opts{man1dir}, $opts{man1ext});
89     if ($has_man1dir) {
90         if (my ($link) = m|#.*link\s*=\s*\S+/(\S+)|) {
91             my $old = "$opts{man1dir}/$leaf.$opts{man1ext}";
92             my $new = "$opts{man1dir}/$link.$opts{man1ext}";
93             unlink($new);
94             link($old, $new);
95             $old =~ s/^\Q$opts{destdir}\E// if $opts{destdir};
96             $new =~ s/^\Q$opts{destdir}\E// if $opts{destdir};
97             $packlist->{$new} = { from => $old, type => 'link' };
98         }
99     }
100 }
101 close $fh or my_die("close 'utils.lst': $!");
102
103 sub pod2man {
104     my($modpods, $mandir, $manext, $where) = @_;
105     if ($mandir eq ' ' or $mandir eq '') {
106         if ($where) {
107             warn "Skipping installation of $where man pages.\n"
108         } else {
109             warn "Skipping installation of $_ man page.\n"
110                 foreach values %$modpods;
111         }
112         return;
113     }
114
115     if ($opts{verbose}) {
116         if ($where) {
117             print "installing from $where\n";
118         } else {
119             print "installing $_\n"
120                 foreach sort keys %$modpods;
121         }
122     }
123
124     mkpath($mandir);
125
126     foreach my $manpage (sort keys %$modpods) {
127         my $mod = $modpods->{$manpage};
128
129         # Skip files without pod docs
130         my $has_pod;
131         my $fh = open_or_die($mod);
132         while (my $line = <$fh>) {
133             if ($line =~ /^=head1\b/) {
134                 ++$has_pod;
135                 last;
136             }
137         }
138         close $fh or my_die("close '$mod': $!");
139         # Sadly it doesn't seem possible to re-use this handle for the call
140         # to parse_from_file() below, as Pod::Man relies on source_filename(),
141         # which Pod::Simple only sets accurately if it opens the file itself.
142
143         unless ($has_pod)
144         {
145             warn "no documentation in $mod\n" unless $opts{silent};
146             next;
147         }
148
149         if ($^O eq 'os2' || $^O eq 'amigaos' || $^O eq 'uwin' || $^O eq 'cygwin') {
150             $manpage =~ s#::#.#g;
151         }
152         my $tmp = "${mandir}/${manpage}.tmp";
153         $manpage = "${mandir}/${manpage}.${manext}";
154
155         my $parser = Pod::Man->new( section => $manext,
156                                     official=> 1,
157                                     center  => 'Perl Programmers Reference Guide'
158                                   );
159         my $xmanpage = $manpage;
160         $xmanpage =~ s/^\Q$opts{'destdir'}\E// if $opts{'destdir'};
161         print "  $xmanpage\n" unless $opts{silent};
162         if (!$opts{notify} && $parser->parse_from_file($mod, $tmp)) {
163             if (-s $tmp) {
164                 if (safe_rename($tmp, $manpage)) {
165                     $packlist->{$xmanpage} = { type => 'file' };
166                     next;
167                 }
168             }
169             unlink($tmp);
170         }
171     }
172 }
173
174 $packlist->write() unless $opts{notify};
175 print "  Installation complete\n" if $opts{verbose};
176
177 # ex: set ts=8 sts=4 sw=4 et: