fix prototype mismatch (from Hans Mulder <hansm@xs4all.nl>)
[p5sagit/p5-mst-13.2.git] / lib / CPAN / FirstTime.pm
1 package CPAN::Mirrored::By;
2
3 sub new { 
4     my($self,@arg) = @_;
5     bless [@arg], $self;
6 }
7 sub continent { shift->[0] }
8 sub country { shift->[1] }
9 sub url { shift->[2] }
10
11 package CPAN::FirstTime;
12
13 use strict;
14 use ExtUtils::MakeMaker qw(prompt);
15 use FileHandle ();
16 use File::Basename ();
17 use File::Path ();
18 use vars qw($VERSION);
19 $VERSION = substr q$Revision: 1.37 $, 10;
20
21 =head1 NAME
22
23 CPAN::FirstTime - Utility for CPAN::Config file Initialization
24
25 =head1 SYNOPSIS
26
27 CPAN::FirstTime::init()
28
29 =head1 DESCRIPTION
30
31 The init routine asks a few questions and writes a CPAN::Config
32 file. Nothing special.
33
34 =cut
35
36
37 sub init {
38     my($configpm) = @_;
39     use Config;
40     unless ($CPAN::VERSION) {
41         require CPAN::Nox;
42     }
43     eval {require CPAN::Config;};
44     $CPAN::Config ||= {};
45     local($/) = "\n";
46     local($\) = "";
47     local($|) = 1;
48
49     my($ans,$default,$local,$cont,$url,$expected_size);
50
51     #
52     # Files, directories
53     #
54
55     print qq[
56
57 CPAN is the world-wide archive of perl resources. It consists of about
58 100 sites that all replicate the same contents all around the globe.
59 Many countries have at least one CPAN site already. The resources
60 found on CPAN are easily accessible with the CPAN.pm module. If you
61 want to use CPAN.pm, you have to configure it properly.
62
63 If you do not want to enter a dialog now, you can answer 'no' to this
64 question and I\'ll try to autoconfigure. (Note: you can revisit this
65 dialog anytime later by typing 'o conf init' at the cpan prompt.)
66
67 ];
68
69     my $manual_conf =
70         ExtUtils::MakeMaker::prompt("Are you ready for manual configuration?",
71                                     "yes");
72     my $fastread;
73     {
74       local $^W;
75       if ($manual_conf =~ /^\s*y/i) {
76         $fastread = 0;
77         *prompt = \&ExtUtils::MakeMaker::prompt;
78       } else {
79         $fastread = 1;
80         $CPAN::Config->{urllist} ||= [];
81         # prototype should match that of &MakeMaker::prompt
82         *prompt = sub ($;$) {
83           my($q,$a) = @_;
84           my($ret) = defined $a ? $a : "";
85           printf qq{%s [%s]\n\n}, $q, $ret;
86           $ret;
87         };
88       }
89     }
90     print qq{
91
92 The following questions are intended to help you with the
93 configuration. The CPAN module needs a directory of its own to cache
94 important index files and maybe keep a temporary mirror of CPAN files.
95 This may be a site-wide directory or a personal directory.
96
97 };
98
99     my $cpan_home = $CPAN::Config->{cpan_home} || MM->catdir($ENV{HOME}, ".cpan");
100     if (-d $cpan_home) {
101         print qq{
102
103 I see you already have a  directory
104     $cpan_home
105 Shall we use it as the general CPAN build and cache directory?
106
107 };
108     } else {
109         print qq{
110
111 First of all, I\'d like to create this directory. Where?
112
113 };
114     }
115
116     $default = $cpan_home;
117     while ($ans = prompt("CPAN build and cache directory?",$default)) {
118       eval { File::Path::mkpath($ans); }; # dies if it can't
119       if ($@) {
120         warn "Couldn't create directory $ans.
121 Please retry.\n";
122         next;
123       }
124       if (-d $ans && -w _) {
125         last;
126       } else {
127         warn "Couldn't find directory $ans
128   or directory is not writable. Please retry.\n";
129       }
130     }
131     $CPAN::Config->{cpan_home} = $ans;
132
133     print qq{
134
135 If you want, I can keep the source files after a build in the cpan
136 home directory. If you choose so then future builds will take the
137 files from there. If you don\'t want to keep them, answer 0 to the
138 next question.
139
140 };
141
142     $CPAN::Config->{keep_source_where} = MM->catdir($CPAN::Config->{cpan_home},"sources");
143     $CPAN::Config->{build_dir} = MM->catdir($CPAN::Config->{cpan_home},"build");
144
145     #
146     # Cache size, Index expire
147     #
148
149     print qq{
150
151 How big should the disk cache be for keeping the build directories
152 with all the intermediate files?
153
154 };
155
156     $default = $CPAN::Config->{build_cache} || 10;
157     $ans = prompt("Cache size for build directory (in MB)?", $default);
158     $CPAN::Config->{build_cache} = $ans;
159
160     # XXX This the time when we refetch the index files (in days)
161     $CPAN::Config->{'index_expire'} = 1;
162
163     print qq{
164
165 By default, each time the CPAN module is started, cache scanning
166 is performed to keep the cache size in sync. To prevent from this,
167 disable the cache scanning with 'never'.
168
169 };
170
171     $default = $CPAN::Config->{scan_cache} || 'atstart';
172     do {
173         $ans = prompt("Perform cache scanning (atstart or never)?", $default);
174     } while ($ans ne 'atstart' && $ans ne 'never');
175     $CPAN::Config->{scan_cache} = $ans;
176
177     #
178     # prerequisites_policy
179     # Do we follow PREREQ_PM?
180     #
181     print qq{
182
183 The CPAN module can detect when a module that which you are trying to
184 build depends on prerequisites. If this happens, it can build the
185 prerequisites for you automatically ('follow'), ask you for
186 confirmation ('ask'), or just ignore them ('ignore'). Please set your
187 policy to one of the three values.
188
189 };
190
191     $default = $CPAN::Config->{prerequisites_policy} || 'follow';
192     do {
193       $ans =
194           prompt("Policy on building prerequisites (follow, ask or ignore)?",
195                  $default);
196     } while ($ans ne 'follow' && $ans ne 'ask' && $ans ne 'ignore');
197     $CPAN::Config->{prerequisites_policy} = $ans;
198
199     #
200     # External programs
201     #
202
203     print qq{
204
205 The CPAN module will need a few external programs to work
206 properly. Please correct me, if I guess the wrong path for a program.
207 Don\'t panic if you do not have some of them, just press ENTER for
208 those.
209
210 };
211
212     my $old_warn = $^W;
213     local $^W if $^O eq 'MacOS';
214     my(@path) = split /$Config{'path_sep'}/, $ENV{'PATH'};
215     local $^W = $old_warn;
216     my $progname;
217     for $progname (qw/gzip tar unzip make lynx ncftpget ncftp ftp/){
218       if ($^O eq 'MacOS') {
219           $CPAN::Config->{$progname} = 'not_here';
220           next;
221       }
222       my $progcall = $progname;
223       # we don't need ncftp if we have ncftpget
224       next if $progname eq "ncftp" && $CPAN::Config->{ncftpget} gt " ";
225       my $path = $CPAN::Config->{$progname} 
226           || $Config::Config{$progname}
227               || "";
228       if (MM->file_name_is_absolute($path)) {
229         # testing existence is not good enough, some have these exe
230         # extensions
231
232         # warn "Warning: configured $path does not exist\n" unless -e $path;
233         # $path = "";
234       } else {
235         $path = '';
236       }
237       unless ($path) {
238         # e.g. make -> nmake
239         $progcall = $Config::Config{$progname} if $Config::Config{$progname};
240       }
241
242       $path ||= find_exe($progcall,[@path]);
243       warn "Warning: $progcall not found in PATH\n" unless
244           $path; # not -e $path, because find_exe already checked that
245       $ans = prompt("Where is your $progname program?",$path) || $path;
246       $CPAN::Config->{$progname} = $ans;
247     }
248     my $path = $CPAN::Config->{'pager'} || 
249         $ENV{PAGER} || find_exe("less",[@path]) || 
250             find_exe("more",[@path]) || ($^O eq 'MacOS' ? $ENV{EDITOR} : 0 )
251             || "more";
252     $ans = prompt("What is your favorite pager program?",$path);
253     $CPAN::Config->{'pager'} = $ans;
254     $path = $CPAN::Config->{'shell'};
255     if (MM->file_name_is_absolute($path)) {
256         warn "Warning: configured $path does not exist\n" unless -e $path;
257         $path = "";
258     }
259     $path ||= $ENV{SHELL};
260     if ($^O eq 'MacOS') {
261         $CPAN::Config->{'shell'} = 'not_here';
262     } else {
263         $path =~ s,\\,/,g if $^O eq 'os2';      # Cosmetic only
264         $ans = prompt("What is your favorite shell?",$path);
265         $CPAN::Config->{'shell'} = $ans;
266     }
267
268     #
269     # Arguments to make etc.
270     #
271
272     print qq{
273
274 Every Makefile.PL is run by perl in a separate process. Likewise we
275 run \'make\' and \'make install\' in processes. If you have any parameters
276 \(e.g. PREFIX, INSTALLPRIVLIB, UNINST or the like\) you want to pass to
277 the calls, please specify them here.
278
279 If you don\'t understand this question, just press ENTER.
280
281 };
282
283     $default = $CPAN::Config->{makepl_arg} || "";
284     $CPAN::Config->{makepl_arg} =
285         prompt("Parameters for the 'perl Makefile.PL' command?",$default);
286     $default = $CPAN::Config->{make_arg} || "";
287     $CPAN::Config->{make_arg} = prompt("Parameters for the 'make' command?",$default);
288
289     $default = $CPAN::Config->{make_install_arg} || $CPAN::Config->{make_arg} || "";
290     $CPAN::Config->{make_install_arg} =
291         prompt("Parameters for the 'make install' command?",$default);
292
293     #
294     # Alarm period
295     #
296
297     print qq{
298
299 Sometimes you may wish to leave the processes run by CPAN alone
300 without caring about them. As sometimes the Makefile.PL contains
301 question you\'re expected to answer, you can set a timer that will
302 kill a 'perl Makefile.PL' process after the specified time in seconds.
303
304 If you set this value to 0, these processes will wait forever. This is
305 the default and recommended setting.
306
307 };
308
309     $default = $CPAN::Config->{inactivity_timeout} || 0;
310     $CPAN::Config->{inactivity_timeout} =
311         prompt("Timeout for inactivity during Makefile.PL?",$default);
312
313     # Proxies
314
315     print qq{
316
317 If you\'re accessing the net via proxies, you can specify them in the
318 CPAN configuration or via environment variables. The variable in
319 the \$CPAN::Config takes precedence.
320
321 };
322
323     for (qw/ftp_proxy http_proxy no_proxy/) {
324         $default = $CPAN::Config->{$_} || $ENV{$_};
325         $CPAN::Config->{$_} = prompt("Your $_?",$default);
326     }
327
328     #
329     # MIRRORED.BY
330     #
331
332     conf_sites() unless $fastread;
333
334     unless (@{$CPAN::Config->{'wait_list'}||[]}) {
335         print qq{
336
337 WAIT support is available as a Plugin. You need the CPAN::WAIT module
338 to actually use it.  But we need to know your favorite WAIT server. If
339 you don\'t know a WAIT server near you, just press ENTER.
340
341 };
342         $default = "wait://ls6.informatik.uni-dortmund.de:1404";
343         $ans = prompt("Your favorite WAIT server?\n  ",$default);
344         push @{$CPAN::Config->{'wait_list'}}, $ans;
345     }
346
347     # We don't ask that now, it will be noticed in time, won't it?
348     $CPAN::Config->{'inhibit_startup_message'} = 0;
349     $CPAN::Config->{'getcwd'} = 'cwd';
350
351     print "\n\n";
352     CPAN::Config->commit($configpm);
353 }
354
355 sub conf_sites {
356   my $m = 'MIRRORED.BY';
357   my $mby = MM->catfile($CPAN::Config->{keep_source_where},$m);
358   File::Path::mkpath(File::Basename::dirname($mby));
359   if (-f $mby && -f $m && -M $m < -M $mby) {
360     require File::Copy;
361     File::Copy::copy($m,$mby) or die "Could not update $mby: $!";
362   }
363   while () {
364     if ( ! -f $mby ){
365       print qq{You have no $mby
366   I\'m trying to fetch one
367 };
368       $mby = CPAN::FTP->localize($m,$mby,3);
369     } elsif (-M $mby > 30 ) {
370       print qq{Your $mby is older than 30 days,
371   I\'m trying to fetch one
372 };
373       $mby = CPAN::FTP->localize($m,$mby,3);
374     } elsif (-s $mby == 0) {
375       print qq{You have an empty $mby,
376   I\'m trying to fetch one
377 };
378       $mby = CPAN::FTP->localize($m,$mby,3);
379     } else {
380       last;
381     }
382   }
383   read_mirrored_by($mby);
384 }
385
386 sub find_exe {
387     my($exe,$path) = @_;
388     my($dir);
389     #warn "in find_exe exe[$exe] path[@$path]";
390     for $dir (@$path) {
391         my $abs = MM->catfile($dir,$exe);
392         if (($abs = MM->maybe_command($abs))) {
393             return $abs;
394         }
395     }
396 }
397
398 sub picklist {
399     my($items,$prompt,$default,$require_nonempty,$empty_warning)=@_;
400     $default ||= '';
401
402     my ($item, $i);
403     for $item (@$items) {
404         printf "(%d) %s\n", ++$i, $item;
405     }
406
407     my @nums;
408     while (1) {
409         my $num = prompt($prompt,$default);
410         @nums = split (' ', $num);
411         (warn "invalid items entered, try again\n"), next
412             if grep (/\D/ || $_ < 1 || $_ > $i, @nums);
413         if ($require_nonempty) {
414             (warn "$empty_warning\n"), next
415                 unless @nums;
416         }
417         last;
418     }
419     print "\n";
420     for (@nums) { $_-- }
421     @{$items}[@nums];
422 }
423
424 sub read_mirrored_by {
425     my($local) = @_;
426     my(%all,$url,$expected_size,$default,$ans,$host,$dst,$country,$continent,@location);
427     my $fh = FileHandle->new;
428     $fh->open($local) or die "Couldn't open $local: $!";
429     local $/ = "\012";
430     while (<$fh>) {
431         ($host) = /^([\w\.\-]+)/ unless defined $host;
432         next unless defined $host;
433         next unless /\s+dst_(dst|location)/;
434         /location\s+=\s+\"([^\"]+)/ and @location = (split /\s*,\s*/, $1) and
435             ($continent, $country) = @location[-1,-2];
436         $continent =~ s/\s\(.*//;
437         $continent =~ s/\W+$//; # if Jarkko doesn't know latitude/longitude
438         /dst_dst\s+=\s+\"([^\"]+)/  and $dst = $1;
439         next unless $host && $dst && $continent && $country;
440         $all{$continent}{$country}{$dst} = CPAN::Mirrored::By->new($continent,$country,$dst);
441         undef $host;
442         $dst=$continent=$country="";
443     }
444     $fh->close;
445     $CPAN::Config->{urllist} ||= [];
446     my(@previous_urls);
447     if (@previous_urls = @{$CPAN::Config->{urllist}}) {
448         $CPAN::Config->{urllist} = [];
449     }
450
451     print qq{
452
453 Now we need to know where your favorite CPAN sites are located. Push
454 a few sites onto the array (just in case the first on the array won\'t
455 work). If you are mirroring CPAN to your local workstation, specify a
456 file: URL.
457
458 First, pick a nearby continent and country (you can pick several of
459 each, separated by spaces, or none if you just want to keep your
460 existing selections). Then, you will be presented with a list of URLs
461 of CPAN mirrors in the countries you selected, along with previously
462 selected URLs. Select some of those URLs, or just keep the old list.
463 Finally, you will be prompted for any extra URLs -- file:, ftp:, or
464 http: -- that host a CPAN mirror.
465
466 };
467
468     my (@cont, $cont, %cont, @countries, @urls, %seen);
469     my $no_previous_warn = 
470        "Sorry! since you don't have any existing picks, you must make a\n" .
471        "geographic selection.";
472     @cont = picklist([sort keys %all],
473                      "Select your continent (or several nearby continents)",
474                      '',
475                      ! @previous_urls,
476                      $no_previous_warn);
477
478
479     foreach $cont (@cont) {
480         my @c = sort keys %{$all{$cont}};
481         @cont{@c} = map ($cont, 0..$#c);
482         @c = map ("$_ ($cont)", @c) if @cont > 1;
483         push (@countries, @c);
484     }
485
486     if (@countries) {
487         @countries = picklist (\@countries,
488                                "Select your country (or several nearby countries)",
489                                '',
490                                ! @previous_urls,
491                                $no_previous_warn);
492         %seen = map (($_ => 1), @previous_urls);
493         # hmmm, should take list of defaults from CPAN::Config->{'urllist'}...
494         foreach $country (@countries) {
495             (my $bare_country = $country) =~ s/ \(.*\)//;
496             my @u = sort keys %{$all{$cont{$bare_country}}{$bare_country}};
497             @u = grep (! $seen{$_}, @u);
498             @u = map ("$_ ($bare_country)", @u)
499                if @countries > 1;
500             push (@urls, @u);
501         }
502     }
503     push (@urls, map ("$_ (previous pick)", @previous_urls));
504     my $prompt = "Select as many URLs as you like";
505     if (@previous_urls) {
506        $default = join (' ', ((scalar @urls) - (scalar @previous_urls) + 1) ..
507                              (scalar @urls));
508        $prompt .= "\n(or just hit RETURN to keep your previous picks)";
509     }
510
511     @urls = picklist (\@urls, $prompt, $default);
512     foreach (@urls) { s/ \(.*\)//; }
513     %seen = map (($_ => 1), @urls);
514
515     do {
516         $ans = prompt ("Enter another URL or RETURN to quit:", "");
517
518         if ($ans) {
519             $ans =~ s|/?$|/|; # has to end with one slash
520             $ans = "file:$ans" unless $ans =~ /:/; # without a scheme is a file:
521             if ($ans =~ /^\w+:\/./) {
522                push @urls, $ans 
523                   unless $seen{$ans};
524             }
525             else {
526                 print qq{"$ans" doesn\'t look like an URL at first sight.
527 I\'ll ignore it for now.  You can add it to $INC{'CPAN/MyConfig.pm'}
528 later if you\'re sure it\'s right.\n};
529             }
530         }
531     } while $ans;
532
533     push @{$CPAN::Config->{urllist}}, @urls;
534     # xxx delete or comment these out when you're happy that it works
535     print "New set of picks:\n";
536     map { print "  $_\n" } @{$CPAN::Config->{urllist}};
537 }
538
539 1;