Correct typo
[p5sagit/p5-mst-13.2.git] / installperl
1 #!./perl
2 BEGIN { @INC=('./lib', '../lib') }
3 use File::Find;
4 use File::Path qw(mkpath);
5 use Config;
6 use subs qw(unlink rename link chmod);
7
8 $mainperldir = "/usr/bin";
9 $exe_ext = $Config{exe_ext};
10
11 while (@ARGV) {
12     $nonono = 1 if $ARGV[0] eq '-n';
13     $versiononly = 1 if $ARGV[0] eq '-v';
14     shift;
15 }
16
17 umask 022;
18
19 @scripts = qw(  utils/c2ph utils/h2ph utils/h2xs utils/pstruct
20                 utils/perlbug utils/perldoc
21                 x2p/s2p x2p/find2perl
22                 pod/pod2man pod/pod2html pod/pod2latex pod/pod2text);
23
24 # pod documentation now handled by separate installman script.
25 # These two are archaic leftovers.
26 @manpages = qw(x2p/a2p.man x2p/s2p.man);
27
28 @pods = (<pod/*.pod>);
29
30 $ver = $];
31 $release = substr($ver,0,3);   # Not used presently.
32 $patchlevel = substr($ver,3,2);
33 die "Patchlevel of perl ($patchlevel)",
34     "and patchlevel of config.sh ($Config{'PATCHLEVEL'}) don't match\n"
35         if $patchlevel != $Config{'PATCHLEVEL'};
36
37 # Fetch some frequently-used items from %Config
38 $installbin = $Config{installbin};
39 $installscript = $Config{installscript};
40 $installprivlib = $Config{installprivlib};
41 $installarchlib = $Config{installarchlib};
42 $installsitelib = $Config{installsitelib};
43 $installsitearch = $Config{installsitearch};
44 $installman1dir = $Config{installman1dir};
45 $man1ext = $Config{man1ext};
46 # Did we build libperl as a shared library?
47 $d_shrplib = $Config{d_shrplib};
48 $shrpdir = $Config{shrpdir};
49 # Shared library and dynamic loading suffixes.
50 $so = $Config{so};
51 $dlext = $Config{dlext};
52
53 $d_dosuid = $Config{d_dosuid};
54 $binexp = $Config{binexp};
55
56 # Do some quick sanity checks.
57
58 if ($d_dosuid && $>) { die "You must run as root to install suidperl\n"; }
59
60    $installbin          || die "No installbin directory in config.sh\n";
61 -d $installbin          || mkpath($installbin, 1, 0777);
62 -d $installbin          || die "$installbin is not a directory\n";
63 -w $installbin          || die "$installbin is not writable by you\n"
64         unless $installbin =~ m#^/afs/# || $nonono;
65
66 -x 'perl' . $exe_ext    || die "perl isn't executable!\n";
67 -x 'suidperl' . $exe_ext|| die "suidperl isn't executable!\n" if $d_dosuid;
68
69 -x 't/TEST'             || warn "WARNING: You've never run 'make test'!!!",
70         "  (Installing anyway.)\n";
71
72 if ($d_shrplib) {
73     if (!<libperl*.$so*>) {
74         warn "WARNING: Can't find libperl*.$so* to install into $shrpdir.",
75             "  (Installing other things anyway.)\n";
76     } else {
77         mkpath($shrpdir, 1, 0777);
78         -w $shrpdir     || $nonono || die "$shrpdir is not writable by you\n";
79         &cmd("cp libperl*.$so* $shrpdir");
80     }
81 }
82
83 # First we install the version-numbered executables.
84
85 &safe_unlink("$installbin/perl$ver$exe_ext");
86 &cmd("cp perl$exe_ext $installbin/perl$ver$exe_ext");
87
88 &safe_unlink("$installbin/sperl$ver$exe_ext");
89 if ($d_dosuid) {
90     &cmd("cp suidperl$exe_ext $installbin/sperl$ver$exe_ext");
91     &chmod(04711, "$installbin/sperl$ver$exe_ext");
92 }
93
94 exit 0 if $versiononly;
95
96 # Make links to ordinary names if installbin directory isn't current directory.
97
98 if (! &samepath($installbin, '.')) {
99     &safe_unlink("$installbin/perl$exe_ext", "$installbin/suidperl$exe_ext");
100     &link("$installbin/perl$ver$exe_ext", "$installbin/perl$exe_ext");
101     &link("$installbin/sperl$ver$exe_ext", "$installbin/suidperl$exe_ext") 
102       if $d_dosuid;
103 }
104
105 if (! &samepath($installbin, 'x2p')) {
106     &safe_unlink("$installbin/a2p$exe_ext");
107     &cmd("cp x2p/a2p$exe_ext $installbin/a2p$exe_ext");
108     &chmod(0755, "$installbin/a2p$exe_ext");
109 }
110
111 # cppstdin is just a script, but it is architecture-dependent, so
112 # it can't safely be shared.  Place it in $installbin.
113 # Note that Configure doesn't build cppstin if it isn't needed, so
114 # we skip this if cppstdin doesn't exist.
115 if ((-f cppstdin) && (! &samepath($installbin, '.'))) {
116     &safe_unlink("$installbin/cppstdin");
117     &cmd("cp cppstdin $installbin/cppstdin");
118     &chmod(0755, "$installbin/cppstdin");
119 }
120
121 # Install scripts.
122
123 mkpath($installscript, 1, 0777);
124
125 for (@scripts) {
126     &cmd("cp $_ $installscript");
127     s#.*/##; &chmod(0755, "$installscript/$_");
128 }
129
130 # Install pod pages.  Where? I guess in $installprivlib/pod.
131 mkpath("${installprivlib}/pod", 1, 0777);
132 foreach $file (@pods) {
133     # $file is a name like  pod/perl.pod
134     cp_if_diff($file, "${installprivlib}/${file}");
135 }
136
137 # Install old man pages.
138
139 if ($installman1dir ne '') {
140     mkpath($installman1dir, 1, 0777);
141
142     if (! &samepath($installman1dir, '.')) {
143         for (@manpages) {
144             ($new = $_) =~ s/man$/$man1ext/;
145             $new =~ s#.*/##;
146             print STDERR "  Installing $installman1dir/$new\n";
147             next if $nonono;
148             open(MI,$_) || warn "Can't open $_: $!\n";
149             open(MO,">$installman1dir/$new") || 
150                     warn "Can't install $installman1dir/$new: $!\n";
151             print MO ".ds RP Release $release Patchlevel $patchlevel\n";
152             while (<MI>) {
153                 print MO;
154             }
155             close MI;
156             close MO;
157         }
158     }
159 }
160
161 # Install library files.
162
163 $do_installarchlib = $do_installprivlib = 0;
164     
165 mkpath($installprivlib, 1, 0777);
166 mkpath($installarchlib, 1, 0777);
167 mkpath($installsitelib, 1, 0777) if ($installsitelib);
168 mkpath($installsitearch, 1, 0777) if ($installsitearch);
169
170 if (chdir "lib") {
171     $do_installarchlib = ! &samepath($installarchlib, '.');
172     $do_installprivlib = ! &samepath($installprivlib, '.');
173
174     if ($do_installarchlib || $do_installprivlib) {
175         find(\&installlib, '.');
176     }
177     chdir ".." || die "Can't cd back to source directory: $!\n";
178 }
179 else {
180     warn "Can't cd to lib to install lib files: $!\n";
181 }
182
183 # Install header files and libraries.
184 mkpath("$installarchlib/CORE", 1, 0777);
185 @corefiles = <*.h libperl*.*>;
186 # AIX needs perl.exp installed as well.
187 push(@corefiles,'perl.exp') if $^O eq 'aix';
188 # If they have built sperl.o...
189 push(@corefiles,'sperl.o') if -f 'sperl.o';
190 foreach $file (@corefiles) {
191     cp_if_diff($file,"$installarchlib/CORE/$file");
192     &chmod($file =~ /^libperl/ ? 0555 : 0444,"$installarchlib/CORE/$file");
193 }
194
195 # Offer to install perl in a "standard" location
196
197 $mainperl_is_instperl = 0;
198
199 if (-w $mainperldir && ! &samepath($mainperldir, $installbin) && !$nonono) {
200     # First make sure $mainperldir/perl is not already the same as
201     # the perl we just installed
202     if (-x "$mainperldir/perl$exe_ext") {
203         # Try to be clever about mainperl being a symbolic link
204         # to binexp/perl if binexp and installbin are different.
205         $mainperl_is_instperl =
206             &samepath("$mainperldir/perl$exe_ext", "$installbin/perl$exe_ext") ||
207              (($binexp ne $installbin) &&
208               (-l "$mainperldir/perl$exe_ext") &&
209               ((readlink "$mainperldir/perl$exe_ext") eq "$binexp/perl$exe_ext"));
210     }
211     if ((! $mainperl_is_instperl) &&
212         (&yn("Many scripts expect perl to be installed as " .
213              "$mainperldir/perl.\n" . 
214              "Do you wish to have $mainperldir/perl be the same as\n" .
215              "$binexp/perl? [y] ")))
216     {   
217         unlink("$mainperldir/perl$exe_ext");
218         eval 'link("$installbin/perl$exe_ext", "$mainperldir/perl$exe_ext")' ||
219         eval 'symlink("$binexp/perl$exe_ext", "$mainperldir/perl$exe_ext")' ||
220         &cmd("cp $installbin/perl$exe_ext $mainperldir$exe_ext");
221         $mainperl_is_instperl = 1;
222     }
223 }
224
225 # Check to make sure there aren't other perls around in installer's
226 # path.  This is probably UNIX-specific.  Check all absolute directories
227 # in the path except for where public executables are supposed to live.
228 # Also skip $mainperl if the user opted to have it be a link to the
229 # installed perl.
230
231 $dirsep = ($^O eq 'os2') ? ';' : ':' ;
232 ($path = $ENV{"PATH"}) =~ s:\\:/:g ;
233 @path = split(/$dirsep/, $path);
234 @otherperls = ();
235 for (@path) {
236     next unless m,^/,;
237     next if ($_ eq $binexp);
238     # Use &samepath here because some systems have other dirs linked
239     # to $mainperldir (like SunOS)
240     next if ($mainperl_is_instperl && &samepath($_, $mainperldir));
241     push(@otherperls, "$_/perl$exe_ext")
242       if (-x "$_/perl$exe_ext" && ! -d "$_/perl$exe_ext");
243 }
244 if (@otherperls) {
245     print STDERR "\nWarning: perl appears in your path in the following " .
246         "locations beyond where\nwe just installed it:\n";
247     for (@otherperls) {
248         print STDERR "    ", $_, "\n";
249     }
250     print STDERR "\n";
251 }
252
253 print STDERR "  Installation complete\n";
254
255 exit 0;
256
257 ###############################################################################
258
259 sub yn {
260     local($prompt) = @_;
261     local($answer);
262     local($default) = $prompt =~ m/\[([yn])\]\s*$/i;
263     print STDERR $prompt;
264     chop($answer = <STDIN>);
265     $answer = $default if $answer =~ m/^\s*$/;
266     ($answer =~ m/^[yY]/);
267 }
268
269 sub unlink {
270     local(@names) = @_;
271     my($cnt) = 0;
272
273     foreach $name (@names) {
274         next unless -e $name;
275         chmod 0777, $name if $^O eq 'os2';
276         print STDERR "  unlink $name\n";
277         ( CORE::unlink($name) and ++$cnt 
278           or warn "Couldn't unlink $name: $!\n" ) unless $nonono;
279     }
280     return $cnt;
281 }
282
283 sub safe_unlink {
284     local(@names) = @_;
285
286     foreach $name (@names) {
287         next unless -e $name;
288         next if $nonono;
289         chmod 0777, $name if $^O eq 'os2';
290         print STDERR "  unlink $name\n";
291         next if CORE::unlink($name);
292         warn "Couldn't unlink $name: $!\n";
293         if ($! =~ /busy/i) {
294             print STDERR "  mv $name $name.old\n";
295             &rename($name, "$name.old") || warn "Couldn't rename $name: $!\n";
296         }
297     }
298 }
299
300 sub cmd {
301     local($cmd) = @_;
302     print STDERR "  $cmd\n";
303     unless ($nonono) {
304         system $cmd;
305         warn "Command failed!!!\n" if $?;
306     }
307 }
308
309 sub rename {
310     local($from,$to) = @_;
311     if (-f $to and not unlink($to)) {
312         my($i);
313         for ($i = 1; $i < 50; $i++) {
314             last if CORE::rename($to, "$to.$i");
315         }
316         warn("Cannot rename to `$to.$i': $!"), return 0 
317            if $i >= 50; # Give up!
318     }
319     link($from,$to) || return 0;
320     unlink($from);
321 }
322
323 sub link {
324     local($from,$to) = @_;
325
326     print STDERR "  ln $from $to\n";
327     eval {
328       CORE::link($from,$to) || warn "Couldn't link $from to $to: $!\n" unless $nonono;
329     };
330     if ($@) {
331       system( $cp, $from, $to )
332         && warn "Couldn't copy $from to $to: $!\n" unless $nonono;
333     }
334 }
335
336 sub chmod {
337     local($mode,$name) = @_;
338
339     printf STDERR "  chmod %o %s\n", $mode, $name;
340     CORE::chmod($mode,$name) || warn sprintf("Couldn't chmod %o %s: $!\n",$mode,$name)
341         unless $nonono;
342 }
343
344 sub samepath {
345     local($p1, $p2) = @_;
346     local($dev1, $ino1, $dev2, $ino2);
347
348     if ($p1 ne $p2) {
349         ($dev1, $ino1) = stat($p1);
350         ($dev2, $ino2) = stat($p2);
351         ($dev1 == $dev2 && $ino1 == $ino2);
352     }
353     else {
354         1;
355     }
356 }
357
358 sub installlib {
359     my $dir = $File::Find::dir;
360     $dir =~ s#^\.(?![^/])/?##;
361
362     my $name = $_;
363     
364     # ignore patch backups and the .exists files.
365     return if $name =~ m{\.orig$|~$|^\.exists};
366
367     $name = "$dir/$name" if $dir ne '';
368
369     my $installlib = $installprivlib;
370     if ((substr($dir, 0, 4) eq 'auto') || ($name eq 'Config.pm')) {
371         $installlib = $installarchlib;
372         return unless $do_installarchlib;
373     } else {
374         return unless $do_installprivlib;
375     }
376
377     if (-f $_) {
378         if (/\.al$/ || /\.ix$/) {
379             $installlib = $installprivlib;
380             #We're installing *.al and *.ix files into $installprivlib,
381             #but we have to delete old *.al and *.ix files from the 5.000
382             #distribution:
383             #This might not work because $archname might have changed.
384             &unlink("$installarchlib/$name");
385         }
386         system "cmp", "-s", $_, "$installlib/$name";
387         if ($?) {
388             &unlink("$installlib/$name");
389             mkpath("$installlib/$dir", 1, 0777);
390             cp_if_diff($_, "$installlib/$name");
391             # HP-UX (at least) needs to maintain execute permissions
392             # on dynamically-loaded libraries.
393             if ($name =~ /\.(so|$dlext)$/o) {
394                 &chmod(0555, "$installlib/$name");
395             }
396             else {
397                 &chmod(0444, "$installlib/$name");
398             }
399         }
400     } elsif (-d $_) {
401         mkpath("$installlib/$name", 1, 0777);
402     }
403 }
404
405 # Copy $from to $to, only if $from is different than $to.
406 # Also preserve modification times for .a libraries.
407 # On some systems, if you do
408 #   ranlib libperl.a
409 #   cp libperl.a /usr/local/lib/perl5/archlib/CORE/libperl.a
410 # and then try to link against the installed libperl.a, you might
411 # get an error message to the effect that the symbol table is older
412 # than the library.
413 sub cp_if_diff {
414     my($from,$to)=@_;
415     -f $from || die "$0: $from not found";
416     system "cmp", "-s", $from, $to;
417     if ($?) {
418         my ($atime, $mtime);
419         unlink($to);   # In case we don't have write permissions.
420         cmd("cp $from $to");
421         # Restore timestamps if it's a .a library.
422         if ($to =~ /\.a$/) {
423             ($atime, $mtime) = (stat $from)[8,9];
424             utime $atime, $mtime, $to;
425         }
426     }
427 }