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