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