This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Quote string argument in example -- necessary if using strict subs
[perl5.git] / installman
1 #!./perl
2 BEGIN { @INC = ('lib') }
3 use Config;
4 use Getopt::Long;
5 use File::Find;
6 use File::Path qw(mkpath);
7 use subs qw(unlink chmod rename link);
8 require Cwd;
9
10 umask 022;
11 $ENV{SHELL} = 'sh' if $^O eq 'os2';
12
13 $ver = $];
14 $release = substr($ver,0,3);   # Not used presently.
15 $patchlevel = substr($ver,3,2);
16 die "Patchlevel of perl ($patchlevel)",
17     "and patchlevel of config.sh ($Config{'PATCHLEVEL'}) don't match\n"
18         if $patchlevel != $Config{'PATCHLEVEL'};
19
20 $usage =
21 "Usage:  installman --man1dir=/usr/wherever --man1ext=1
22                     --man3dir=/usr/wherever --man3ext=3
23                     --notify --help
24         Defaults are:
25         man1dir = $Config{'installman1dir'};
26         man1ext = $Config{'man1ext'};
27         man3dir = $Config{'installman3dir'};
28         man3ext = $Config{'man3ext'};
29         --notify (or -n) just lists commands that would be executed.\n";
30
31 GetOptions( qw( man1dir=s man1ext=s man3dir=s man3ext=s notify n help)) 
32         || die $usage;
33 die $usage if $opt_help;
34
35 # These are written funny to avoid -w typo warnings.
36 $man1dir = defined($opt_man1dir) ? $opt_man1dir : $Config{'installman1dir'};
37 $man1ext = defined($opt_man1ext) ? $opt_man1ext : $Config{'man1ext'};
38 $man3dir = defined($opt_man3dir) ? $opt_man3dir : $Config{'installman3dir'};
39 $man3ext = defined($opt_man3ext) ? $opt_man3ext : $Config{'man3ext'};
40
41 $notify = $opt_notify || $opt_n;
42
43 #Sanity checks
44
45 -x  "./perl$Config{exe_ext}" 
46   or warn "./perl$Config{exe_ext} not found!  Have you run make?\n";
47 -d  $Config{'installprivlib'}
48         || warn "Perl library directory $Config{'installprivlib'} not found.
49                 Have you run make install?.  (Installing anyway.)\n";
50 -x "t/perl$Config{exe_ext}"             || warn "WARNING: You've never run 'make test'!!!",
51         "  (Installing anyway.)\n";
52
53 # Install the main pod pages.
54 runpod2man('pod', $man1dir, $man1ext);
55
56 # Install the pods for library modules.
57 runpod2man('lib', $man3dir, $man3ext);
58
59 sub runpod2man {
60     my($poddir, $mandir, $manext) = @_;
61     my($builddir) = Cwd::getcwd();
62
63     if ($mandir eq ' ' or $mandir eq '') {
64         print STDERR "Skipping installation of $poddir man pages.\n";
65         return;
66     }
67
68     chdir $poddir || die "Unable to cd to $poddir directory!\n$!\n";
69
70     # We insist on using the current version of pod2man in case there
71     # are enhancements or changes from previous installed versions.
72     # The error message doesn't include the '..' because the user
73     # won't be aware that we've chdir to $poddir.
74     -r  "../pod/pod2man" || die "Executable pod/pod2man not found.\n";
75
76     # We want to be sure to use the current perl.  We can't rely on
77     # the installed perl because it might not be actually installed
78     # yet. (The user may have set the $install* Configure variables 
79     # to point to some temporary home, from which the executable gets
80     # installed by occult means.)
81     $pod2man = "../perl -I ../lib ../pod/pod2man --section=$manext --official";
82
83     mkpath($mandir, 1, 0777);  # In File::Path
84     # Make a list of all the .pm and .pod files in the directory.  We will
85     # always run pod2man from the lib directory and feed it the full pathname
86     # of the pod.  This might be useful for pod2man someday.
87     @modpods = ();
88     find(\&lsmodpods, '.');
89     foreach $mod (@modpods) {
90         $manpage = $mod;
91         my $tmp;
92         # Skip .pm files that have corresponding .pod files, and Functions.pm.
93         next if (($tmp = $mod) =~ s/\.pm$/.pod/ && -f $tmp);
94         next if ($mod eq 'Pod/Functions.pm');   #### Used only by pod itself
95
96         # Convert name from  File/Basename.pm to File::Basename.3 format,
97         # if necessary.
98         $manpage =~ s#\.p(m|od)$##;
99         if ($^O eq 'os2') {
100           $manpage =~ s#/#.#g;
101         } else {
102           $manpage =~ s#/#::#g;
103         }
104         $tmp = "${mandir}/${manpage}.tmp";
105         $manpage = "${mandir}/${manpage}.${manext}";
106         if (&cmd("$pod2man $mod > $tmp") == 0 && !$notify && -s $tmp) {
107             rename($tmp, $manpage) && next;
108         }
109         unless ($notify) {
110     unlink($tmp);
111         }
112     }
113     chdir "$builddir" || die "Unable to cd back to $builddir directory!\n$!\n";
114 }
115
116 sub lsmodpods {
117     my $dir  = $File::Find::dir;
118     my $name = $File::Find::name;
119     if (-f $_) {
120         $name =~ s#^\./##;
121         push(@modpods, $name) if ($name =~ /\.p(m|od)$/);
122     }
123 }
124
125 print STDERR "  Installation complete\n";
126
127 exit 0;
128     
129
130 ###############################################################################
131 # Utility subroutines from installperl
132
133 sub cmd {
134     local($cmd) = @_;
135     print STDERR "  $cmd\n";
136     unless ($notify) {
137         if ($Config{d_fork}) {
138             fork ? wait : exec $cmd;  # Allow user to ^C out of command.
139         }
140         else {
141             system $cmd;
142         }
143         warn "Command failed!!\n" if $?;
144     }
145     return $? != 0;
146 }
147
148 sub unlink {
149     local(@names) = @_;
150     my $cnt = 0;
151
152     foreach $name (@names) {
153 next unless -e $name;
154 chmod 0777, $name if $^O eq 'os2';
155 print STDERR "  unlink $name\n";
156 ( CORE::unlink($name) and ++$cnt 
157     or warn "Couldn't unlink $name: $!\n" ) unless $nonono;
158     }
159     return $cnt;
160 }
161
162 sub link {
163     local($from,$to) = @_;
164
165     print STDERR "  ln $from $to\n";
166     eval { CORE::link($from,$to) }
167 || system('cp', $from, $to) == 0
168 || warn "Couldn't link $from to $to: $!\n" unless $notify;
169 }
170
171 sub rename {
172     local($from,$to) = @_;
173     if (-f $to and not unlink($to)) {
174 my($i);
175 for ($i = 1; $i < 50; $i++) {
176     last if CORE::rename($to, "$to.$i");
177 }
178 warn("Cannot rename to `$to.$i': $!"), return 0 
179     if $i >= 50;        # Give up!
180     }
181     link($from,$to) || return 0;
182     unlink($from);
183 }
184
185 sub chmod {
186     local($mode,$name) = @_;
187
188     printf STDERR "  chmod %o %s\n", $mode, $name;
189     CORE::chmod($mode,$name) || warn sprintf("Couldn't chmod %o %s: $!\n",$mode,$name)
190         unless $notify;
191 }
192
193 sub samepath {
194     local($p1, $p2) = @_;
195     local($dev1, $ino1, $dev2, $ino2);
196
197     if ($p1 ne $p2) {
198         ($dev1, $ino1) = stat($p1);
199         ($dev2, $ino2) = stat($p2);
200         ($dev1 == $dev2 && $ino1 == $ino2);
201     }
202     else {
203         1;
204     }
205 }