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