73da720ece25d78f206292d1bfc627788269e0e4
[p5sagit/p5-mst-13.2.git] / installperl
1 #!./perl
2 BEGIN { @INC=('./lib', '../lib') }
3
4 use File::Find;
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 = ('cppstdin', 'c2ph', 'pstruct', 'x2p/s2p', 'x2p/find2perl');
17 @manpages = (<pod/*.man>, 'x2p/a2p.man', 'x2p/s2p.man');
18
19 # Read in the config file.
20
21 open(CONFIG, "config.sh") || die "You haven't run Configure yet!\n";
22 while (<CONFIG>) {
23     if (s/^(\w+=)/\$$1/) {
24         $accum =~ s/'undef'/undef/g;
25         eval $accum;
26         $accum = '';
27     }
28     $accum .= $_;
29 }
30 close CONFIG;
31
32 $ver = sprintf("%5.3f", $] + 0);
33 $release = substr($ver,0,3);
34 $patchlevel = substr($ver,3,2);
35
36 # Do some quick sanity checks.
37
38 if ($d_dosuid && $>) { die "You must run as root to install suidperl\n"; }
39
40    $installbin          || die "No installbin directory in config.sh\n";
41 -d $installbin          || die "$installbin is not a directory\n";
42 -w $installbin          || die "$installbin is not writable by you\n"
43         unless $installbin =~ m#^/afs/# || $nonono;
44
45 -x 'perl'               || die "perl isn't executable!\n";
46 -x 'suidperl'           || die "suidperl isn't executable!\n" if $d_dosuid;
47
48 -x 't/TEST'             || warn "WARNING: You've never run 'make test'!!!",
49         "  (Installing anyway.)\n";
50
51 if ($d_shrplib) {
52     if (!<libperl*.$so*>) {
53         warn "WARNING: Can't find libperl*.$so* to install into $shrpdir.",
54             "  (Installing other things anyway.)\n";
55     } else {
56         &makedir($shrpdir);
57         -w $shrpdir     || die "$shrpdir is not writable by you\n";
58         &cmd("cp libperl*.$so* $shrpdir");
59     }
60 }
61
62 # First we install the version-numbered executables.
63
64 &unlink("$installbin/perl$ver");
65 &cmd("cp perl $installbin/perl$ver");
66
67 &unlink("$installbin/sperl$ver");
68 if ($d_dosuid) {
69     &cmd("cp suidperl $installbin/sperl$ver");
70     &chmod(04711, "$installbin/sperl$ver");
71 }
72
73 exit 0 if $versiononly;
74
75 # Make links to ordinary names if installbin directory isn't current directory.
76
77 if (! &samepath($installbin, '.')) {
78     &unlink("$installbin/perl", "$installbin/suidperl");
79     &link("$installbin/perl$ver", "$installbin/perl");
80     &link("$installbin/sperl$ver", "$installbin/suidperl") if $d_dosuid;
81 }
82
83 if (! &samepath($installbin, 'x2p')) {
84     &unlink("$installbin/a2p");
85     &cmd("cp x2p/a2p $installbin/a2p");
86     &chmod(0755, "$installbin/a2p");
87 }
88
89 # Install scripts.
90
91 &makedir($installscript);
92
93 for (@scripts) {
94     if (-f $_) {   # cppstdin might not exist on this system.
95         &cmd("cp $_ $installscript");
96         s#.*/##; &chmod(0755, "$installscript/$_");
97     }
98 }
99
100 # Install man pages.
101
102 if ($installmansrc ne '') {
103     &makedir($installmansrc);
104
105     if (! &samepath($installmansrc, '.')) {
106         for (@manpages) {
107             ($new = $_) =~ s/man$/$manext/;
108             $new =~ s#.*/##;
109             print STDERR "  Installing $installmansrc/$new\n";
110             next if $nonono;
111             open(MI,$_) || warn "Can't open $_: $!\n";
112             open(MO,">$installmansrc/$new") || warn "Can't install $installmansrc/$new: $!\n";
113             print MO ".ds RP Release $release Patchlevel $patchlevel\n";
114             while (<MI>) {
115                 print MO;
116             }
117             close MI;
118             close MO;
119         }
120     }
121 }
122
123 # Install library files.
124
125 $do_installarchlib = $do_installprivlib = 0;
126     
127 &makedir($installprivlib);
128 &makedir($installarchlib);
129 if (chdir "lib") {
130     $do_installarchlib = ! &samepath($installarchlib, '.');
131     $do_installprivlib = ! &samepath($installprivlib, '.');
132
133     if ($do_installarchlib || $do_installprivlib) {
134         find(\&installlib, '.');
135     }
136     chdir ".." || die "Can't cd back to source directory: $!\n";
137 }
138 else {
139     warn "Can't cd to lib to install lib files: $!\n";
140 }
141
142 # Install header files
143 makedir("$installarchlib/CORE");
144 foreach $file (<*.h libperl*.a>) {
145     cp_if_diff($file,"$installarchlib/CORE/$file");
146 }
147
148 # Offer to install perl in a "standard" location
149
150 $mainperl_is_instperl = 0;
151
152 if (-w $mainperldir && ! &samepath($mainperldir, $installbin) && !$nonono) {
153     # First make sure $mainperldir/perl is not already the same as
154     # the perl we just installed
155     if (-x "$mainperldir/perl") {
156         # Try to be clever about mainperl being a symbolic link
157         # to binexp/perl if binexp and installbin are different.
158         $mainperl_is_instperl =
159             &samepath("$mainperldir/perl", "$installbin/perl") ||
160              (($binexp ne $installbin) &&
161               (-l "$mainperldir/perl") &&
162               ((readlink "$mainperldir/perl") eq "$binexp/perl"));
163     }
164     if ((! $mainperl_is_instperl) &&
165         (&yn("Many scripts expect perl to be installed as " .
166              "$mainperldir/perl.\n" . 
167              "Do you wish to have $mainperldir/perl be the same as\n" .
168              "$binexp/perl? [y] ")))
169     {   
170         unlink("$mainperldir/perl");
171         eval 'link("$installbin/perl", "$mainperldir/perl")' ||
172         eval 'symlink("$binexp/perl", "$mainperldir/perl")' ||
173         &cmd("cp $installbin/perl $mainperldir");
174         $mainperl_is_instperl = 1;
175     }
176 }
177
178 # Check to make sure there aren't other perls around in installer's
179 # path.  This is probably UNIX-specific.  Check all absolute directories
180 # in the path except for where public executables are supposed to live.
181 # Also skip $mainperl if the user opted to have it be a link to the
182 # installed perl.
183
184 @path = split(/:/, $ENV{"PATH"});
185 @otherperls = ();
186 for (@path) {
187     next unless m,^/,;
188     next if ($_ eq $binexp);
189     # Use &samepath here because some systems have other dirs linked
190     # to $mainperldir (like SunOS)
191     next if ($mainperl_is_instperl && &samepath($_, $mainperldir));
192     push(@otherperls, "$_/perl") if (-x "$_/perl" && ! -d "$_/perl");
193 }
194 if (@otherperls) {
195     print STDERR "\nWarning: perl appears in your path in the following " .
196         "locations beyond where\nwe just installed it:\n";
197     for (@otherperls) {
198         print STDERR "    ", $_, "\n";
199     }
200     print STDERR "\n";
201 }
202
203 print STDERR "  Installation complete\n";
204
205 exit 0;
206
207 ###############################################################################
208
209 sub yn {
210     local($prompt) = @_;
211     local($answer);
212     local($default) = $prompt =~ m/\[([yn])\]\s*$/i;
213     print STDERR $prompt;
214     chop($answer = <STDIN>);
215     $answer = $default if $answer =~ m/^\s*$/;
216     ($answer =~ m/^[yY]/);
217 }
218
219 sub unlink {
220     local(@names) = @_;
221
222     foreach $name (@names) {
223         next unless -e $name;
224         print STDERR "  unlink $name\n";
225         unlink($name) || warn "Couldn't unlink $name: $!\n" unless $nonono;
226     }
227 }
228
229 sub cmd {
230     local($cmd) = @_;
231     print STDERR "  $cmd\n";
232     unless ($nonono) {
233         system $cmd;
234         warn "Command failed!!!\n" if $?;
235     }
236 }
237
238 sub link {
239     local($from,$to) = @_;
240
241     print STDERR "  ln $from $to\n";
242     link($from,$to) || warn "Couldn't link $from to $to: $!\n" unless $nonono;
243 }
244
245 sub chmod {
246     local($mode,$name) = @_;
247
248     printf STDERR "  chmod %o %s\n", $mode, $name;
249     chmod($mode,$name) || warn sprintf("Couldn't chmod %o %s: $!\n",$mode,$name)
250         unless $nonono;
251 }
252
253 sub makedir {
254     local($dir) = @_;
255     unless (-d $dir) {
256         local($shortdir) = $dir;
257
258         $shortdir =~ s#(.*)/.*#$1#;
259         &makedir($shortdir);
260
261         print STDERR "  mkdir $dir\n";
262         mkdir($dir, 0777) || warn "Couldn't create $dir: $!\n" unless $nonono;
263     }
264 }
265
266 sub samepath {
267     local($p1, $p2) = @_;
268     local($dev1, $ino1, $dev2, $ino2);
269
270     if ($p1 ne p2) {
271         ($dev1, $ino1) = stat($p1);
272         ($dev2, $ino2) = stat($p2);
273         ($dev1 == $dev2 && $ino1 == $ino2);
274     }
275     else {
276         1;
277     }
278 }
279
280 sub installlib {
281     my $dir = $File::Find::dir;
282     $dir =~ s#^\.(?![^/])/?##;
283
284     my $name = $_;
285     $name = "$dir/$name" if $dir ne '';
286
287     my $installlib = $installprivlib;
288     if ((substr($dir, 0, 4) eq 'auto') || ($name eq 'Config.pm')) {
289         $installlib = $installarchlib;
290         return unless $do_installarchlib;
291     } else {
292         return unless $do_installprivlib;
293     }
294
295     if (-f $_) {
296         if (/\.al$/ || /\.ix$/) {
297             $installlib = $installprivlib;
298             #We're installing *.al and *.ix files into $installprivlib,
299             #but we have to delete old *.al and *.ix files from the 5.000
300             #distribution:
301             &unlink("$installarchlib/$name");
302         }
303         system "cmp", "-s", $_, "$installlib/$name";
304         if ($?) {
305             &unlink("$installlib/$name");
306             &makedir("$installlib/$dir");
307             &cmd("cp $_ $installlib/$dir");
308             &chmod(0644, "$installlib/$name");
309         }
310     } elsif (-d $_) {
311         &makedir("$installlib/$name");
312     }
313 }
314
315 sub cp_if_diff {
316     my($from,$to)=@_;
317     -f $from || die "$0: $from not found";
318     system "cmp", "-s", $from, $to;
319     if ($?) {
320         cmd("cp $from $to");
321     }
322 }