$VERSION "a5" doesn't quite work.
[p5sagit/p5-mst-13.2.git] / installman
CommitLineData
f0512cd0 1#!./perl -w
e8fcf3b9 2BEGIN { @INC = qw(lib) }
f0512cd0 3use strict;
16d20bd9 4use Config;
5use Getopt::Long;
6use File::Find;
354f3b56 7use File::Copy;
356fc125 8use File::Path qw(mkpath);
354f3b56 9use ExtUtils::Packlist;
a2743834 10use Pod::Man;
cd8bccb5 11use subs qw(unlink chmod rename link);
f0512cd0 12use vars qw($packlist @modpods);
16d20bd9 13require Cwd;
14
791b4ad3 15if ($Config{d_umask}) {
16 umask(022); # umasks like 077 aren't that useful for installations
17}
18
cd8bccb5 19$ENV{SHELL} = 'sh' if $^O eq 'os2';
16d20bd9 20
f0512cd0 21my $ver = $Config{version}; # Not used presently.
22my $release = substr($],0,3); # Not used presently.
23my $patchlevel = substr($],3,2);
16d20bd9 24die "Patchlevel of perl ($patchlevel)",
cceca5ed 25 "and patchlevel of config.sh ($Config{'PERL_VERSION'}) don't match\n"
26 if $patchlevel != $Config{'PERL_VERSION'};
16d20bd9 27
f0512cd0 28my $usage =
16d20bd9 29"Usage: installman --man1dir=/usr/wherever --man1ext=1
b5f05010 30 --man3dir=/usr/wherever --man3ext=3
f1745d4f 31 --batchlimit=40
eabd589f 32 --notify --verbose --silent --help
16d20bd9 33 Defaults are:
34 man1dir = $Config{'installman1dir'};
35 man1ext = $Config{'man1ext'};
36 man3dir = $Config{'installman3dir'};
37 man3ext = $Config{'man3ext'};
eabd589f 38 --notify (or -n) just lists commands that would be executed.
39 --verbose (or -V) report all progress.
40 --silent (or -S) be silent. Only report errors.\n";
16d20bd9 41
f0512cd0 42my %opts;
43GetOptions( \%opts,
f1745d4f 44 qw( man1dir=s man1ext=s man3dir=s man3ext=s batchlimit=i
f0512cd0 45 notify n help silent S verbose V))
16d20bd9 46 || die $usage;
f0512cd0 47die $usage if $opts{help};
16d20bd9 48
f0512cd0 49$opts{man1dir} = $Config{'installman1dir'}
50 unless defined($opts{man1dir});
51$opts{man1ext} = $Config{'man1ext'}
52 unless defined($opts{man1ext});
53$opts{man3dir} = $Config{'installman3dir'}
54 unless defined($opts{man3dir});
55$opts{man3ext} = $Config{'man3ext'}
56 unless defined($opts{man3ext});
57$opts{silent} ||= $opts{S};
f0512cd0 58$opts{notify} ||= $opts{n};
4ad019ef 59$opts{verbose} ||= $opts{V} || $opts{notify};
16d20bd9 60
61#Sanity checks
62
cd8bccb5 63-x "./perl$Config{exe_ext}"
64 or warn "./perl$Config{exe_ext} not found! Have you run make?\n";
16d20bd9 65-d $Config{'installprivlib'}
66 || warn "Perl library directory $Config{'installprivlib'} not found.
67 Have you run make install?. (Installing anyway.)\n";
cd8bccb5 68-x "t/perl$Config{exe_ext}" || warn "WARNING: You've never run 'make test'!!!",
16d20bd9 69 " (Installing anyway.)\n";
70
354f3b56 71$packlist = ExtUtils::Packlist->new("$Config{installarchlib}/.packlist");
72
21dae8a0 73
16d20bd9 74# Install the main pod pages.
a2743834 75pod2man('pod', $opts{man1dir}, $opts{man1ext});
16d20bd9 76
77# Install the pods for library modules.
a2743834 78pod2man('lib', $opts{man3dir}, $opts{man3ext});
16d20bd9 79
1fef88e7 80# Install the pods embedded in the installed scripts
21dae8a0 81open UTILS, "utils.lst" or die "Can't open 'utils.lst': $!";
82while (<UTILS>) {
83 next if /^#/;
84 chomp;
85 $_ = $1 if /#.*pod\s*=\s*(\S+)/;
7feaa10c 86 my ($where, $what) = m|^(.*?)/(\S+)|;
a2743834 87 pod2man($where, $opts{man1dir}, $opts{man1ext}, $what);
e8fcf3b9 88 if (my ($where2, $what2) = m|#.*link\s*=\s*(\S+)/(\S+)|) {
89 my $old = "$opts{man1dir}/$what.$opts{man1ext}";
90 my $new = "$opts{man1dir}/$what2.$opts{man1ext}";
91 unlink($new);
92 link($old, $new);
21dae8a0 93 }
94}
1fef88e7 95
a2743834 96sub pod2man {
f1745d4f 97 # @script is scripts names if we are installing manpages embedded
98 # in scripts, () otherwise
99 my($poddir, $mandir, $manext, @script) = @_;
1fef88e7 100
101 my($downdir); # can't just use .. when installing xsubpp manpage
102
103 $downdir = $poddir;
104 $downdir =~ s:[^/]+:..:g;
16d20bd9 105 my($builddir) = Cwd::getcwd();
106
107 if ($mandir eq ' ' or $mandir eq '') {
f1745d4f 108 if (@script) {
109 warn "Skipping installation of $poddir/$_ man page.\n"
110 foreach @script;
111 } else {
112 warn "Skipping installation of $poddir man pages.\n";
113 }
16d20bd9 114 return;
115 }
116
f0512cd0 117 print "chdir $poddir\n" if $opts{verbose};
1fef88e7 118 chdir $poddir || die "Unable to cd to $poddir directory!\n$!\n";
16d20bd9 119
418918ac 120 mkpath($mandir, $opts{verbose}, 0777) unless $opts{notify}; # In File::Path
16d20bd9 121 # Make a list of all the .pm and .pod files in the directory. We will
a2743834 122 # always run from the lib directory and use the full pathname
123 # of the pod.
f1745d4f 124 if (@script) {
125 @modpods = @script;
72b3d9b4 126 }
127 else {
1fef88e7 128 @modpods = ();
f0512cd0 129 File::Find::find(\&lsmodpods, '.');
1fef88e7 130 }
f1745d4f 131 my @to_process;
f0512cd0 132 foreach my $mod (@modpods) {
133 my $manpage = $mod;
cd8bccb5 134 my $tmp;
135 # Skip .pm files that have corresponding .pod files, and Functions.pm.
136 next if (($tmp = $mod) =~ s/\.pm$/.pod/ && -f $tmp);
137 next if ($mod eq 'Pod/Functions.pm'); #### Used only by pod itself
138
16d20bd9 139 # Convert name from File/Basename.pm to File::Basename.3 format,
140 # if necessary.
141 $manpage =~ s#\.p(m|od)$##;
4fabb596 142 if ($^O eq 'os2' || $^O eq 'amigaos' || $^O eq 'uwin' || $^O eq 'cygwin') {
cd8bccb5 143 $manpage =~ s#/#.#g;
72b3d9b4 144 }
145 else {
cd8bccb5 146 $manpage =~ s#/#::#g;
147 }
148 $tmp = "${mandir}/${manpage}.tmp";
16d20bd9 149 $manpage = "${mandir}/${manpage}.${manext}";
f1745d4f 150 push @to_process, [$mod, $tmp, $manpage];
151 }
a2743834 152
153 my $parser = Pod::Man->new( section => $manext,
154 official=> 1,
155 center => 'Perl Programmers Reference Guide'
156 );
157 foreach my $page (@to_process) {
158 my($pod, $tmp, $manpage) = @$page;
159
160 print " $manpage\n";
161 if (!$opts{notify} && $parser->parse_from_file($pod, $tmp)) {
162 if (-s $tmp) {
163 if (rename($tmp, $manpage)) {
164 $packlist->{$manpage} = { type => 'file' };
165 next;
166 }
167 }
168 unlink($tmp);
6d64b06f 169 }
16d20bd9 170 }
171 chdir "$builddir" || die "Unable to cd back to $builddir directory!\n$!\n";
f0512cd0 172 print " chdir $builddir\n" if $opts{verbose};
16d20bd9 173}
174
175sub lsmodpods {
176 my $dir = $File::Find::dir;
177 my $name = $File::Find::name;
178 if (-f $_) {
179 $name =~ s#^\./##;
180 push(@modpods, $name) if ($name =~ /\.p(m|od)$/);
181 }
182}
183
f0512cd0 184$packlist->write() unless $opts{notify};
185print " Installation complete\n" if $opts{verbose};
16d20bd9 186
187exit 0;
188
189
190###############################################################################
191# Utility subroutines from installperl
192
cd8bccb5 193sub unlink {
f0512cd0 194 my(@names) = @_;
cd8bccb5 195 my $cnt = 0;
196
f0512cd0 197 foreach my $name (@names) {
72b3d9b4 198 next unless -e $name;
199 chmod 0777, $name if $^O eq 'os2';
f0512cd0 200 print " unlink $name\n" if $opts{verbose};
72b3d9b4 201 ( CORE::unlink($name) and ++$cnt
f0512cd0 202 or warn "Couldn't unlink $name: $!\n" ) unless $opts{notify};
cd8bccb5 203 }
204 return $cnt;
205}
206
16d20bd9 207sub link {
354f3b56 208 my($from,$to) = @_;
209 my($success) = 0;
16d20bd9 210
a2743834 211 print " ln $from $to\n" if $opts{verbose};
354f3b56 212 eval {
213 CORE::link($from, $to)
214 ? $success++
215 : ($from =~ m#^/afs/# || $to =~ m#^/afs/#)
216 ? die "AFS" # okay inside eval {}
217 : warn "Couldn't link $from to $to: $!\n"
f0512cd0 218 unless $opts{notify};
354f3b56 219 };
220 if ($@) {
221 File::Copy::copy($from, $to)
222 ? $success++
223 : warn "Couldn't copy $from to $to: $!\n"
f0512cd0 224 unless $opts{notify};
354f3b56 225 }
226 $success;
cd8bccb5 227}
228
229sub rename {
f0512cd0 230 my($from,$to) = @_;
55a105fd 231 if (-f $to and not unlink($to)) {
72b3d9b4 232 my($i);
233 for ($i = 1; $i < 50; $i++) {
234 last if CORE::rename($to, "$to.$i");
235 }
236 warn("Cannot rename to `$to.$i': $!"), return 0
237 if $i >= 50; # Give up!
cd8bccb5 238 }
55a105fd 239 link($from,$to) || return 0;
240 unlink($from);
16d20bd9 241}
242
243sub chmod {
f0512cd0 244 my($mode,$name) = @_;
16d20bd9 245
f0512cd0 246 printf " chmod %o %s\n", $mode, $name if $opts{verbose};
cd8bccb5 247 CORE::chmod($mode,$name) || warn sprintf("Couldn't chmod %o %s: $!\n",$mode,$name)
f0512cd0 248 unless $opts{notify};
16d20bd9 249}
250
16d20bd9 251sub samepath {
f0512cd0 252 my($p1, $p2) = @_;
253 my($dev1, $ino1, $dev2, $ino2);
16d20bd9 254
255 if ($p1 ne $p2) {
256 ($dev1, $ino1) = stat($p1);
257 ($dev2, $ino2) = stat($p2);
258 ($dev1 == $dev2 && $ino1 == $ino2);
259 }
260 else {
261 1;
262 }
263}