package interactive; # $Id$

use diagnostics;
use strict;

#-######################################################################################
#- misc imports
#-######################################################################################
use MDK::Common::Func;
use common;

#- minimal example using interactive:
#
#- > use lib qw(/usr/lib/libDrakX);
#- > use interactive;
#- > my $in = interactive->vnew;
#- > $in->ask_okcancel('title', 'question');
#- > $in->exit;

#- ask_from_entries takes:
#-  val      => reference to the value
#-  label    => description
#-  icon     => icon to put before the description
#-  help     => tooltip
#-  advanced => wether it is shown in by default or only in advanced mode
#-  disabled => function returning wether it should be disabled (grayed)
#-  gtk      => gtk preferences
#-  type     => 
#-     button => (with clicked or clicked_may_quit)
#-               (type defaults to button if clicked or clicked_may_quit is there)
#-               (val need not be a reference) (if clicked_may_quit return true, it's as if "Ok" was pressed)
#-     label => (val need not be a reference) (type defaults to label if val is not a reference) 
#-     bool (with text)
#-     range (with min, max)
#-     combo (with list, not_edit, format)
#-     list (with list, icon2f (aka icon), separator (aka tree), format (aka pre_format function),
#-           help can be a hash or a function,
#-           tree_expanded boolean telling wether the tree should be wide open by default
#-           quit_if_double_click boolean
#-           allow_empty_list disables the special cases for 0 and 1 element lists)
#-     entry (the default) (with hidden)
#
#- heritate from this class and you'll get all made interactivity for same steps.
#- for this you need to provide
#- - ask_from_listW(o, title, messages, arrayref, default) returns one string of arrayref
#-
#- where
#- - o is the object
#- - title is a string
#- - messages is an refarray of strings
#- - default is an optional string (default is in arrayref)
#- - arrayref is an arrayref of strings
#- - arrayref2 contains booleans telling the default state,
#-
#- ask_from_list and ask_from_list_ are wrappers around ask_from_biglist and ask_from_smalllist
#-
#- ask_from_list_ just translate arrayref before calling ask_from_list and untranslate the result
#-
#- ask_from_listW should handle differently small lists and big ones.
#-


#-######################################################################################
#- OO Stuff
#-######################################################################################
sub new($) {
    my ($type) = @_;

    bless {}, ref $type || $type;
}

sub vnew {
    my ($type, $su, $icon) = @_;
    $su = $su eq "su";
    if ($ENV{INTERACTIVE_HTTP}) {
	require interactive_http;
	return interactive_http->new;
    }
    require c;
    if ($su) {
	$ENV{PATH} = "/sbin:/usr/sbin:$ENV{PATH}";
	$su = '' if $::testing || $ENV{TESTING};
    }
    if ($ENV{DISPLAY} && system('/usr/X11R6/bin/xtest') == 0) {
	if ($su && $>) {
	    if (`/sbin/pidof "kdeinit: kwin"` > 0) {
		exec("kdesu", "-c", "$0 @ARGV") or die _("kdesu missing");
	    } else {
		exec {'consolehelper'} $0, @ARGV or die _("consolehelper missing");
	    }
	}
	eval { require interactive_gtk };
	if (!$@) {
	    my $o = interactive_gtk->new;
	    if ($icon && $icon ne 'default' && !$::isWizard) { $o->{icon} = $icon } else { undef $o->{icon} }
	    return $o;
	}
    } else {
	if ($su && $>) {
	    exec {'consolehelper'} $0, @ARGV or die _("consolehelper missing");
	}
    }

    if ($su && $>) {
	die "you must be root to run this program";
    }
    require 'log.pm';
    undef *log::l;
    *log::l = sub {}; # otherwise, it will bother us :(
    require interactive_newt;
    interactive_newt->new;
}

sub enter_console {}
sub leave_console {}
sub suspend {}
sub resume {}
sub end {}
sub exit { exit($_[0]) }

#-######################################################################################
#- Interactive functions
#-######################################################################################
sub ask_warn {
    my ($o, $title, $message) = @_;
    local $::isWizard=0;
    ask_from_listf_no_check($o, $title, $message, undef, [ _("Ok") ]);
}

sub ask_yesorno {
    my ($o, $title, $message, $def, $help) = @_;
    ask_from_list_($o, $title, $message, [ __("Yes"), __("No") ], $def ? "Yes" : "No", $help) eq "Yes";
}

sub ask_okcancel {
    my ($o, $title, $message, $def, $help) = @_;

    if ($::isWizard) {
	$::no_separator = 1;
    	$o->ask_from_no_check({ title => $title, messages => $message, focus_cancel => !$def });
    } else {
	ask_from_list_($o, $title, $message, [ __("Ok"), __("Cancel") ], $def ? "Ok" : "Cancel", $help) eq "Ok";
    }
}

sub ask_file {
    my ($o, $title, $dir) = @_;
    $o->ask_fileW($title, $dir);
}
sub ask_fileW {
    my ($o, $title, $dir) = @_;
    $o->ask_from_entry($title, _("Choose a file"));
}

sub ask_from_list {
    my ($o, $title, $message, $l, $def, $help) = @_;
    ask_from_listf($o, $title, $message, undef, $l, $def, $help);
}

sub ask_from_list_ {
    my ($o, $title, $message, $l, $def, $help) = @_;
    ask_from_listf($o, $title, $message, sub { translate($_[0]) }, $l, $def, $help);
}

sub ask_from_listf_ {
    my ($o, $title, $message, $f, $l, $def, $help) = @_;
    ask_from_listf($o, $title, $message, sub { translate($f->(@_)) }, $l, $def, $help);
}
sub ask_from_listf {
    my ($o, $title, $message, $f, $l, $def, $help) = @_;
    @$l == 0 and die "ask_from_list: empty list\n" . common::backtrace();
    @$l == 1 and return $l->[0];
    goto &ask_from_listf_no_check;
}

sub ask_from_listf_no_check {
    my ($o, $title, $message, $f, $l, $def, $help) = @_;

    if (@$l <= 2 && !$::isWizard) {
	my $ret = eval {
	    ask_from_no_check($o, 
	      { title => $title, messages => $message, ok => $l->[0] && may_apply($f, $l->[0]), 
		if_($l->[1], cancel => may_apply($f, $l->[1]), focus_cancel => $def eq $l->[1]) }, []
            ) ? $l->[0] : $l->[1];
	};
	die if $@ && $@ !~ /^wizcancel/;
	$@ ? undef : $ret;
    } else {
	ask_from($o, $title, $message, [ { val => \$def, type => 'list', list => $l, help => $help, format => $f } ]) && $def;
    }
}

sub ask_from_treelist {
    my ($o, $title, $message, $separator, $l, $def) = @_;
    ask_from_treelistf($o, $title, $message, $separator, undef, $l, $def);
}
sub ask_from_treelist_ {
    my ($o, $title, $message, $separator, $l, $def) = @_;
    my $transl = sub { join '|', map { translate($_) } split(quotemeta($separator), $_[0]) }; 
    ask_from_treelistf($o, $title, $message, $separator, $transl, $l, $def);
}
sub ask_from_treelistf {
    my ($o, $title, $message, $separator, $f, $l, $def) = @_;
    ask_from($o, $title, $message, [ { val => \$def, separator => $separator, list => $l, format => $f, sort => 1 } ]) or return;
    $def;
}

sub ask_many_from_list {
    my ($o, $title, $message, @l) = @_;
    @l = grep { @{$_->{list}} } @l or return '';
    foreach my $h (@l) {
	$h->{e}{$_} = {
	    text => may_apply($h->{label}, $_),
	    val => $h->{val} ? $h->{val}->($_) : do {
		my $i =
		  $h->{value} ? $h->{value}->($_) : 
		    $h->{values} ? member($_, @{$h->{values}}) : 0;
		\$i;
	    },
	    type => 'bool',
	    help => may_apply($h->{help}, $_, ''),
	    icon => may_apply($h->{icon2f}, $_, ''),
	} foreach @{$h->{list}};
	if ($h->{sort}) {
	    $h->{list} = [ sort { $h->{e}{$a}{text} cmp $h->{e}{$b}{text} } @{$h->{list}} ];
	}
    }
    $o->ask_from($title, $message, [ map { my $h = $_; map { $h->{e}{$_} } @{$h->{list}} } @l ]) or return;

    @l = map {
	my $h = $_;
	[ grep { ${$h->{e}{$_}{val}} } @{$h->{list}} ];
    } @l;
    wantarray ? @l : $l[0];
}

sub ask_from_entry {
    my ($o, $title, $message, %callback) = @_;
    first(ask_from_entries($o, $title, $message, [''], %callback));
}
sub ask_from_entries {
    my ($o, $title, $message, $l, %callback) = @_;

    my @l = map { my $i = ''; { label => $_, val => \$i } } @$l;

    $o->ask_from($title, $message, \@l, %callback) ?
      map { ${$_->{val}} } @l :
      undef;
}

#- can get a hash of callback: focus_out changed and complete
#- moreove if you pass a hash with a field list -> combo
#- if you pass a hash with a field hidden -> emulate stty -echo
sub ask_from {
    my ($o, $title, $message, $l, %callback) = @_;
    ask_from_($o, { title => $title, messages => $message, callbacks => \%callback }, $l);
}


sub ask_from_normalize {
    my ($o, $common, $l) = @_;

    foreach my $e (@$l) {
	if (my $li = $e->{list}) {
	    if ($e->{sort} || @$li > 10 && !exists $e->{sort}) {
		my @l2 = map { may_apply($e->{format}, $_) } @$li;
		my @places = sort { $l2[$a] cmp $l2[$b] } 0 .. $#l2;
		$e->{list} = $li = [ map { $li->[$_] } @places ];
	    }
	    $e->{type} = 'iconlist' if $e->{icon2f};
	    $e->{type} = 'treelist' if $e->{separator};
	    add2hash_($e, { not_edit => 1 });
	    $e->{type} ||= 'combo';
	    ${$e->{val}} = $li->[0] if ($e->{type} ne 'combo' || $e->{not_edit}) && !member(${$e->{val}}, @$li);
	    if ($e->{type} eq 'combo' && $e->{format}) {
		my @l = map { $e->{format}->($_) } @{$e->{list}};
		delete $e->{format};
		each_index {
		    ${$e->{val}} = $l[$::i] if $_ eq ${$e->{val}};
		} @{$e->{list}};
		($e->{list}, $e->{saved_list}) = (\@l, $e->{list});
	    }
	} elsif ($e->{type} eq 'range') {
	    $e->{min} <= $e->{max} or die "bad range min $e->{min} > max $e->{max} (called from " . join(':', caller()) . ")";
	    ${$e->{val}} = max($e->{min}, min(${$e->{val}}, $e->{max}));
	} elsif ($e->{type} eq 'button' || $e->{clicked} || $e->{clicked_may_quit}) {
	    $e->{type} = 'button';
	    $e->{clicked_may_quit} ||= $e->{clicked} ? sub { $e->{clicked}(); 0 } : sub {};	    
	    $e->{val} = \ (my $v = $e->{val}) if !ref($e->{val});
	} elsif ($e->{type} eq 'label' || !ref($e->{val})) {
	    $e->{type} = 'label';
	    $e->{val} = \ (my $v = $e->{val}) if !ref($e->{val});
	} else {
	    $e->{type} ||= 'entry';
	}
	$e->{disabled} ||= sub { 0 };
    }

    #- don't display empty lists and one element lists
    @$l = grep { 
	if ($_->{list} && $_->{not_edit} && !$_->{allow_empty_list}) {
	    if (@{$_->{list}} == ()) {
		eval {
		    require log;
		    log::l("ask_from_normalize: empty list for $_->{label}\n" . common::backtrace());
		};
	    }
	    @{$_->{list}} > 1;
	} else {
	    1;
	}
    } @$l;

    $common->{advanced_label} ||= _("Advanced");
    $common->{advanced_label_close} ||= _("Basic");
    $common->{$_} = [ deref($common->{$_}) ] foreach qw(messages advanced_messages);
    add2hash_($common, { ok => _("Ok"), cancel => _("Cancel") }) if !exists $common->{ok} && !$::isWizard;
    add2hash_($common->{callbacks} ||= {}, { changed => sub {}, focus_out => sub {}, complete => sub { 0 }, canceled => sub { 0 } });
}

sub ask_from_ {
    my ($o, $common, $l) = @_;
    ask_from_normalize($o, $common, $l);
    @$l or return 1;
    ask_from_real($o, $common, $l);
}
sub ask_from_no_check {
    my ($o, $common, $l) = @_;
    ask_from_normalize($o, $common, $l);
    $o->ask_fromW($common, [ grep { !$_->{advanced} } @$l ], [ grep { $_->{advanced} } @$l ]);
}
sub ask_from_real {
    my ($o, $common, $l) = @_;
    my $v = $o->ask_fromW($common, [ grep { !$_->{advanced} } @$l ], [ grep { $_->{advanced} } @$l ]);
    %$common = ();
    foreach my $e (@$l) {
	my $l = delete $e->{saved_list} or next;
	each_index {
	    ${$e->{val}} = $l->[$::i] if $_ eq ${$e->{val}};
	} @{$e->{list}};
	$e->{list} = $l;
    }
    $v;
}


sub ask_browse_tree_info {
    my ($o, $title, $message, $common) = @_;
    add2hash_($common, { ok => _("Ok"), cancel => _("Cancel") });
    add2hash_($common, { title => $title, message => $message });
    add2hash_($common, { grep_allowed_to_toggle      => sub { @_ },
			 grep_unselected             => sub { grep { $common->{node_state}($_) eq 'unselected' } @_ },
			 check_interactive_to_toggle => sub { 1 },
			 toggle_nodes                => sub {
			     my ($set_state, @nodes) = @_;
			     my $new_state = !$common->{grep_unselected}($nodes[0]) ? 'selected' : 'unselected';
			     $set_state->($_, $new_state) foreach @nodes;
			 },
		       });
    $o->ask_browse_tree_info_refW($common);
}
sub ask_browse_tree_info_refW { #- default definition, do not use with too many items (memory consuming)
    my ($o, $common) = @_;
    my ($l, $v, $h) = ([], [], {});
    $common->{build_tree}(sub {
			      my ($node) = $common->{grep_allowed_to_toggle}(@_);
			      if (my $state = $node && $common->{node_state}($node)) {
				  push @$l, $node;
				  $state eq 'selected' and push @$v, $node;
				  $h->{$node} = $state eq 'selected';
			      }
			  }, 'flat');
    add2hash_($common, { list   => $l, #- TODO interactivity of toggle is missing
			 values => $v,
			 help   => sub { $common->{get_info}($_[0]) },
		       });
    my ($new_v) = $o->ask_many_from_list($common->{title}, $common->{message}, $common) or return;
    $common->{toggle_nodes}(sub {}, grep { ! delete $h->{$_} } @$new_v);
    $common->{toggle_nodes}(sub {}, grep { $h->{$_} } keys %$h);
    1;
}

sub wait_message {
    my ($o, $title, $message, $temp) = @_;

    my $w = $o->wait_messageW($title, [ _("Please wait"), deref($message) ]);
    push @tempory::objects, $w if $temp;
    my $b = before_leaving { $o->wait_message_endW($w) };

    #- enable access through set
    MDK::Common::Func::add_f4before_leaving(sub { $o->wait_message_nextW([ deref($_[1]) ], $w) }, $b, 'set');
    $b;
}

sub kill {}

#-######################################################################################
#- Wonderful perl :(
#-######################################################################################
1;
href='#n230'>230</a>
<a id='n231' href='#n231'>231</a>
<a id='n232' href='#n232'>232</a>
<a id='n233' href='#n233'>233</a>
<a id='n234' href='#n234'>234</a>
<a id='n235' href='#n235'>235</a>
<a id='n236' href='#n236'>236</a>
<a id='n237' href='#n237'>237</a>
<a id='n238' href='#n238'>238</a>
<a id='n239' href='#n239'>239</a>
<a id='n240' href='#n240'>240</a>
<a id='n241' href='#n241'>241</a>
<a id='n242' href='#n242'>242</a>
<a id='n243' href='#n243'>243</a>
<a id='n244' href='#n244'>244</a>
<a id='n245' href='#n245'>245</a>
<a id='n246' href='#n246'>246</a>
<a id='n247' href='#n247'>247</a>
<a id='n248' href='#n248'>248</a>
<a id='n249' href='#n249'>249</a>
<a id='n250' href='#n250'>250</a>
<a id='n251' href='#n251'>251</a>
<a id='n252' href='#n252'>252</a>
<a id='n253' href='#n253'>253</a>
<a id='n254' href='#n254'>254</a>
<a id='n255' href='#n255'>255</a>
<a id='n256' href='#n256'>256</a>
<a id='n257' href='#n257'>257</a>
<a id='n258' href='#n258'>258</a>
<a id='n259' href='#n259'>259</a>
<a id='n260' href='#n260'>260</a>
<a id='n261' href='#n261'>261</a>
<a id='n262' href='#n262'>262</a>
<a id='n263' href='#n263'>263</a>
<a id='n264' href='#n264'>264</a>
<a id='n265' href='#n265'>265</a>
<a id='n266' href='#n266'>266</a>
<a id='n267' href='#n267'>267</a>
<a id='n268' href='#n268'>268</a>
<a id='n269' href='#n269'>269</a>
<a id='n270' href='#n270'>270</a>
<a id='n271' href='#n271'>271</a>
<a id='n272' href='#n272'>272</a>
<a id='n273' href='#n273'>273</a>
<a id='n274' href='#n274'>274</a>
<a id='n275' href='#n275'>275</a>
<a id='n276' href='#n276'>276</a>
<a id='n277' href='#n277'>277</a>
<a id='n278' href='#n278'>278</a>
<a id='n279' href='#n279'>279</a>
<a id='n280' href='#n280'>280</a>
<a id='n281' href='#n281'>281</a>
<a id='n282' href='#n282'>282</a>
<a id='n283' href='#n283'>283</a>
<a id='n284' href='#n284'>284</a>
<a id='n285' href='#n285'>285</a>
<a id='n286' href='#n286'>286</a>
<a id='n287' href='#n287'>287</a>
<a id='n288' href='#n288'>288</a>
<a id='n289' href='#n289'>289</a>
<a id='n290' href='#n290'>290</a>
<a id='n291' href='#n291'>291</a>
<a id='n292' href='#n292'>292</a>
<a id='n293' href='#n293'>293</a>
<a id='n294' href='#n294'>294</a>
<a id='n295' href='#n295'>295</a>
<a id='n296' href='#n296'>296</a>
<a id='n297' href='#n297'>297</a>
<a id='n298' href='#n298'>298</a>
<a id='n299' href='#n299'>299</a>
<a id='n300' href='#n300'>300</a>
<a id='n301' href='#n301'>301</a>
<a id='n302' href='#n302'>302</a>
<a id='n303' href='#n303'>303</a>
<a id='n304' href='#n304'>304</a>
<a id='n305' href='#n305'>305</a>
<a id='n306' href='#n306'>306</a>
<a id='n307' href='#n307'>307</a>
<a id='n308' href='#n308'>308</a>
<a id='n309' href='#n309'>309</a>
<a id='n310' href='#n310'>310</a>
<a id='n311' href='#n311'>311</a>
<a id='n312' href='#n312'>312</a>
<a id='n313' href='#n313'>313</a>
<a id='n314' href='#n314'>314</a>
<a id='n315' href='#n315'>315</a>
<a id='n316' href='#n316'>316</a>
<a id='n317' href='#n317'>317</a>
<a id='n318' href='#n318'>318</a>
<a id='n319' href='#n319'>319</a>
<a id='n320' href='#n320'>320</a>
<a id='n321' href='#n321'>321</a>
<a id='n322' href='#n322'>322</a>
<a id='n323' href='#n323'>323</a>
<a id='n324' href='#n324'>324</a>
<a id='n325' href='#n325'>325</a>
<a id='n326' href='#n326'>326</a>
<a id='n327' href='#n327'>327</a>
<a id='n328' href='#n328'>328</a>
<a id='n329' href='#n329'>329</a>
<a id='n330' href='#n330'>330</a>
<a id='n331' href='#n331'>331</a>
<a id='n332' href='#n332'>332</a>
<a id='n333' href='#n333'>333</a>
<a id='n334' href='#n334'>334</a>
<a id='n335' href='#n335'>335</a>
<a id='n336' href='#n336'>336</a>
<a id='n337' href='#n337'>337</a>
<a id='n338' href='#n338'>338</a>
<a id='n339' href='#n339'>339</a>
<a id='n340' href='#n340'>340</a>
<a id='n341' href='#n341'>341</a>
<a id='n342' href='#n342'>342</a>
<a id='n343' href='#n343'>343</a>
<a id='n344' href='#n344'>344</a>
<a id='n345' href='#n345'>345</a>
<a id='n346' href='#n346'>346</a>
<a id='n347' href='#n347'>347</a>
<a id='n348' href='#n348'>348</a>
<a id='n349' href='#n349'>349</a>
<a id='n350' href='#n350'>350</a>
<a id='n351' href='#n351'>351</a>
<a id='n352' href='#n352'>352</a>
<a id='n353' href='#n353'>353</a>
<a id='n354' href='#n354'>354</a>
<a id='n355' href='#n355'>355</a>
<a id='n356' href='#n356'>356</a>
<a id='n357' href='#n357'>357</a>
<a id='n358' href='#n358'>358</a>
<a id='n359' href='#n359'>359</a>
<a id='n360' href='#n360'>360</a>
<a id='n361' href='#n361'>361</a>
<a id='n362' href='#n362'>362</a>
<a id='n363' href='#n363'>363</a>
<a id='n364' href='#n364'>364</a>
<a id='n365' href='#n365'>365</a>
<a id='n366' href='#n366'>366</a>
<a id='n367' href='#n367'>367</a>
<a id='n368' href='#n368'>368</a>
<a id='n369' href='#n369'>369</a>
<a id='n370' href='#n370'>370</a>
<a id='n371' href='#n371'>371</a>
<a id='n372' href='#n372'>372</a>
<a id='n373' href='#n373'>373</a>
<a id='n374' href='#n374'>374</a>
<a id='n375' href='#n375'>375</a>
<a id='n376' href='#n376'>376</a>
<a id='n377' href='#n377'>377</a>
<a id='n378' href='#n378'>378</a>
<a id='n379' href='#n379'>379</a>
<a id='n380' href='#n380'>380</a>
<a id='n381' href='#n381'>381</a>
<a id='n382' href='#n382'>382</a>
<a id='n383' href='#n383'>383</a>
<a id='n384' href='#n384'>384</a>
<a id='n385' href='#n385'>385</a>
<a id='n386' href='#n386'>386</a>
<a id='n387' href='#n387'>387</a>
<a id='n388' href='#n388'>388</a>
<a id='n389' href='#n389'>389</a>
<a id='n390' href='#n390'>390</a>
<a id='n391' href='#n391'>391</a>
<a id='n392' href='#n392'>392</a>
<a id='n393' href='#n393'>393</a>
<a id='n394' href='#n394'>394</a>
<a id='n395' href='#n395'>395</a>
<a id='n396' href='#n396'>396</a>
<a id='n397' href='#n397'>397</a>
<a id='n398' href='#n398'>398</a>
<a id='n399' href='#n399'>399</a>
<a id='n400' href='#n400'>400</a>
<a id='n401' href='#n401'>401</a>
<a id='n402' href='#n402'>402</a>
<a id='n403' href='#n403'>403</a>
<a id='n404' href='#n404'>404</a>
<a id='n405' href='#n405'>405</a>
<a id='n406' href='#n406'>406</a>
<a id='n407' href='#n407'>407</a>
<a id='n408' href='#n408'>408</a>
<a id='n409' href='#n409'>409</a>
<a id='n410' href='#n410'>410</a>
<a id='n411' href='#n411'>411</a>
<a id='n412' href='#n412'>412</a>
<a id='n413' href='#n413'>413</a>
<a id='n414' href='#n414'>414</a>
<a id='n415' href='#n415'>415</a>
<a id='n416' href='#n416'>416</a>
<a id='n417' href='#n417'>417</a>
<a id='n418' href='#n418'>418</a>
<a id='n419' href='#n419'>419</a>
<a id='n420' href='#n420'>420</a>
<a id='n421' href='#n421'>421</a>
<a id='n422' href='#n422'>422</a>
<a id='n423' href='#n423'>423</a>
<a id='n424' href='#n424'>424</a>
<a id='n425' href='#n425'>425</a>
<a id='n426' href='#n426'>426</a>
<a id='n427' href='#n427'>427</a>
<a id='n428' href='#n428'>428</a>
<a id='n429' href='#n429'>429</a>
<a id='n430' href='#n430'>430</a>
<a id='n431' href='#n431'>431</a>
<a id='n432' href='#n432'>432</a>
<a id='n433' href='#n433'>433</a>
<a id='n434' href='#n434'>434</a>
<a id='n435' href='#n435'>435</a>
<a id='n436' href='#n436'>436</a>
<a id='n437' href='#n437'>437</a>
<a id='n438' href='#n438'>438</a>
<a id='n439' href='#n439'>439</a>
<a id='n440' href='#n440'>440</a>
<a id='n441' href='#n441'>441</a>
<a id='n442' href='#n442'>442</a>
<a id='n443' href='#n443'>443</a>
<a id='n444' href='#n444'>444</a>
<a id='n445' href='#n445'>445</a>
<a id='n446' href='#n446'>446</a>
<a id='n447' href='#n447'>447</a>
<a id='n448' href='#n448'>448</a>
<a id='n449' href='#n449'>449</a>
</pre></td>
<td class='lines'><pre><code><span class="hl kwa">package</span> fs<span class="hl opt">;</span> <span class="hl slc"># $Id$</span>

<span class="hl kwa">use</span> diagnostics<span class="hl opt">;</span>
<span class="hl kwa">use</span> strict<span class="hl opt">;</span>

<span class="hl kwa">use</span> common <span class="hl str">qw(:common :file :system :functional)</span><span class="hl opt">;</span>
<span class="hl kwa">use</span> <span class="hl kwc">log</span><span class="hl opt">;</span>
<span class="hl kwa">use</span> devices<span class="hl opt">;</span>
<span class="hl kwa">use</span> partition_table <span class="hl str">qw(:types)</span><span class="hl opt">;</span>
<span class="hl kwa">use</span> run_program<span class="hl opt">;</span>
<span class="hl kwa">use</span> swap<span class="hl opt">;</span>
<span class="hl kwa">use</span> detect_devices<span class="hl opt">;</span>
<span class="hl kwa">use</span> commands<span class="hl opt">;</span>
<span class="hl kwa">use</span> modules<span class="hl opt">;</span>
<span class="hl kwa">use</span> fsedit<span class="hl opt">;</span>
<span class="hl kwa">use</span> loopback<span class="hl opt">;</span>

<span class="hl num">1</span><span class="hl opt">;</span>

<span class="hl kwa">sub</span> add_options<span class="hl opt">(</span>\<span class="hl kwb">$&#64;</span><span class="hl opt">) {</span>
    <span class="hl kwc">my</span> <span class="hl opt">(</span><span class="hl kwb">$option, &#64;options</span><span class="hl opt">) =</span> <span class="hl kwb">&#64;_</span><span class="hl opt">;</span>
    <span class="hl kwc">my</span> <span class="hl kwb">%l</span><span class="hl opt">;</span> <span class="hl kwb">&#64;l</span><span class="hl opt">{</span><span class="hl kwc">split</span><span class="hl opt">(</span><span class="hl str">&apos;,&apos;</span><span class="hl opt">,</span> <span class="hl kwb">$$option</span><span class="hl opt">),</span> <span class="hl kwb">&#64;options</span><span class="hl opt">} = ();</span> <span class="hl kwc">delete</span> <span class="hl kwb">$l</span><span class="hl opt">{</span>defaults<span class="hl opt">};</span>
    <span class="hl kwb">$$option</span> <span class="hl opt">=</span> <span class="hl kwc">join</span><span class="hl opt">(</span><span class="hl str">&apos;,&apos;</span><span class="hl opt">,</span> <span class="hl kwc">keys</span> <span class="hl kwb">%l</span><span class="hl opt">) ||</span> <span class="hl str">&quot;defaults&quot;</span><span class="hl opt">;</span>
<span class="hl opt">}</span>

<span class="hl kwa">sub</span> read_fstab<span class="hl opt">($) {</span>
    <span class="hl kwc">my</span> <span class="hl opt">(</span><span class="hl kwb">$file</span><span class="hl opt">) =</span> <span class="hl kwb">&#64;_</span><span class="hl opt">;</span>

    <span class="hl kwc">local</span> <span class="hl opt">*</span>F<span class="hl opt">;</span>
    <span class="hl kwc">open</span> F<span class="hl opt">,</span> <span class="hl kwb">$file</span> <span class="hl kwc">or</span> <span class="hl kwa">return</span><span class="hl opt">;</span>

    <span class="hl kwc">map</span> <span class="hl opt">{</span>
	<span class="hl kwc">my</span> <span class="hl opt">(</span><span class="hl kwb">$dev, &#64;l</span><span class="hl opt">) =</span> <span class="hl kwc">split</span><span class="hl opt">;</span>
	<span class="hl kwb">$dev</span> <span class="hl opt">=~</span> s<span class="hl opt">,</span><span class="hl kwd">/(tmp|dev)/</span><span class="hl opt">,,;</span>
	<span class="hl opt">{</span> device <span class="hl opt">=&gt;</span> <span class="hl kwb">$dev,</span> mntpoint <span class="hl opt">=&gt;</span> <span class="hl kwb">$l</span><span class="hl opt">[</span><span class="hl num">0</span><span class="hl opt">],</span> type <span class="hl opt">=&gt;</span> <span class="hl kwb">$l</span><span class="hl opt">[</span><span class="hl num">1</span><span class="hl opt">],</span> options <span class="hl opt">=&gt;</span> <span class="hl kwb">$l</span><span class="hl opt">[</span><span class="hl num">2</span><span class="hl opt">] }</span>
    <span class="hl opt">} &lt;</span>F<span class="hl opt">&gt;;</span>
<span class="hl opt">}</span>

<span class="hl kwa">sub</span> up_mount_point <span class="hl opt">{</span>
    <span class="hl kwc">my</span> <span class="hl opt">(</span><span class="hl kwb">$mntpoint, $fstab</span><span class="hl opt">) =</span> <span class="hl kwb">&#64;_</span><span class="hl opt">;</span>
    <span class="hl kwa">while</span> <span class="hl opt">(</span><span class="hl num">1</span><span class="hl opt">) {</span>
	<span class="hl kwb">$mntpoint</span> <span class="hl opt">=</span> dirname<span class="hl opt">(</span><span class="hl kwb">$mntpoint</span><span class="hl opt">);</span>
	<span class="hl kwb">$mntpoint</span> <span class="hl kwc">ne</span> <span class="hl str">&quot;.&quot;</span> <span class="hl kwc">or</span> <span class="hl kwa">return</span><span class="hl opt">;</span>
	<span class="hl kwb">$_</span><span class="hl opt">-&gt;{</span>mntpoint<span class="hl opt">}</span> <span class="hl kwc">eq</span> <span class="hl kwb">$mntpoint</span> <span class="hl kwc">and</span> <span class="hl kwa">return</span> <span class="hl kwb">$_</span> <span class="hl kwa">foreach</span> <span class="hl kwb">&#64;$fstab</span><span class="hl opt">;</span>
    <span class="hl opt">}</span>
<span class="hl opt">}</span>

<span class="hl kwa">sub</span> check_mounted<span class="hl opt">($) {</span>
    <span class="hl kwc">my</span> <span class="hl opt">(</span><span class="hl kwb">$fstab</span><span class="hl opt">) =</span> <span class="hl kwb">&#64;_</span><span class="hl opt">;</span>

    <span class="hl kwc">local</span> <span class="hl opt">(*</span>F<span class="hl opt">, *</span>G<span class="hl opt">, *</span>H<span class="hl opt">);</span>
    <span class="hl kwc">open</span> F<span class="hl opt">,</span> <span class="hl str">&quot;/etc/mtab&quot;</span><span class="hl opt">;</span>
    <span class="hl kwc">open</span> G<span class="hl opt">,</span> <span class="hl str">&quot;/proc/mounts&quot;</span><span class="hl opt">;</span>
    <span class="hl kwc">open</span> H<span class="hl opt">,</span> <span class="hl str">&quot;/proc/swaps&quot;</span><span class="hl opt">;</span>
    <span class="hl kwa">foreach</span> <span class="hl opt">(&lt;</span>F<span class="hl opt">&gt;, &lt;</span>G<span class="hl opt">&gt;, &lt;</span>H<span class="hl opt">&gt;) {</span>
	<span class="hl kwa">foreach</span> <span class="hl kwc">my</span> <span class="hl kwb">$p</span> <span class="hl opt">(</span><span class="hl kwb">&#64;$fstab</span><span class="hl opt">) {</span>
	    <span class="hl kwd">/$p-&gt;{device}\s+([^\s]*)\s+/</span> <span class="hl kwc">and</span> <span class="hl kwb">$p</span><span class="hl opt">-&gt;{</span>mntpoint<span class="hl opt">} =</span> <span class="hl kwb">$1, $p</span><span class="hl opt">-&gt;{</span>isMounted<span class="hl opt">} =</span> <span class="hl kwb">$p</span><span class="hl opt">-&gt;{</span>isFormatted<span class="hl opt">} =</span> <span class="hl num">1</span><span class="hl opt">;</span>
	<span class="hl opt">}</span>
    <span class="hl opt">}</span>
<span class="hl opt">}</span>

<span class="hl kwa">sub</span> get_mntpoints_from_fstab <span class="hl opt">{</span>
    <span class="hl kwc">my</span> <span class="hl opt">(</span><span class="hl kwb">$fstab, $prefix, $uniq</span><span class="hl opt">) =</span> <span class="hl kwb">&#64;_</span><span class="hl opt">;</span>

    <span class="hl kwc">log</span><span class="hl opt">::</span>l<span class="hl opt">(</span><span class="hl str">&quot;reading fstab&quot;</span><span class="hl opt">);</span>
    <span class="hl kwa">foreach</span> <span class="hl opt">(</span>read_fstab<span class="hl opt">(</span><span class="hl str">&quot;</span><span class="hl ipl">$prefix/etc/fstab</span><span class="hl str">&quot;</span><span class="hl opt">)) {</span>
	<span class="hl kwa">next if</span> <span class="hl kwb">$uniq</span> <span class="hl opt">&amp;&amp;</span> fsedit<span class="hl opt">::</span>mntpoint2part<span class="hl opt">(</span><span class="hl kwb">$_</span><span class="hl opt">-&gt;{</span>mntpoint<span class="hl opt">},</span> <span class="hl kwb">$fstab</span><span class="hl opt">);</span>

	<span class="hl kwa">foreach</span> <span class="hl kwc">my</span> <span class="hl kwb">$p</span> <span class="hl opt">(</span><span class="hl kwb">&#64;$fstab</span><span class="hl opt">) {</span>
	    <span class="hl kwb">$p</span><span class="hl opt">-&gt;{</span>device<span class="hl opt">}</span> <span class="hl kwc">eq</span> <span class="hl kwb">$_</span><span class="hl opt">-&gt;{</span>device<span class="hl opt">}</span> <span class="hl kwc">or</span> <span class="hl kwa">next</span><span class="hl opt">;</span>
	    <span class="hl kwb">$_</span><span class="hl opt">-&gt;{</span>type<span class="hl opt">}</span> <span class="hl kwc">ne</span> <span class="hl str">&apos;auto&apos;</span> <span class="hl opt">&amp;&amp;</span> <span class="hl kwb">$_</span><span class="hl opt">-&gt;{</span>type<span class="hl opt">}</span> <span class="hl kwc">ne</span> type2fs<span class="hl opt">(</span><span class="hl kwb">$p</span><span class="hl opt">-&gt;{</span>type<span class="hl opt">})</span> <span class="hl kwc">and</span>
		<span class="hl kwc">log</span><span class="hl opt">::</span>l<span class="hl opt">(</span><span class="hl str">&quot;err, fstab and partition table do not agree for</span> <span class="hl ipl">$_</span><span class="hl str">-&gt;{device} type: &quot;</span> <span class="hl opt">. (</span>type2fs<span class="hl opt">(</span><span class="hl kwb">$p</span><span class="hl opt">-&gt;{</span>type<span class="hl opt">}) ||</span> type2name<span class="hl opt">(</span><span class="hl kwb">$p</span><span class="hl opt">-&gt;{</span>type<span class="hl opt">})) .</span> <span class="hl str">&quot; vs</span> <span class="hl ipl">$_</span><span class="hl str">-&gt;{type}&quot;</span><span class="hl opt">),</span> <span class="hl kwa">next</span><span class="hl opt">;</span>
	    <span class="hl kwc">delete</span> <span class="hl kwb">$p</span><span class="hl opt">-&gt;{</span>unsafeMntpoint<span class="hl opt">} || !</span><span class="hl kwb">$p</span><span class="hl opt">-&gt;{</span>mntpoint<span class="hl opt">}</span> <span class="hl kwc">or</span> <span class="hl kwa">next</span><span class="hl opt">;</span>
	    <span class="hl kwb">$p</span><span class="hl opt">-&gt;{</span>mntpoint<span class="hl opt">} =</span> <span class="hl kwb">$_</span><span class="hl opt">-&gt;{</span>mntpoint<span class="hl opt">};</span>
	    <span class="hl kwb">$p</span><span class="hl opt">-&gt;{</span>options<span class="hl opt">} =</span> <span class="hl kwb">$_</span><span class="hl opt">-&gt;{</span>options<span class="hl opt">};</span>
	<span class="hl opt">}</span>
    <span class="hl opt">}</span>
<span class="hl opt">}</span>

<span class="hl slc">#- mke2fs -b (1024|2048|4096) -c -i(1024 &gt; 262144) -N (1 &gt; 100000000) -m (0-100%) -L volume-label</span>
<span class="hl slc">#- tune2fs</span>
<span class="hl kwa">sub</span> format_ext2<span class="hl opt">(</span><span class="hl kwb">$&#64;</span><span class="hl opt">) {</span>
    <span class="hl kwc">my</span> <span class="hl opt">(</span><span class="hl kwb">$dev, &#64;options</span><span class="hl opt">) =</span> <span class="hl kwb">&#64;_</span><span class="hl opt">;</span>

    <span class="hl kwb">$dev</span> <span class="hl opt">=~</span> m<span class="hl opt">,(</span>rd<span class="hl opt">|</span>ida<span class="hl opt">)/,</span> <span class="hl kwc">and push</span> <span class="hl kwb">&#64;options,</span> <span class="hl str">qw(-b 4096 -R stride=16)</span><span class="hl opt">;</span> <span class="hl slc">#- For RAID only.</span>
    <span class="hl kwc">push</span> <span class="hl kwb">&#64;options,</span> <span class="hl str">qw(-b 1024 -O none)</span> <span class="hl kwa">if</span> arch<span class="hl opt">() =~</span> <span class="hl kwd">/alpha/</span><span class="hl opt">;</span>

    run_program<span class="hl opt">::</span>run<span class="hl opt">(</span><span class="hl str">&quot;mke2fs&quot;</span><span class="hl opt">,</span> <span class="hl kwb">&#64;options,</span> devices<span class="hl opt">::</span>make<span class="hl opt">(</span><span class="hl kwb">$dev</span><span class="hl opt">))</span> <span class="hl kwc">or die</span> _<span class="hl opt">(</span><span class="hl str">&quot;</span><span class="hl ipl">%s</span> <span class="hl str">formatting of</span> <span class="hl ipl">%s</span> <span class="hl str">failed&quot;</span><span class="hl opt">,</span> <span class="hl str">&quot;ext2&quot;</span><span class="hl opt">,</span> <span class="hl kwb">$dev</span><span class="hl opt">);</span>
<span class="hl opt">}</span>

<span class="hl kwa">sub</span> format_reiserfs<span class="hl opt">(</span><span class="hl kwb">$&#64;</span><span class="hl opt">) {</span>
    <span class="hl kwc">my</span> <span class="hl opt">(</span><span class="hl kwb">$dev, &#64;options</span><span class="hl opt">) =</span> <span class="hl kwb">&#64;_</span><span class="hl opt">;</span>

    <span class="hl slc">#TODO add -h tea</span>
    run_program<span class="hl opt">::</span>run<span class="hl opt">(</span><span class="hl str">&quot;mkreiserfs&quot;</span><span class="hl opt">,</span> <span class="hl str">&quot;-f&quot;</span><span class="hl opt">,</span> <span class="hl kwb">&#64;options,</span> devices<span class="hl opt">::</span>make<span class="hl opt">(</span><span class="hl kwb">$dev</span><span class="hl opt">))</span> <span class="hl kwc">or die</span> _<span class="hl opt">(</span><span class="hl str">&quot;</span><span class="hl ipl">%s</span> <span class="hl str">formatting of</span> <span class="hl ipl">%s</span> <span class="hl str">failed&quot;</span><span class="hl opt">,</span> <span class="hl str">&quot;reiserfs&quot;</span><span class="hl opt">,</span> <span class="hl kwb">$dev</span><span class="hl opt">);</span>
<span class="hl opt">}</span>

<span class="hl kwa">sub</span> format_dos<span class="hl opt">(</span><span class="hl kwb">$&#64;</span><span class="hl opt">) {</span>
    <span class="hl kwc">my</span> <span class="hl opt">(</span><span class="hl kwb">$dev, &#64;options</span><span class="hl opt">) =</span> <span class="hl kwb">&#64;_</span><span class="hl opt">;</span>

    run_program<span class="hl opt">::</span>run<span class="hl opt">(</span><span class="hl str">&quot;mkdosfs&quot;</span><span class="hl opt">,</span> <span class="hl kwb">&#64;options,</span> devices<span class="hl opt">::</span>make<span class="hl opt">(</span><span class="hl kwb">$dev</span><span class="hl opt">))</span> <span class="hl kwc">or die</span> _<span class="hl opt">(</span><span class="hl str">&quot;</span><span class="hl ipl">%s</span> <span class="hl str">formatting of</span> <span class="hl ipl">%s</span> <span class="hl str">failed&quot;</span><span class="hl opt">,</span> <span class="hl str">&quot;dos&quot;</span><span class="hl opt">,</span> <span class="hl kwb">$dev</span><span class="hl opt">);</span>
<span class="hl opt">}</span>

<span class="hl kwa">sub</span> format_hfs<span class="hl opt">(</span><span class="hl kwb">$&#64;</span><span class="hl opt">) {</span>
    <span class="hl kwc">my</span> <span class="hl opt">(</span><span class="hl kwb">$dev, &#64;options</span><span class="hl opt">) =</span> <span class="hl kwb">&#64;_</span><span class="hl opt">;</span>

    run_program<span class="hl opt">::</span>run<span class="hl opt">(</span><span class="hl str">&quot;hformat&quot;</span><span class="hl opt">,</span> <span class="hl kwb">&#64;options,</span> devices<span class="hl opt">::</span>make<span class="hl opt">(</span><span class="hl kwb">$dev</span><span class="hl opt">))</span> <span class="hl kwc">or die</span> _<span class="hl opt">(</span><span class="hl str">&quot;</span><span class="hl ipl">%s</span> <span class="hl str">formatting of</span> <span class="hl ipl">%s</span> <span class="hl str">failed&quot;</span><span class="hl opt">,</span> <span class="hl str">&quot;HFS&quot;</span><span class="hl opt">,</span> <span class="hl kwb">$dev</span><span class="hl opt">);</span>
<span class="hl opt">}</span>

<span class="hl kwa">sub</span> real_format_part <span class="hl opt">{</span>
    <span class="hl kwc">my</span> <span class="hl opt">(</span><span class="hl kwb">$part</span><span class="hl opt">) =</span> <span class="hl kwb">&#64;_</span><span class="hl opt">;</span>

    <span class="hl kwb">$part</span><span class="hl opt">-&gt;{</span>isFormatted<span class="hl opt">}</span> <span class="hl kwc">and</span> <span class="hl kwa">return</span><span class="hl opt">;</span>

    <span class="hl kwc">my</span> <span class="hl kwb">&#64;options</span> <span class="hl opt">=</span> <span class="hl kwb">$part</span><span class="hl opt">-&gt;{</span>toFormatCheck<span class="hl opt">}</span> ? <span class="hl str">&quot;-c&quot;</span> <span class="hl opt">: ();