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