package install_gtk; # $Id$ use diagnostics; use strict; use ugtk2 qw(:wrappers :helpers :create); use common; use lang; use devices; #-##################################################################################### #-INTERN CONSTANT #-##################################################################################### my @background; #- if we're running for the doc team, we want screenshots with #- a good B&W contrast: we'll override values of our theme my $theme_overriding_for_doc = q(style "galaxy-default" { base[SELECTED] = "#E0E0FF" base[ACTIVE] = "#E0E0FF" base[PRELIGHT] = "#E0E0FF" bg[SELECTED] = "#E0E0FF" bg[ACTIVE] = "#E0E0FF" bg[PRELIGHT] = "#E0E0FF" text[ACTIVE] = "#000000" text[PRELIGHT] = "#000000" text[SELECTED] = "#000000" fg[SELECTED] = "#000000" } style "white-on-blue" { base[NORMAL] = { 0.93, 0.93, 0.93 } bg[NORMAL] = { 0.93, 0.93, 0.93 } text[NORMAL] = "#000000" fg[NORMAL] = "#000000" } style "background" { bg[NORMAL] = { 0.93, 0.93, 0.93 } } style "background-logo" { bg[NORMAL] = { 0.70, 0.70, 0.70 } } widget "*logo*" style "background-logo" ); #------------------------------------------------------------------------------ sub load_rc { my ($o, $name) = @_; if (my $f = -r $name ? $name : find { -r $_ } map { "$_/themes-$name.rc" } ("share", $ENV{SHARE_PATH}, dirname(__FILE__))) { my @contents = cat_($f); $o->{doc} and push @contents, $theme_overriding_for_doc; Gtk2::Rc->parse_string(join("\n", @contents)); foreach (@contents) { if (/style\s+"background"/ .. /^\s*$/) { @background = map { $_ * 256 * 257 } split ',', $1 if /NORMAL.*\{(.*)\}/; } } } if ($::move) { #- override selection color since we won't do inverse-video on the text when it's images Gtk2::Rc->parse_string(q( style "galaxy-default" { base[ACTIVE] = "#CECECE" base[SELECTED] = "#CECECE" text[ACTIVE] = "#000000" text[PRELIGHT] = "#000000" text[SELECTED] = "#000000" } )); } } #------------------------------------------------------------------------------ sub load_font { my ($o) = @_; if (lang::text_direction_rtl()) { Gtk2::Widget->set_default_direction('rtl'); my ($x, $y) = $::WizardWindow->get_position; my ($width) = $::WizardWindow->get_size; $::WizardWindow->move($::rootwidth - $width - $x, $y); } Gtk2::Rc->parse_string(q( style "default-font" { font_name = ") . lang::l2pango_font($o->{locale}{lang}) . q(" } widget "*" style "default-font" )); } #------------------------------------------------------------------------------ sub default_theme { my ($o) = @_; $::move ? '/usr/share/themes/Galaxy/gtk-2.0/gtkrc' : $o->{meta_class} eq 'firewall' ? 'mdk-Firewall' : $o->{simple_themes} || $o->{vga16} ? 'blue' : 'galaxy'; } sub install_theme { my ($o) = @_; load_rc($o, $o->{theme} ||= default_theme($o)); load_font($o); $::move or gtkset_background(@background); } #------------------------------------------------------------------------------ sub create_help_window { my ($o) = @_; my $w; if ($w = $o->{help_window}) { $w->{window}->foreach(sub { $_[0]->destroy }, undef); } else { $w = $o->{help_window} = bless {}, 'ugtk2'; $w->{rwindow} = $w->{window} = Gtk2::Window->new('toplevel'); $w->{rwindow}->set_uposition($::rootwidth - $::helpwidth, $::rootheight - $::helpheight); $w->{rwindow}->set_size_request($::helpwidth, $::helpheight); $w->{rwindow}->set_title('skip'); }; gtkadd($w->{window}, create_scrolled_window($o->{help_window_text} = Gtk2::TextView->new)); $w->show; } #------------------------------------------------------------------------------ my %steps; sub create_steps_window { my ($o) = @_; return if $::stepswidth == 0; $o->{steps_window} and $o->{steps_window}->destroy; my $w = bless {}, 'ugtk2'; $w->{rwindow} = $w->{window} = Gtk2::Window->new('toplevel'); $w->{rwindow}->set_uposition(lang::text_direction_rtl() ? ($::rootwidth - $::stepswidth - 8) : 8, 150); $w->{rwindow}->set_size_request($::stepswidth, -1); $w->{rwindow}->set_name('Steps'); $w->{rwindow}->set_title('skip'); $steps{$_} ||= gtkcreate_pixbuf("steps_$_") foreach qw(on off); my $category = sub { gtkset_markup(Gtk2::Label->new, $o->{doc} ? $_[0] : '' . $_[0] . '') }; gtkpack__(my $vb = Gtk2::VBox->new(0, 3), $steps{inst} = $category->(N("System installation")), ''); foreach (grep { !eval $o->{steps}{$_}{hidden} } @{$o->{orderedSteps}}) { $_ eq 'setRootPassword' and gtkpack__($vb, '', '', $steps{conf} = $category->(N("System configuration")), ''); $steps{steps}{$_} = { img => gtkcreate_img('steps_off.png'), txt => Gtk2::Label->new(translate($o->{steps}{$_}{text})) }; gtkpack__($vb, gtkpack__(Gtk2::HBox->new(0, 7), $steps{steps}{$_}{img}, $steps{steps}{$_}{txt})); } gtkadd($w->{window}, $vb); $w->show; $o->{steps_window} = $w; } sub update_steps_position { my ($o) = @_; return if !$steps{steps}; my $last_step; foreach (@{$o->{orderedSteps}}) { exists $steps{steps}{$_} or next; if ($o->{steps}{$_}{entered} && !$o->{steps}{$_}{done}) { $steps{steps}{$_}{img}->set_from_pixbuf($steps{on}); $last_step and $steps{steps}{$last_step}{img}->set_from_pixbuf($steps{off}); return; } $last_step = $_; } } #------------------------------------------------------------------------------ sub create_logo_window { my ($o) = @_; return if $::logowidth == 0 || $::move; gtkdestroy($o->{logo_window}); my $w = bless {}, 'ugtk2'; $w->{rwindow} = $w->{window} = Gtk2::Window->new('toplevel'); # $w->{rwindow}->set_position(0, 0); $w->{rwindow}->set_size_request($::logowidth, $::logoheight); $w->{rwindow}->set_name("logo"); $w->{rwindow}->set_title('skip'); $w->show; my $file = $o->{meta_class} eq 'firewall' ? "logo-mandrake-Firewall.png" : "logo-mandrake.png"; -r $file or $file = "$ENV{SHARE_PATH}/$file"; -r $file and gtkadd($w->{window}, gtkcreate_img($file)); $o->{logo_window} = $w; } #------------------------------------------------------------------------------ sub init_gtk { my ($o) = @_; symlink("/tmp/stage2/etc/$_", "/etc/$_") foreach qw(gtk-2.0 pango fonts); if ($o->{vga16}) { #- inactivate antialias in VGA16 because it makes fonts look worse output('/tmp/fonts.conf', q( /etc/fonts/fonts.conf false )); $ENV{FONTCONFIG_FILE} = '/tmp/fonts.conf'; } Gtk2->init; Gtk2->set_locale; } #------------------------------------------------------------------------------ sub init_sizes() { ($::rootwidth, $::rootheight) = (Gtk2::Gdk->screen_width, Gtk2::Gdk->screen_height); #- ($::rootheight, $::rootwidth) = (min(768, $::rootheight), min(1024, $::rootwidth)); $::move or $::stepswidth = $::rootwidth <= 640 ? 0 : 160; ($::logowidth, $::logoheight) = $::rootwidth <= 640 ? (0, 0) : (500, 40); ($::helpwidth, $::helpheight) = ($::rootwidth - $::stepswidth, $::move && 15); ($::windowwidth, $::windowheight) = ($::rootwidth - $::stepswidth, $::rootheight - $::helpheight - $::logoheight); ($::real_windowwidth, $::real_windowheight) = (576, 418); $::move and $::windowwidth -= 100; } #------------------------------------------------------------------------------ sub createXconf { my ($file, $mouse_type, $mouse_dev, $_wacom_dev, $Driver) = @_; symlinkf(devices::make($mouse_dev), "/dev/mouse") if $mouse_dev ne 'none'; #- needed for imlib to start on 8-bit depth visual. symlink("/tmp/stage2/etc/imrc", "/etc/imrc"); symlink("/tmp/stage2/etc/im_palette.pal", "/etc/im_palette.pal"); #- remove "error opening security policy file" warning symlink("/tmp/stage2/etc/X11", "/etc/X11"); if ($Driver) { output($file, sprintf(<<'END', $mouse_type, $Driver, $Driver eq 'fbdev' ? '"default"' : '"800x600" "640x480"')); Section "Files" FontPath "/usr/X11R6/lib/X11/fonts:unscaled" EndSection Section "InputDevice" Identifier "Keyboard" Driver "Keyboard" Option "XkbDisable" Option "XkbModel" "pc105" Option "XkbLayout" "" EndSection Section "InputDevice" Identifier "Mouse" Driver "mouse" Option "Protocol" "%s" Option "Device" "/dev/mouse" Option "ZAxisMapping" "4 5" EndSection Section "Monitor" Identifier "monitor" HorizSync 31.5-35.5 VertRefresh 50-70 EndSection Section "Device" Identifier "device" Driver "%s" EndSection Section "Screen" Identifier "screen" Device "device" Monitor "monitor" DefaultColorDepth 16 Subsection "Display" Depth 16 Modes %s EndSubsection EndSection Section "ServerLayout" Identifier "layout" Screen "screen" InputDevice "Mouse" "CorePointer" InputDevice "Keyboard" "CoreKeyboard" EndSection END } } 1; 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
package urpm::args;

# $Id$

use strict;
use warnings;
no warnings 'once';
use Getopt::Long;# 2.33;
use urpm::download;
use urpm::msg;
use Exporter;

our @ISA = 'Exporter';
our @EXPORT = '%options';

(our $VERSION) = q($Revision$) =~ /(\d+)/;

# The program that invokes us
(my $tool = $0) =~ s!.*/!!;

# Configuration of Getopt. urpmf is a special case, because we need to
# parse non-alphanumerical options (-! -( -))
my @configuration = qw(bundling gnu_compat permute);
push @configuration, 'pass_through'
    if $tool eq 'urpmf' || $tool eq 'urpmi.addmedia';
Getopt::Long::Configure(@configuration);

# global urpm object to be passed by the main program
my $urpm;

# stores the values of the command-line options
our %options = (verbose => 0);

# used by urpmf
sub add_param_closure {
    my (@tags) = @_;
    return sub { $::qf .= join $::separator, '', map { "%$_" } @tags };
}

# debug code to display a nice message when exiting, 
# to ensure f*cking code (eg: Sys::Syslog) won't exit and break graphical interfaces
END { $::debug_exit and print STDERR "EXITING (pid=$$)\n" }

# options specifications for Getopt::Long

my %options_spec_all = (
	'debug' => sub { 
	    $::debug_exit = 1; 
	    $options{verbose}++;
	    $urpm->{debug} = $urpm->{debug_URPM} = sub { print STDERR "$_[0]\n" };
	},
	'q|quiet' => sub { --$options{verbose} },
	'v|verbose' => sub { ++$options{verbose} },
	'urpmi-root=s' => sub { urpm::set_files($urpm, $_[1]) },
	'wait-lock' => \$options{wait_lock},
	'use-copied-hdlist' => sub { $urpm->{options}{use_copied_hdlist} = 1 },
	'tune-rpm=s' => sub { urpm::set_tune_rpm($urpm, $_[1]) },
);

my %options_spec = (

    # warning: for gurpm, urpm is _not_ a real urpmi object, only options should be altered:
    gurpmi => {
	'media|mediums=s' => sub { $urpm->{options}{media} = 1 },
	"help|h" => sub { gurpmi::usage() },
	'searchmedia|search-media=s' => sub { $urpm->{options}{searchmedia} = 1 },
    },

    urpmi => {
	"version" => sub { require urpm; print "$tool $urpm::VERSION\n"; exit(0) },
	"help|h" => sub {
	    if (defined &::usage) { ::usage() } else { die "No help defined\n" }
	},
	"no-locales" => sub { $urpm::msg::no_translation = 1 },
	update => \$::update,
	'media|mediums=s' => \$::media,
	'excludemedia|exclude-media=s' => \$::excludemedia,
	'sortmedia|sort-media=s' => \$::sortmedia,
	'searchmedia|search-media=s' => \$::searchmedia,
	'synthesis=s' => \$options{synthesis},
	auto => sub { $urpm->{options}{auto} =  1 },
	'allow-medium-change' => \$::allow_medium_change,
	'gui' => \$::gui,
	'auto-select' => \$::auto_select,
	'auto-update' => sub { $::auto_update = $::auto_select = 1 },
	'no-remove|no-uninstall' => \$::no_remove,
	'no-install|noinstall' => \$::no_install,
	'keep!' => sub { $urpm->{options}{keep} = $_[1] },
	'logfile=s' => \$::logfile,
	'split-level=s' => sub { $urpm->{options}{'split-level'} = $_[1] },
	'split-length=s' => sub { $urpm->{options}{'split-length'} = $_[1] },
	'fuzzy!' => sub { $urpm->{options}{fuzzy} = $_[1] },
	'src|s' => sub { $urpm->{error}("option --src is deprecated, use --buildrequires instead (nb: it doesn't download src.rpm anymore)");
			 $options{buildrequires} = 1 },
	'buildrequires' => \$options{buildrequires},
	'install-src' => \$::install_src,
	clean => sub { $::clean = 1; $::noclean = 0 },
	noclean => sub {
	    $::clean = $urpm->{options}{'pre-clean'} = $urpm->{options}{'post-clean'} = 0;
	    $::noclean = 1;
	},
	'pre-clean!' => sub { $urpm->{options}{'pre-clean'} = $_[1] },
	'post-clean!' => sub { $urpm->{options}{'post-clean'} = $_[1] },
	'no-priority-upgrade' => sub {
	    #- keep this option which is passed by older urpmi.
	    #- since we can't know what the previous_priority_upgrade list was, 
	    #- just use a rubbish value which will mean list has changed
	    $options{previous_priority_upgrade} = 'list_has_changed';
	},
	'previous-priority-upgrade=s' => \$options{previous_priority_upgrade},
	force => \$::force,
	justdb => \$options{justdb},
	replacepkgs => \$options{replacepkgs},
	'suggests!' => sub { $urpm->{options}{'no-suggests'} = !$_[1] },
	'allow-nodeps' => sub { $urpm->{options}{'allow-nodeps'} = 1 },
	'allow-force' => sub { $urpm->{options}{'allow-force'} = 1 },
	'parallel=s' => \$::parallel,
	
	# deprecated in favor of --downloader xxx
	wget => sub { $urpm->{options}{downloader} = 'wget' },
	curl => sub { $urpm->{options}{downloader} = 'curl' },
	prozilla => sub { $urpm->{options}{downloader} = 'prozilla' },
	aria2 => sub { $urpm->{options}{downloader} = 'aria2' },
	'downloader=s' => sub { $urpm->{options}{downloader} = $_[1] },

	'curl-options=s' => sub { $urpm->{options}{'curl-options'} = $_[1] },
	'rsync-options=s' => sub { $urpm->{options}{'rsync-options'} = $_[1] },
	'wget-options=s' => sub { $urpm->{options}{'wget-options'} = $_[1] },
	'prozilla-options=s' => sub { $urpm->{options}{'prozilla-options'} = $_[1] },
	'aria2-options=s' => sub { $urpm->{options}{'aria2-options'} = $_[1] },
	'limit-rate=s' => sub { $urpm->{options}{'limit-rate'} = $_[1] },
	'resume!' => sub { $urpm->{options}{resume} = $_[1] },
	'retry=s' => sub { $urpm->{options}{retry} = $_[1] },
	'proxy=s' => sub {
	    my (undef, $value) = @_;
	    my ($proxy, $port) = $value =~ m,^(?:http://)?([^:/]+(:\d+)?)/*$,
		or die N("bad proxy declaration on command line\n");
	    $proxy .= ":1080" unless $port;
	    urpm::download::set_cmdline_proxy(http_proxy => "http://$proxy/");
	},
	'proxy-user=s' => sub {
	    my (undef, $value) = @_;
	    if ($value eq 'ask') { #- should prompt for user/password
		urpm::download::set_cmdline_proxy(ask => 1);
	    } else {
		$value =~ /(.+):(.+)/ or die N("bad proxy declaration on command line\n");
		urpm::download::set_cmdline_proxy(user => $1, pwd => $2);
	    }
	},
	'bug=s' => \$options{bug},
	'env=s' => \$::env,
	'verify-rpm!' => sub { $urpm->{options}{'verify-rpm'} = $_[1] },
	'strict-arch!' => sub { $urpm->{options}{'strict-arch'} = $_[1] },
	'norebuild!' => sub { $urpm->{options}{'build-hdlist-on-error'} = !$_[1] },
	'test!' => \$::test,
	'debug__do_not_install' => \$options{debug__do_not_install},
	'skip=s' => \$options{skip},
	'prefer=s' => \$options{prefer},
 	'root=s' => sub { set_root($urpm, $_[1]) },
	'use-distrib=s' => \$options{usedistrib},
	'probe-synthesis' => sub { $options{probe_with} = 'synthesis' },
	'probe-hdlist' => sub { $options{probe_with} = 'synthesis' }, #- ignored, kept for compatibility
	'excludepath|exclude-path=s' => sub { $urpm->{options}{excludepath} = $_[1] },
	'excludedocs|exclude-docs' => sub { $urpm->{options}{excludedocs} = 1 },
	'ignoresize' => sub { $urpm->{options}{ignoresize} = 1 },
	'ignorearch' => sub { $urpm->{options}{ignorearch} = 1 },
	noscripts => sub { $urpm->{options}{noscripts} = 1 },
	repackage => sub { $urpm->{options}{repackage} = 1 },
	'more-choices' => sub { $urpm->{options}{morechoices} = 1 },
	'expect-install!' => \$::urpm::main_loop::expect_install,
	'nolock' => \$options{nolock},
	restricted => \$::restricted,
	'no-md5sum' => \$::nomd5sum,
	'force-key' => \$::forcekey,
	a => \$::all,
	p => sub { $::use_provides = 1 },
	P => sub { $::use_provides = 0 },
	y => sub { $urpm->{options}{fuzzy} = 1 },
	z => sub { $urpm->{options}{compress} = 1 },
    },

    urpme => {
	auto => \$::auto,
	a => \$::matches,
	noscripts => \$::noscripts,
	repackage => \$::repackage,
	restricted => \$::restricted,
    },

    #- see also below, autogenerated callbacks
    urpmf => {
	conffiles => add_param_closure('conf_files'),
	debug => \$::debug,
	'literal|l' => \$::literal,
	name => sub {
	    add_param_closure('name')->();
	    #- Remove default tag in front if --name is explicitly given
	    $::qf =~ s/^%default:?//;
	},
	'qf=s' => \$::qf,
	'uniq|u' => \$::uniq,
	m => add_param_closure('media'),
	i => sub { $::pattern = 'i' },
	I => sub { $::pattern = '' },
	f => sub { $::full = 1 },
	'F=s' => sub { $::separator = $_[1] },
	'e=s' => sub { $::expr .= "($_[1])" },
	a => sub { $::expr .= ' && ' },
	o => sub { $::expr .= ' || ' },
	'<>' => sub {
	    my $p = shift;
	    if ($p =~ /^-?([!()])$/) {
		# This is for -! -( -)
		$::expr .= $1;
	    }
	    elsif ($p =~ /^--?(.+)/) {
		# unrecognized option
		die "Unknown option: $1\n";
	    }
	    else {
		# This is for non-option arguments.
		add_urpmf_parameter($p);
	    }
	},
    },

    urpmq => {
	update => \$options{update},
	'media|mediums=s' => \$options{media},
	'excludemedia|exclude-media=s' => \$options{excludemedia},
	'sortmedia|sort-media=s' => \$options{sortmedia},
	'searchmedia|search-media=s' => \$options{searchmedia},
	'auto-select' => sub {
	    $options{deps} = $options{upgrade} = $options{auto_select} = 1;
	},
	fuzzy => sub { $urpm->{options}{fuzzy} = 1; $options{all} = 1 },
	keep => \$options{keep},
	list => \$options{list},
	changelog => \$options{changelog},
	conflicts => \$options{conflicts},
	provides => \$options{provides},
	sourcerpm => \$options{sourcerpm},
	'summary|S' => \$options{summary},
	'list-media:s' => sub { $options{list_media} = $_[1] || 'all' },
	'list-url' => \$options{list_url},
	'list-nodes' => \$options{list_nodes},
	'list-aliases' => \$options{list_aliases},
	'ignorearch' => \$options{ignorearch},
	'dump-config' => \$options{dump_config},
	'src|s' => \$options{src},
	sources => \$options{sources},
	force => \$options{force},
	'parallel=s' => \$options{parallel},
	'env=s' => \$options{env},
	requires => sub {
	    $urpm->{error}("--requires behaviour changed, use --requires-recursive to get the old behaviour");
	    $options{requires} = 1;
	},
	'requires-recursive|d' => \$options{deps},
	u => \$options{upgrade},
	a => \$options{all},
	'm|M' => sub { $options{deps} = $options{upgrade} = 1 },
	c => \$options{complete},
	g => \$options{group},
	'whatprovides|p' => \$options{use_provides},
	P => sub { $options{use_provides} = 0 },
	R => sub { $urpm->{error}($options{what_requires} ?
				    "option -RR is deprecated, use --whatrequires-recursive instead" : 
				    "option -R is deprecated, use --whatrequires instead");
	           $options{what_requires} and $options{what_requires_recursive} = 1; 
		   $options{what_requires} = 1 },
	whatrequires => sub { $options{what_requires} = 1 },
	'whatrequires-recursive' => sub { $options{what_requires_recursive} = $options{what_requires} = 1 },
	y => sub { $urpm->{options}{fuzzy} = 1; $options{all} = 1 },
	Y => sub { $urpm->{options}{fuzzy} = 1; $options{all} = $options{caseinsensitive} = 1 },
	i => \$options{info},
	l => \$options{files},
	r => sub {
	    $options{version} = $options{release} = 1;
	},
	f => sub {
	    $options{version} = $options{release} = $options{arch} = 1;
	},
	'<>' => sub {
	    my $x = $_[0];
	    if ($x =~ /\.rpm$/) {
		if (-r $x) { push @::files, $x }
		else { print STDERR N("urpmq: cannot read rpm file \"%s\"\n", $x) }
	    } elsif ($x =~ /^--?(.+)/) { # unrecognized option
		die "Unknown option: $1\n";
	    } else {
		if ($options{src}) {
		    push @::src_names, $x;
		} else {
		    push @::names, $x;
		}
		$options{src} = 0; #- reset switch for next package.
	    }
	},
    },

    'urpmi.update' => {
	a => \$options{all},
	c => sub {}, # obsolete
	f => sub { ++$options{force}; $options{probe_with} = 'rpms' if $options{force} == 2 },
	z => sub { ++$options{compress} },
	update => \$options{update},
	'ignore!' => sub { $options{ignore} = $_[1] },
	'force-key' => \$options{forcekey},
	'no-md5sum' => \$options{nomd5sum},
	'noa|d' => \my $_dummy, #- default, kept for compatibility
	'norebuild!' => sub { $urpm->{options}{'build-hdlist-on-error'} = !$_[1]; $options{force} = 0 },
	'probe-rpms' => sub { $options{probe_with} = 'rpms' },
	'<>' => sub {
	    my ($p) = @_;
	    if ($p =~ /^--?(.+)/) { # unrecognized option
		die "Unknown option: $1\n";
	    }
	    push @::cmdline, $p;
	},
    },

    'urpmi.addmedia' => {
	'xml-info=s' => \$options{'xml-info'},
	'no-probe' => sub { $options{probe_with} = undef },
	distrib => sub { $options{distrib} = 1 },
	'mirrorlist=s' => \$options{mirrorlist},
        interactive => sub { $options{interactive} = 1 },
        'all-media' => sub { $options{allmedia} = 1 },
	'from=s' => \$options{mirrors_url},
	virtual => \$options{virtual},
	nopubkey => \$options{nopubkey},
	raw => \$options{raw},
    },

    'urpmi.recover' => {
	'list=s' => \$::listdate,
	'list-all' => sub { $::listdate = -1 },
	'list-safe' => sub { $::listdate = 'checkpoint' },
	checkpoint => \$::do_checkpoint,
	'rollback=s' => \$::rollback,
	noclean => \$::noclean,
	disable => \$::disable,
    },

);



# generate urpmf options callbacks
sub add_urpmf_cmdline_tags {
    foreach my $k (@_) {
	$options_spec{urpmf}{$k} ||= add_param_closure($k);
    }
}

sub add_urpmf_parameter {
    my ($p) = @_;

    if ($::literal) {
	$p = quotemeta $p;
    } else {
	$p =~ /\([^?|]*\)$/ and $urpm->{error}(N("by default urpmf awaits a regexp. you should use option \"--literal\"")); 
	push @::raw_non_literals, $p;
	# quote "+" chars for packages with + in their names
	$p =~ s/\+/\\+/g;
    }
    $::expr .= ($::expr ? ' || ' : '') . "m{$p}" . $::pattern;
}

# common options setup

foreach my $k ('allow-medium-change', 'auto', 'auto-select', 'expect-install!', 'no-priority-upgrade', 'previous-priority-upgrade=s', 'root=s', 'test!', 'verify-rpm!')
{
    $options_spec{gurpmi}{$k} = $options_spec{urpmi}{$k};
}
$options_spec{gurpmi2} = $options_spec{gurpmi};

foreach my $k ("help|h", "version", "no-locales", "test!", "force", "root=s", "use-distrib=s",
    "parallel=s")
{
    $options_spec{urpme}{$k} = $options_spec{urpmi}{$k};
}
foreach my $k ("root=s", "nolock", "use-distrib=s", "skip=s", "prefer=s", "synthesis=s", 'suggests!')
{
    $options_spec{urpmq}{$k} = $options_spec{urpmi}{$k};
}

foreach my $k ("help|h", "version", "no-locales", "update", "media|mediums=s",
    "excludemedia|exclude-media=s", "sortmedia|sort-media=s", "use-distrib=s",
    "synthesis=s", "env=s")
{
    $options_spec{urpmf}{$k} = $options_spec{urpmi}{$k};
}

foreach my $k ("help|h", "version", "wget", "curl", "prozilla", "aria2", 'downloader=s', "proxy=s", "proxy-user=s",
    'limit-rate=s',
    "wget-options=s", "curl-options=s", "rsync-options=s", "prozilla-options=s", "aria2-options=s")
{
    $options_spec{'urpmi.update'}{$k} =
    $options_spec{urpmq}{$k} = $options_spec{urpmi}{$k};
}

foreach my $k ("help|h", "wget", "curl", "prozilla", "aria2", 'downloader=s', "proxy=s", "proxy-user=s", "f", "z",
    "limit-rate=s", "no-md5sum", "update", "norebuild!", "probe-rpms",
    "wget-options=s", "curl-options=s", "rsync-options=s", "prozilla-options=s", "aria2-options=s", '<>')
{
    $options_spec{'urpmi.addmedia'}{$k} = $options_spec{'urpmi.update'}{$k};
}

foreach my $k ("a", '<>') {
    $options_spec{'urpmi.removemedia'}{$k} = $options_spec{'urpmi.update'}{$k};
}
foreach my $k ("y") {
    $options_spec{'urpmi.removemedia'}{$k} = $options_spec{urpmi}{$k};
}

foreach my $k ("probe-synthesis", "probe-hdlist") # probe-hdlist is obsolete
{
    $options_spec{'urpmi.addmedia'}{$k} = 
      $options_spec{urpme}{$k} = 
	$options_spec{urpmq}{$k} = $options_spec{urpmi}{$k};
}

foreach my $k ("help|h", "version") {
    $options_spec{'urpmi.removemedia'}{$k} = 
      $options_spec{'urpmi.recover'}{$k} = $options_spec{urpmi}{$k};
}

sub set_root {
    my ($urpm, $root) = @_;
	    require File::Spec;
	    $urpm->{root} = File::Spec->rel2abs($root);
	    if (!-d $urpm->{root}) {
		$urpm->{fatal}->(9, N("chroot directory doesn't exist"));
	    }
}

sub parse_cmdline {
    my %args = @_;
    $urpm = $args{urpm};
    foreach my $k (keys %{$args{defaults} || {}}) {
	$options{$k} = $args{defaults}{$k};
    }
    my $ret = GetOptions(%{$options_spec{$tool}}, %options_spec_all);

    $options{verbose} >= 0 or $urpm->{info} = sub {};
    $options{verbose} > 0 or $urpm->{log} = sub {};

    $urpm->{tune_rpm} and urpm::tune_rpm($urpm);

    if ($tool ne 'urpmi.addmedia' && $tool ne 'urpmi.update' &&
	  $options{probe_with} && !$options{usedistrib}) {
	die N("Can't use %s without %s", "--probe-$options{probe_with}", "--use-distrib");
    }
    if ($options{probe_with} && $options{probe_with} eq 'rpms' && $options{virtual}) {
	die N("Can't use %s with %s", "--probe-rpms", "--virtual");
    }
    if ($options{nolock} && $options{wait_lock}) {
	warn N("Can't use %s with %s", "--wait-lock", "--nolock") . "\n";
    }
    if ($tool eq 'urpmf' && @ARGV && $ARGV[0] eq '--') {
	if (@ARGV == 2) {
	    add_urpmf_parameter($ARGV[1]);
            $ret = 1;
	}
	else {
	    die N("Too many arguments\n");
	}
    }
    $ret;
}

1;

__END__

=head1 NAME

urpm::args - command-line argument parser for the urpm* tools

=head1 SYNOPSIS

    urpm::args::parse_cmdline();

=head1 DESCRIPTION

=head1 COPYRIGHT

Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005 MandrakeSoft SA

Copyright (C) 2005, 2006 Mandriva SA

=cut