summaryrefslogtreecommitdiffstats
path: root/urpmi
blob: b49e64f0aa2d8e8471f8454d7b799fddf9785304 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
#!/usr/bin/perl
# vim:et:ai:ts=4:sts=4:sw=4

#- Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005 MandrakeSoft SA
#- Copyright (C) 2005-2010 Mandriva SA
#- Copyright (C) 2011-2020 Mageia
#-
#- This program is free software; you can redistribute it and/or modify
#- it under the terms of the GNU General Public License as published by
#- the Free Software Foundation; either version 2, or (at your option)
#- any later version.
#-
#- This program is distributed in the hope that it will be useful,
#- but WITHOUT ANY WARRANTY; without even the implied warranty of
#- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#- GNU General Public License for more details.
#-
#- You should have received a copy of the GNU General Public License
#- along with this program; if not, write to the Free Software
#- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

use strict;
use urpm;
use urpm::args;
use urpm::msg;
use urpm::media;
use urpm::select;
use urpm::util qw(cat_ difference2 find member partition untaint);
use urpm::main_loop;
use Term::ANSIColor;

#- default options.
our $update = 0;
our $media = '';
our $searchmedia;
our $excludemedia = '';
our $sortmedia = '';
our $allow_medium_change = 0;
our $auto_select = 0;
our $auto_update = 0;
our $no_install = 0;
our $no_remove = 0;
our $install_src = 0;
our $clean = 0;
our $noclean = 0;
our $force = 0;
our $parallel = '';
our $env = '';
our $test = 0;
our $all = 0;
our $use_provides = 1;
our $logfile = '';
our $restricted = 0;
our $forcekey = 0;

my @files;
my @src_files;
my @names;
my @src_names;

$ENV{PATH} = "/sbin:/usr/sbin:/bin:/usr/bin";
delete @ENV{qw(ENV BASH_ENV IFS CDPATH)};
$ENV{HOME} ||= "/root";
$ENV{USER} ||= "root";

sub usage () {
    print color('bright_yellow');
    print urpm::args::copyright('urpmi', [ '1999-2010', 'Mandriva' ], [ '2011-2021', 'Mageia' ]);
    print "
" . color('bold cyan') . "  --help" . color('reset') . "             - " . N("print this help message.") ."
" . color('bold cyan') . "  --media" . color('reset') . "            - " . N("use only the given media, separated by comma.") ."
" . color('bold cyan') . "  --excludemedia" . color('reset') . "     - " . N("do not use the given media, separated by comma.") ."
" . color('bold cyan') . "  --update" . color('reset') . "           - " . N("use only update media.") ."
" . color('bold cyan') . "  --searchmedia" . color('reset') . "      - " . N("use only the given media to search requested packages.") ."
" . color('bold cyan') . "  --sortmedia" . color('reset') . "        - " . N("sort media according to substrings separated by comma.") ."
" . color('bold cyan') . "  --synthesis" . color('reset') . "        - " . N("use the given synthesis instead of urpmi db.") ."
" . color('bold cyan') . "  --auto" . color('reset') . "             - " . N("non-interactive mode, assume default answers to questions.") ."
" . color('bold cyan') . "  --auto-select" . color('reset') . "      - " . N("automatically select packages to upgrade the system.") ."
" . color('bold cyan') . "  --auto-update" . color('reset') . "      - " . N("update media then upgrade the system.") ."
" . color('bold cyan') . "    --no-md5sum" . color('reset') . "        - " . N("disable MD5SUM file checking.") ."
" . color('bold cyan') . "    --force-key" . color('reset') . "        - " . N("force update of gpg key.") ."
" . color('bold cyan') . "  --auto-orphans" . color('reset') . "     - " . N("remove orphans without asking") ."
" . color('bold cyan') . "  --no-recommends" . color('reset') . "    - " . N("do not auto select \"recommended\" packages.") ."
" . color('bold cyan') . "  --no-uninstall" . color('reset') . "     - " . N("never ask to uninstall a package, abort the installation.") ."
" . color('bold cyan') . "  --no-install" . color('reset') . "       - " . N("don't install packages (only download)") ."
" . color('bold cyan') . "  --keep" . color('reset') . "             - " . N("keep existing packages if possible, reject requested
                       packages that lead to removals.") ."
" . color('bold cyan') . "  --split-level" . color('reset') . "      - " . N("split in small transaction if more than given packages
                       are going to be installed or upgraded,
                       default is %d."
                   , urpm::default_options()->{'split-level'}) . "
" . color('bold cyan') . "  --split-length" . color('reset') . "     - " . N("small transaction length, default is %d."
    , urpm::default_options()->{'split-length'}) . "
" . color('bold cyan') . "  --fuzzy, -y" . color('reset') . "        - " . N("impose fuzzy search.") . "
" . color('bold cyan') . "  --buildrequires" . color('reset') . "    - " . N("install the buildrequires of the packages.") . "
" . color('bold cyan') . "  --install-src" . color('reset') . "      - " . N("install only source package (no binaries).") . "
" . color('bold cyan') . "  --clean" . color('reset') . "            - " . N("remove rpm from cache before anything else.") . "
" . color('bold cyan') . "  --noclean" . color('reset') . "          - " . N("don't clean rpms from cache.") . "
" . color('bold cyan') . "  --justdb" . color('reset') . "           - " . N("update only the rpm db, not the filesystem.") . "
" . color('bold cyan') . "  --downgrade" . color('reset') . "        - " . N("downgrade a package from the version currently installed
                       to the previously highest version") . "
" . color('bold cyan') . "  --replacepkgs" . color('reset') . "      - " . N("force installing packages which are already installed.") . "
" . color('bold cyan') . "  --force" . color('reset') . "            - " . N("force invocation even if some packages do not exist.") . "
" . color('bold cyan') . "  --allow-nodeps" . color('reset') . "     - " . N("allow asking user to install packages without
                       dependencies checking.") . "
" . color('bold cyan') . "  --allow-force" . color('reset') . "      - " . N("allow asking user to install packages without
                       dependencies checking and integrity.") . "
" . color('bold cyan') . "  --allow-recommends" . color('reset') . " - " . N("auto select \"recommended\" packages.") . "
" . color('bold cyan') . "  --parallel" . color('reset') . "         - " . N("distributed urpmi across machines of alias.") . "
" . color('bold cyan') . "  --root" . color('reset') . "             - " . N("use another root for rpm installation.") . "
" . color('bold cyan') . "  --urpmi-root" . color('reset') . "       - " . N("use another root for urpmi db & rpm installation.") . "
" . color('bold cyan') . "  --use-distrib" . color('reset') . "      - " . N("configure urpmi on the fly from a distrib tree, useful
                       to install a chroot with --root option.") . "
" . color('bold cyan') . "  --metalink" . color('reset') . "         - " . N("generate and use a local metalink.") . "
" . color('bold cyan') . "  --download-all" . color('reset') . "     - " . N("download all needed packages before trying to install them") . "
" . color('bold cyan') . "  --downloader" . color('reset') . "       - " . N("program to use to retrieve distant files. 
                       known programs: %s", join(', ', urpm::download::ftp_http_downloaders())) . "
" . color('bold cyan') . "  --curl-options" . color('reset') . "     - " . N("additional options to pass to curl.") . "
" . color('bold cyan') . "  --rsync-options" . color('reset') . "    - " . N("additional options to pass to rsynci.") . "
" . color('bold cyan') . "  --wget-options" . color('reset') . "     - " . N("additional options to pass to wget.") . "
" . color("bold cyan") . "  --prozilla-options" . color('reset') . " - " . N("additional options to pass to prozilla.") . "
" . color("bold cyan") . "  --aria2-options" . color('reset') . "    - " . N("additional options to pass to aria2.") . "
" . color("bold cyan") . "  --limit-rate" . color('reset') . "       - " . N("limit the download speed.") . "
" . color("bold cyan") . "  --resume" . color('reset') . "           - " . N("resume transfer of partially-downloaded files.
                       (--no-resume disables it, default is disabled).") . "
" . color("bold cyan") . "  --proxy" . color('reset') . "            - " . N("use specified HTTP proxy, the port number is assumed
                       to be 1080 by default (format is <proxyhost[:port]>).") . "
" . color("bold cyan") . "  --proxy-user" . color('reset') . "       - " . N("specify user and password to use for proxy
                       authentication (format is <user:password>).") . "
" . color("bold cyan") . "  --bug" . color('reset') . "              - " . N("output a bug report in directory indicated by
                       next arg.") . "
" . color("bold cyan") . "  --env" . color('reset') . "              - " . N("use specific environment (typically a bug report).") . "
" . color("bold cyan") . "  --verify-rpm" . color('reset') . "       - " . N("verify rpm signature before installation
                       (--no-verify-rpm disables it, default is enabled).") . "
" . color("bold cyan") . "  --test" . color('reset') . "             - " . N("only verify if the installation can be achieved correctly.") . "
" . color("bold cyan") . "  --excludepath" . color('reset') . "      - " . N("exclude path separated by comma.") . "
" . color("bold cyan") . "  --excludedocs" . color('reset') . "      - " . N("exclude doc files.") . "
" . color("bold cyan") . "  --ignoresize" . color('reset') . "       - " . N("don't verify disk space before installation.") . "
" . color("bold cyan") . "  --ignorearch" . color('reset') . "       - " . N("allow to install rpms for unmatched architectures.") . "
" . color("bold cyan") . "  --noscripts" . color('reset') . "        - " . N("do not execute package scriptlet(s).") . "
" . color("bold cyan") . "  --replacefiles" . color('reset') . "     - " . N("ignore file conflicts.") . "
" . color("bold cyan") . "  --skip" . color('reset') . "             - " . N("packages which installation should be skipped.") . "
" . color("bold cyan") . "  --prefer" . color('reset') . "           - " . N("packages which should be preferred.") . "
" . color("bold cyan") . "  --more-choices" . color('reset') . "     - " . N("when several packages are found, propose more choices
                       than the default.") . "
" . color("bold cyan") . "  --nolock" . color('reset') . "           - " . N("don't lock rpm db.") . "
" . color("bold cyan") . "  --strict-arch" . color('reset') . "      - " . N("upgrade only packages with the same architecture.") . "
" . color("bold cyan") . "  -a" . color('reset') . "                 - " . N("select all matches on command line.") . "
" . color("bold cyan") . "  -p" . color('reset') . "                 - " . N("allow search in provides to find package.") . "
" . color("bold cyan") . "  -P" . color('reset') . "                 - " . N("do not search in provides to find package.") . "
" . color("bold cyan") . "  --quiet, -q" . color('reset') . "        - " . N("quiet mode.") . "
" . color("bold cyan") . "  --verbose, -v" . color('reset') . "      - " . N("verbose mode.") . "
" . color("bold cyan") . "  --debug" . color('reset') . "            - " . N("very verbose mode.") . "
" . "\n";
    print color('bright_yellow');
    print N("  names or rpm files given on command line will be installed.
");
    print color('reset');
    exit(1);
}

# Parse command line
my $command_line = join " ", @ARGV;
my @ARGVcopy; # keep a copy, in case we have to restart

# Expand *.urpmi arguments
if (member('--restricted', @ARGV)) {
    @ARGVcopy = @ARGV;
} else {
    foreach my $a (@ARGV) {
    if ($a =~ /\.urpmi$/) {
        open my $fh, '<', $a or do { warn "Can't open $a: $!\n"; next };
        push @ARGVcopy, map { chomp; $_ } <$fh>;
        close $fh;
    } else {
        push @ARGVcopy, $a;
    }
    }
    @ARGV = @ARGVcopy;
}

my $urpm = urpm->new_parse_cmdline or exit(1);

if (@ARGV && $auto_select) {
    print STDERR color('red') . N("Error: can't use --auto-select along with package list.\n") . color('reset');
    exit 1;
}

# Verify that arguments were given
unless (@ARGV || $auto_select || $clean) {
    if ($options{bug}) {
        print STDERR color('red') . N("Error: To generate a bug report, specify the usual command-line arguments
along with --bug.\n") . color('reset');;
        exit 1;
    }
    usage();
}

my @spec_files;
# Process the rest of the arguments
foreach (@ARGV) {
    if (/\.(?:rpm|spec)$/) {
    if (/\.src\.rpm$/) {
        push @src_files, $_;
    } elsif (/\.spec$/) {
        push @spec_files, $_;
    } else {
        push @files, untaint($_);
    }
    next;
    }
    push @names, $_;
}

if ($options{buildrequires}) {
    push @src_names, @names;
    @names = ();
}

#- use install_src to promote all names as src package.
if ($install_src) {
    @files and $urpm->{fatal}(1, N("You can't install binary rpm files when using --install-src"));
    @spec_files and $urpm->{fatal}(1, N("You can't install spec files"));
    push @src_names, @names;
    @names = ();
    #- allow to use --install-src as a non-root user
    $options{nolock} = 1;
} elsif (@spec_files) {
    if (!$options{buildrequires}) {
    $urpm->{error}(N("defaulting to --buildrequires"));
    $options{buildrequires} = 1;
    }
    push @src_files, @spec_files;
} elsif (@src_files && !$options{buildrequires}) {
    $urpm->{error}(N("please use --buildrequires or --install-src, defaulting to --buildrequires"));
    $options{buildrequires} = 1;
}

#- rurpmi checks
if ($restricted) {
    urpm::error_restricted($urpm) if @files;
    #- force some options
    foreach (qw(keep verify-rpm)) { $urpm->{options}{$_} = 1 }
    #- forbid some other options
    urpm::error_restricted($urpm) if $urpm->{root} || $options{usedistrib} || $force || $env || $parallel || $options{synthesis} || $auto_update || $options{auto_orphans};
    foreach (qw(allow-nodeps allow-force curl-options rsync-options wget-options prozilla-options noscripts)) {
    urpm::error_restricted($urpm) if $urpm->{options}{$_};
    }
}

#- prepare bug report.
my $bug = $options{bug};
if ($bug) {
    mkdir $bug or $urpm->{fatal}(8, (-d $bug
        ? N("Directory [%s] already exists, please use another directory for bug report or delete it", $bug)
        : N("Unable to create directory [%s] for bug report", $bug)));
    #- copy all synthesis file used, along with configuration of urpmi
    my @list_files = grep { -e $_ } $urpm->{skiplist}, $urpm->{instlist}, 
                                    $urpm->{prefer_list}, $urpm->{prefer_vendor_list}, '/root/.rpmdrake';
    system("cp", "-af", @list_files, $urpm->{config}, $bug)
    and die N("Copying failed");
    #- log everything for bug report.
    $logfile = "$bug/urpmi.log";
}

if ($env) {
    urpm::set_env($urpm, $env);
} else {
    if ($< != 0 && !$options{debug__do_not_install}) {
    #- need to be root if binary rpms are to be installed
    $auto_select || @names || @files and $urpm->{fatal}(1, N("Only superuser is allowed to install packages"));
    }
}

unless ($bug || $install_src || $env || $urpm->{options}{'allow-force'} || $urpm->{root}) {
    require urpm::sys;
    urpm::sys::check_fs_writable() or $urpm->{fatal}(1, N("Error: %s appears to be mounted read-only.
Use --allow-force to force operation.", $urpm::sys::mountpoint));
}

unless ($bug || $env || $test) {
    sys_log("called with: $command_line");
}

my ($pid_out, $pid_err);
if ($logfile && !$INC{"Devel/Trace.pm"}) {
    bug_log(scalar localtime(), " urpmi called with $command_line\n");

    open(my $SAVEOUT, ">&STDOUT"); select $SAVEOUT; $| = 1;
    open(my $SAVEERR, ">&STDERR"); select $SAVEERR; $| = 1;

    #- fork twice to copy stdout and stderr to $logfile
    unless ($pid_out = open STDOUT, "|-") {
    select $SAVEOUT; $| = 1;
    $/ = \1;
    binmode STDIN, ':raw'; #- since we read character by character, perl must not try to recognise utf8 strings since it really can't
    while (my $s = <STDIN>) {
        open my $fh, ">>$logfile";
        print $SAVEOUT $s;
        print $fh $s;
    }
    exit 0;
    }
    unless ($pid_err = open STDERR, "|-") {
    select $SAVEERR; $| = 1;
    $/ = \1;
    binmode STDIN, ':raw'; #- since we read character by character, perl must not try to recognise utf8 strings since it really can't
    while (my $s = <STDIN>) {
        open my $fh, ">>$logfile";
        print $SAVEERR $s;
        print $fh $s;
    }
    exit 0;
    }

    #- log to SAVEERR instead of STDERR
    unless ($bug) {
    $urpm->{fatal} = sub { printf $SAVEERR "%s\n", $_[1]; exit($_[0]) };
    $urpm->{error} = sub { printf $SAVEERR "%s\n", $_[0] };
    $urpm->{log}   = sub { printf $SAVEOUT "%s\n", $_[0] };
    }
}

#- make unbuffered
select STDERR; $| = 1;
select STDOUT; $| = 1;

if ($options{previous_priority_upgrade}) {
    # we were just restarted
    # so, no need to update the media again
    $auto_update = 0;
    # temporary hack : if we were using an old version of URPM (eg: when
    # upgrading from 2006), file handles might have leaked, so close them (with
    # some heuristics.)
    require urpm::sys;
    urpm::sys::fix_fd_leak();
    # also, clean up rpm db log files, because rpm might have been upgraded
    urpm::sys::clean_rpmdb_shared_regions('') if !$urpm->{root};
}

my $urpmi_lock = !$env && !$options{nolock} && urpm::lock::urpmi_db($urpm, '', wait => $options{wait_lock});

#- should we ignore arch compatibility
if ($urpm->{options}{ignorearch}) { urpm::shunt_ignorearch() }

if ($urpm->{root}) {
    $urpm->{options}{'priority-upgrade'} = '' if !$ENV{TESTING_priority_upgrade};
}
if ($auto_update && !$bug && !$env) {
    $urpm->{options}{'auto-update'} = 1;
}

urpm::media::configure($urpm,
    excludemedia => $excludemedia,
    media => $media,
    parallel => $parallel,
    searchmedia => $searchmedia,
    cmdline_skiplist => $options{skip},
    sortmedia => $sortmedia,
    synthesis => $options{synthesis},
    update => $update,
    usedistrib => $options{usedistrib},
    probe_with => $options{probe_with},
    download_callback => \&urpm::download::sync_logger,
    nomd5sum => $options{nomd5sum},
);

if ($bug) {
    require urpm::bug_report;
    urpm::bug_report::rpmdb_to_synthesis($urpm, "$bug/rpmdb.cz");
}

urpm::select::set_priority_upgrade_option($urpm, $options{previous_priority_upgrade});

my $state = {};
my %requested = $urpm->register_rpms(@files, @src_files);

#- finish bug environment creation.
if ($bug) {
    urpm::bug_report::write_urpmdb($urpm, $bug);
    urpm::bug_report::copy_requested($urpm, $bug, \%requested);
}

my $rpm_lock = !$env && !$options{nolock} && urpm::lock::rpm_db($urpm, 'exclusive', wait => $options{wait_lock});

#- search the packages according to the selection given by the user.
my $search_result = '';
if (@names) {
    $search_result = urpm::select::search_packages($urpm,
    \%requested, \@names,
    all => $all,
    use_provides => $use_provides,
    fuzzy => $urpm->{options}{fuzzy},
    no_substring => $urpm->{options}{auto}, # do not allow substring match if we can't prompt the user
    ) || $force or exit 1;

    if (%requested) {
        $urpm->{log}("found package(s): " . join(" ", map { scalar $urpm->{depslist}[$_]->fullname } 
                                                      map { split /\|/ } keys %requested));
    }
}
if (@src_names) {
    $search_result = urpm::select::search_packages($urpm, \%requested, \@src_names,
    all => $all,
    use_provides => $use_provides,
    fuzzy => $urpm->{options}{fuzzy},
    src => 1,
    ) || $force or exit 1;
}

# callback called to ask user to choose between virtual packages
# - $choices is the sorted list of choices
# - $prefered is a subset of @$choices (it can be empty)
sub ask_choice {
    my ($urpm, $_db, $_state, $choices, $virtual_pkg_name, $prefered) = @_;

    my @choices;
    if ($prefered && @$prefered) {
    @choices = @$choices;
    } else {
    ($prefered, my $other) = urpm::select::get_preferred($urpm, $choices, $options{prefer});
    @choices = (@$prefered, @$other);
    }
    my $prefer = @$prefered && join(',', grep { member($choices[$_-1], @$prefered) } 1 .. @choices);

    my (@l) = map {
    my ($name, $summary) = (scalar($_->fullname), translate($_->summary));
    $_->flag_installed ?
          ($_->summary ?
             #-PO: here format is "<package_name>: <summary> (to upgrade)"
             N("%s: %s (to upgrade)", $name, $summary) :
           #-PO: here format is "<package_name> (to upgrade)"
           N("%s (to upgrade)", $name)) :

             $_->flag_upgrade ? ($_->summary ?
                                   #-PO: here format is "<package_name>: <summary> (to install)"
                                   N("%s: %s (to install)", $name, $summary) :
                                 #-PO: here format is "<package_name> (to install)"
                                 N("%s (to install)", $name)) : $name;
    } @choices;

    my $n = 1; #- default value.

    if (@l > 1 && !$urpm->{options}{auto}) {
        print color('bright_yellow') . N("In order to satisfy the '%s' dependency, one of the following packages is needed:", $virtual_pkg_name) . color('reset'), "\n";
        my $i = 0;
        foreach (@l) { print " " . ++$i . "- $_\n" }
        $n = message_input(N("What is your choice? (1-%d) ", $i), default => $prefer, range_min => 0, range => $i);
    defined($n) && $n ne "0" or exit 1; # abort.
    if ($n =~ /\D/) {
        my @nn = map { $choices[$_ - 1] } grep { !/\D/ } split /[, \t]+/, $n;
        @nn or exit 1;
        return @nn;
    }
    }
    $choices[$n - 1];
}

#- do the resolution of dependencies between requested package (and auto selection if any).
#- handle parallel option if any.
#- return value is true if program should be restarted (in order to take care of important
#- packages being upgraded (problably urpmi and perl-URPM, but maybe rpm too, and glibc also ?).
my $restart_itself;
if ($options{replacepkgs}) {
    urpm::select::select_replacepkgs($urpm, $state, \%requested);
} else {
    $restart_itself = urpm::select::resolve_dependencies($urpm,
    $state,
    \%requested,
    rpmdb => $env && "$env/rpmdb.cz",
    auto_select => $auto_select,
    callback_choices => \&ask_choice,
    install_src => $install_src,
    keep => $urpm->{options}{keep},
    nodeps => $urpm->{options}{'allow-nodeps'} || $urpm->{options}{'allow-force'},
    no_recommends => $urpm->{options}{'no-recommends'},
    priority_upgrade => $test || $env ? '' : $urpm->{options}{'priority-upgrade'},
    );
}

{
    my $msg = urpm::select::translate_already_installed($state);
    $msg and print "$msg\n";
}

my @unselected_uninstalled = @{$state->{unselected_uninstalled} || []};
if (@unselected_uninstalled) {
    my $list = join "\n", map { $_->name . '-' . $_->version . '-' . $_->release  } @unselected_uninstalled;
    my $msg = @unselected_uninstalled == 1 ?
        N("The following package cannot be installed because it depends on packages
that are older than the installed ones:\n%s", $list)
    : N("The following packages can't be installed because they depend on packages
that are older than the installed ones:\n%s", $list);
    if ($urpm->{options}{auto}) {
    print "$msg\n";
    } else {
    my $noexpr = N("Nn");
    $msg .= N("\nContinue installation anyway?");
    $force || message_input($msg . N(" (Y/n) "), boolean => 1) !~ /[$noexpr]/ or exit 17;
    }
    # Whatever option we selected, the overall installation should fail if some packages are unselected
    $urpm::postponed_msg .= $msg . "\n";
    $urpm::postponed_code = 17;
}

my @ask_unselect = urpm::select::unselected_packages($state);
if (@ask_unselect) {
    my $list = urpm::select::translate_why_unselected($urpm, $state, @ask_unselect);
    my $msg = @ask_unselect == 1 ?
        N("A requested package cannot be installed:\n%s", $list)
    : N("Some requested packages cannot be installed:\n%s", $list);
    if ($urpm->{options}{auto}) {
    print "$msg\n";
    } else {
    my $noexpr = N("Nn");
    $msg .= N("\nContinue installation anyway?");
    $force || message_input($msg . N(" (Y/n) "), boolean => 1) !~ /[$noexpr]/ or exit 17;
    }
    # Whatever option we selected, the overall installation should fail if some packages are unselected
    $urpm::postponed_msg .= $msg . "\n";
    $urpm::postponed_code = 17;
}

if (my @conflicting_pkgs_msgs = 
      $urpm->{options}{'allow-force'} ? () : urpm::select::removed_packages_msgs($urpm, $state)) {
    {
    my $db = urpm::db_open_or_die_($urpm);
    urpm::select::find_removed_from_basesystem($urpm, $db, $state, sub {
        my ($urpm, @pkgs) = @_;
        foreach (@pkgs) {
        $urpm->{error}(N("removing package %s will break your system", $_));
        }
        @pkgs and $no_remove = 1;
    });
    }
    if ($no_remove && !$force) {
    my $list = join("\n", @conflicting_pkgs_msgs);
    my $msg = @conflicting_pkgs_msgs == 1 ?
    N("The installation cannot continue because the following package
has to be removed for others to be upgraded:\n%s\n", $list)
    : N("The installation cannot continue because the following packages
have to be removed for others to be upgraded:\n%s\n", $list);
    print "$msg\n";
    exit 17;
    }

    my $msg = urpm::select::conflicting_packages_msg_(\@conflicting_pkgs_msgs);
    if ($test) {
    $msg = "$msg\n" . N("(test only, removal will not be actually done)");
    }
    if ($urpm->{options}{auto}) {
    print "$msg\n";
    } else {
    $force || urpm::msg::ask_yes_or_no($msg) or exit 17;
    }
}

#- check if there is at least one package to install that
#- has not been given by the user.
my $ask_user = $env || $search_result eq 'substring';

my @to_install = @{$urpm->{depslist}}[sort { $a <=> $b } keys %{$state->{selected}}]; # sorted by medium for format_selected_packages
{
    my @binary = grep { $_->arch ne 'src' } @to_install;
    if ($install_src) {
    if (@binary && $install_src && !$env) {
        my $list = join(' ', sort map { $_->name } @binary);
        $urpm->{fatal}(1, N("You must first call urpmi with --buildrequires to install the following dependencies:\n%s\n", $list));
    }
    } else {
    @to_install = @binary;
    }
}

if (@to_install && $options{auto_orphans}) {
    urpm::orphans::compute_future_unrequested_orphans($urpm, $state);
    if (my @orphans = map { scalar $_->fullname } @{$state->{orphans_to_remove}}) {
    print P("The following orphan package will be removed.",
        "The following orphan packages will be removed.", scalar(@orphans))
      . "\n" . urpm::orphans::add_leading_spaces(join("\n", @orphans) . "\n");
    }
}

#- this cleans up the list of potential orphan packages:
#-     - if a package is explicitly requested on the command line, then
#-       we assume the user doesn't want this package to be auto orphaned
#-       so we remove it from installed-through-deps
#-     - this also takes care of removing packages from 
#-       installed-through-deps if the package was first installed as a 
#-       dep of another package, then removed and then explicitly installed
if (!$urpm->{options}{downgrade} && !$urpm->{options}{reinstall}) {
    urpm::orphans::mark_as_requested($urpm, $state, $test);
}

foreach my $pkg (@to_install) {
    #- reflect change in flag usage, now requested is set whatever a package is selected or not,
    #- but required is always set (so a required but not requested is a pure dependency).
    $ask_user ||= !$pkg->flag_requested || $auto_select || $parallel;
}
$urpm->{nb_install} = @to_install;

sub warn_msg {
    my ($msg) = @_;
    $urpm->{print}(color('red') . N("WARNING: %s option is in use. Some strange problems may happen", $msg) . color('reset'));
}

warn_msg("--allow-force")  if $urpm->{options}{'allow-force'};
warn_msg("--allow-nodeps") if $urpm->{options}{'allow-nodeps'};
warn_msg("--force") if $urpm->{options}{force};
warn_msg("--keep")  if $urpm->{options}{keep};

if (!$urpm->{options}{auto} && $ask_user && $urpm->{nb_install} || $env && !$options{debug__do_not_install}) {
    my $msg = $urpm->{nb_install} == 1 ? N("To satisfy dependencies, the following package is going to be installed:") 
        : N("To satisfy dependencies, the following packages are going to be installed:");
    if ($test) {
    $msg = "$msg\n" . N("(test only, installation will not be actually done)");
    }
    my ($size, $filesize) = $urpm->selected_size_filesize($state);

    my @to_install_formatted = urpm::msg::format_line_selected_packages($urpm, $state, \@to_install);
    my $msg2 = $size >= 0 ? 
      N("%s of additional disk space will be used.", formatXiB($size)) :
      N("%s of disk space will be freed.", formatXiB(-$size));
    my $msg2_ = $filesize ? N("%s of packages will be retrieved.", formatXiB($filesize)) . "\n" : '';
    my $msg3 = P("Proceed with the installation of one package?",
            "Proceed with the installation of the %d packages?",
        $urpm->{nb_install}, $urpm->{nb_install});
    my $p = join("\n", $msg, @to_install_formatted, $msg2, $msg2_ . $msg3);
    if ($env && !$options{debug__do_not_install}) {
    print "$p\n";
    exit 0; #- exit now for specific environment.
    }
    my $noexpr = N("Nn");
    $force || message_input($p . N(" (Y/n) "), boolean => 1) !~ /[$noexpr]/ or exit 17;
}

my $exit_code = urpm::main_loop::run($urpm, $state, 
                     int(@names || @src_names || @files || @src_files), 
                     \@ask_unselect, {
    (!$urpm->{options}{auto} || $allow_medium_change ? (copy_removable =>  sub {
    my $msg = N("Please insert the medium named \"%s\"", $_[0]);
    if (eval { require Hal::Cdroms; 1 }) {
        print "$msg\n";
        Hal::Cdroms->new->wait_for_insert;
        1;
    } else {
        my $msg2 = N("Press Enter when mounted...");
        defined message_input("$msg\n$msg2 ");
    }
    }) : ()),
    trans_log => \&urpm::download::sync_logger,
    bad_signature => sub {
        my ($msg, $msg2) = @_;
        #- rurpmi always abort here
        if ($urpm->{options}{auto} || $restricted) {
        print "$msg\n";
        0;
        } else {
        $force || urpm::msg::ask_yes_or_no("$msg$msg2");
        }
    },                           
    ask_yes_or_no => sub {
    my ($_title, $msg) = @_; # graphical title
    $force || urpm::msg::ask_yes_or_no($msg);
    },
    need_restart => sub {
    my ($need_restart_formatted) = @_;
    print "$_\n" foreach values %$need_restart_formatted;
    },
    message => sub {
    my ($_title, $msg) = @_; # graphical title
        print $msg;
    }
});

if ($exit_code == 0 && $auto_select && !$options{auto_orphans} && !$restart_itself) {
    if (urpm::orphans::check_unrequested_orphans_after_auto_select($urpm)) {
    if (my $msg = urpm::orphans::get_now_orphans_msg($urpm)) {
        print "\n", $msg;
    }
    }
}

unless ($env || $options{nolock}) {
    $urpmi_lock->unlock;
    $rpm_lock->unlock if $rpm_lock;
}

unless ($env) {
    #- try to umount removable device which may have been mounted.
    urpm::removable::try_umounting_removables($urpm);
}

# Merge postponed exit code to the result of package installation.
$exit_code ||= $urpm::postponed_code;

#- restart urpmi if needed, keep command line for that.
if ($restart_itself && !$exit_code) {
    print color('bright_yellow') . N("restarting urpmi"), "\n" . color('reset');
    #- it seems to work correctly with exec instead of system, provided
    #- STDOUT or STDERR are not closed before (else no output at all).
    #- added --previous-priority-upgrade to allow checking if yet if
    #-   priority-upgrade list has changed. and make sure we don't uselessly restart
    #- renamed bug report dir as /restarted to avoid exit because it already exists
    #- This permits to have in a same dir bug reports before and after the restart
    @ARGV = @ARGVcopy;
    my @arg = ($ARGV[0], map {
        $ARGV[$_] . ($ARGV[$_ - 1] eq '--bug' ? "/restarted" : "");
    } (1 .. $#ARGV));
    @arg = ('--previous-priority-upgrade=' . $urpm->{options}{'priority-upgrade'}, 
        grep { !/^--no-priority-upgrade$|--previous-priority-upgrade=/ } @arg);
    exec $0, @arg;
}

#- this help flushing correctly by closing this file before (piped on tee).
#- but killing them is generally better.
if ($pid_err || $pid_out) {
    kill 15, $pid_err, $pid_out;
    close STDERR;
    close STDOUT;
}

# Show postponed message before exiting
print $urpm::postponed_msg if $urpm::postponed_code != 0;

exit($exit_code);