#!/usr/bin/perl # Drakwizard # Copyright (C) 2002 MandrakeSoft Mael Dodin (mdodin@mandrakesoft.com) # # 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 lib qw(/usr/lib/libDrakX); use strict; use vars qw($Wizard_title $Wizard_pix_up $lib_script $perl_module %variable $in $rpm %chooser_hash $welcome $perl_module_name $verbose %description); use XML::Parser; use standalone; use interactive; use MDK::Common::Func; use common; #- I18N. push @::textdomains, 'drakwizard'; my $in = 'interactive'->vnew('su', 'default'); $::direct = /-direct/; $::Wizard_no_previous = 1; $::Wizard_title = "Drakwizard"; $::isWizard = 1; my $standalone = 1; ! -r '/etc/sysconfig/network-scripts/drakconnect_conf' && ( -r '/etc/sysconfig/network-scripts/draknet_conf' || die "no drakconnect conf file found, install drakconnect and try again") && MDK::Common::cp_af('/etc/sysconfig/network-scripts/draknet_conf', '/etc/sysconfig/network-scripts/drakconnect_conf'); if (!defined($ARGV[0])) { my $prefix = "/usr/share/wizards/"; my %wiz = ( 1 => [$prefix."web_wizard/web.wiz", "Apache"], 2 => [$prefix."dhcp_wizard/dhcp.wiz", "Dhcp"], 3 => [$prefix."dns_wizard/dns.wiz", "Dns (server)"], 4 => [$prefix."client_wizard/client.wiz", "Dns (client)"], 5 => [$prefix."news_wizard/news.wiz", "News"], 6 => [$prefix."nfs_wizard/nfs.wiz", "NFS"], 7 => [$prefix."postfix_wizard/postfix.wiz", "Postfix"], 8 => [$prefix."ftp_wizard/ftp.wiz", "Proftpd"], 9 => [$prefix."samba_wizard/samba.wiz", "Samba"], 10 => [$prefix."proxy_wizard/proxy.wiz", "Squid"], 11 => [$prefix."time_wizard/time.wiz", "Time"] ); $in->ask_from( _("Drakwizard wizard selection"), _("Please select a wizard"), [{ val => \$ARGV[0], list => [sort keys %wiz], format => sub { $wiz{$_}[1] }}] ); $ARGV[0] = $wiz{$ARGV[0]}[0]; } my $o = []; my $xmltree = XML::Parser->new(Style => 'Tree')->parsefile($ARGV[0]); get_parameter($o, $xmltree); if (("@ARGV" =~ /--debug/)) { $::verbose = 1; navigation($o, $o->[0]); } else { no warnings; $::verbose = 0; eval { navigation($o, $o->[0]) }; } $in->exit; #sub _ { # my ($format, @params) = @_; # # !$format and return; # sprintf(Locale::gettext::gettext($format), @params);} #} sub member { my $e = shift; foreach (@_) { $e eq $_ and return 1 } 0 } sub get_parameter { my ($o, $tree, $tag ,$page) = @_; foreach my $leaf (@$tree) { if (ref($leaf) eq 'ARRAY') { $page = get_parameter($o, $leaf, $tag, $page); } elsif (ref($leaf) eq 'HASH') { my $common_freetext_chooser; my %actions = ( Wizard => sub { ($Wizard_title, $lib_script, $perl_module, $rpm, $Wizard_pix_up) = @{$leaf}{qw(wizardTitle libScript perlModule rpm defaultImage)}; if ($rpm) { if (!$in->do_pkgs->is_installed($rpm)) { $in->ask_okcancel("error", _("%s is not installed\nClick \"Next\" to install or \"Cancel\" to quit", $rpm)) and $in->do_pkgs->install($rpm) or $in->exit; } } if ($perl_module) { ($::perl_module_name) = ($perl_module =~ /.*\/(.*)\.pm/); require $perl_module; } }, Variable => sub { $variable{$leaf->{name}} = $leaf->{shellVariable}; $ENV{$variable{$leaf->{name}}} = $leaf->{defaultValue}; }, Page => sub { !$welcome and $welcome = $page; my $old_page = $page; push @$o, $page = { %$leaf }; $old_page->{next_page} = $page; if ($leaf->{canBack} =~ /.*false.*/) { $page->{no_prev} = 1} if ($leaf->{canCancel} =~ /.*false.*/) { $page->{no_cancel} = 1} if ($leaf->{nextFinish} =~ /.*true.*/) { $page->{finish} = 1} }, Target => sub { $page->{Target}->{jumpIndex}->{$leaf->{jumpIndex}} = $leaf->{targetName}; }, Info => sub { # This is to avoid monospaced text to be interpolated $leaf->{helpText} = _($leaf->{helpText}); if ($leaf->{fontName} eq "Monospaced") { chomp($leaf->{helpText}); $leaf->{helpText} = $leaf->{helpText} ? "$leaf->{helpText}\n$leaf->{helpText}" : "$leaf->{helpText}\n"; } $page->{info} = $leaf->{helpText} ? "$page->{info}\n$leaf->{helpText}" : "$page->{info}\n"; if ($leaf->{fillScript}) { $page->{info} .= `source $lib_script; $leaf->{fillScript}`; } # XML compatibility $page->{info} =~ s/\\\'/\'/g; $page->{info} =~ s/\\q/\"/g; $page->{info} =~ s/\\a/\&/g; $page->{info} =~ s/\\n/\n/g; }, Freetext => $common_freetext_chooser = sub { push @{$page->{freetext}}, { %$leaf, main_order => (($tag eq 'Chooser') ? 'combo' : ($tag eq 'Boolean') ? 'bool' : ($leaf->{editable} eq 'true') ? 'entry' : 'field')}; }, Chooser => \&$common_freetext_chooser, Boolean => \&$common_freetext_chooser, Option => sub { push @{${$page->{freetext}}[-1]->{Option}}, $leaf->{description}; $chooser_hash{$leaf->{description}} = $leaf->{value}; $description{$leaf->{value}} = $leaf->{description}; }); $actions{$tag} and &{$actions{$tag}}; }elsif($leaf=~ /\w\D/){ $tag = $leaf; } } $page; } sub find_page { my ($o, $name) = @_; $_->{name} eq $name and return $_ foreach @$o; } sub display { my ($o, $page) = @_; if ($page->{no_prev} || $page->{name} eq $welcome->{name}) { $::Wizard_no_previous = 1; } if ($page->{finish}) { $::Wizard_finished = 1; $::Wizard_no_cancel = 1; } if ($page->{nextFinish} eq 'true') { $::Wizard_no_previous = 1; $in->ask_okcancel(_($page->{name}), _($page->{info})) or quit_global($in, 0); die; } elsif (!$page->{freetext}) { $in->ask_okcancel(_($page->{name}), _($page->{info})) or navigation($o, $page->{old_page}); } else { my $valeur; my @liste; my @data = map { if ($_->{main_order} eq 'combo') { if($_->{fillScript}){ @liste = (); $valeur = `. $lib_script && $_->{fillScript}`; while( $valeur =~ /(.+)\n/g){ push @liste, $1; $chooser_hash{$1} = $1; } } else { @liste = @{$_->{Option}}; } } if ($_->{fillScript}) { $ENV{$variable{$_->{variableName}}} = `. $lib_script && $_->{fillScript}`; } elsif (!$ENV{$variable{$_->{variableName}}} && $_->{fillfunc}) { $ENV{$variable{$_->{variableName}}} = $::{$perl_module_name."::"}{"$_->{fillfunc}"}->(); } chomp($ENV{$variable{$_->{variableName}}}); $ENV{$_->{variableName}} = $ENV{$variable{$_->{variableName}}}; ($_->{main_order} eq 'entry') ? { label => _($_->{helpText}), val => \$ENV{$variable{$_->{variableName}}}, type => $_->{main_order} } : ($_->{main_order} eq 'bool') ? { val => \$ENV{$variable{$_->{variableName}}}, type => $_->{main_order}, text => _($_->{helpText}, advanced => 1) } : ($_->{main_order} eq 'field') ? { val => $description{$ENV{$variable{$_->{variableName}}}}? $description{$ENV{$variable{$_->{variableName}}}} : $ENV{$variable{$_->{variableName}}}, label => $_->{helpText} } : { label => $_->{helpText}, val => \$ENV{$variable{$_->{variableName}}}, list => [@liste], type => $_->{main_order} }; } @{$page->{freetext}}; if($page->{executionLevel} eq 'NORMAL') { $in->ask_from(_($page->{name}), _($page->{info}), \@data) or navigation($o, $page->{old_page}); foreach(@{$page->{freetext}}) { if($_->{main_order} eq 'bool') { $ENV{$variable{$_->{variableName}}} = $ENV{$variable{$_->{variableName}}} ? 1 : 0; $description{$ENV{$variable{$_->{variableName}}}} = $ENV{$variable{$_->{variableName}}} ? _("enabled") : _("disabled"); } elsif ($_->{main_order} eq 'combo') { $ENV{$variable{$_->{variableName}}} = $chooser_hash{$ENV{$variable{$_->{variableName}}}}; } } } } undef $::Wizard_no_previous; } sub navigation { my ($o, $page, $previous_page) = @_; $page->{old_page} ||= $previous_page; display($o, $page); my ($next, $prev) = do { if (defined $perl_module_name && $page->{func}) { my @func_arg = split(/\s/, $page->{func}); my $modStatus = $::{$perl_module_name."::"}{"$func_arg[0]"}->(); if ($page->{Target}->{jumpIndex}) { find_page($o, $page->{Target}->{jumpIndex}->{int($modStatus)}), $page; } else { $page->{next_page}, $page->{old_page}; } } elsif ($page->{jumpScript}) { system("source $lib_script ; $page->{jumpScript}"); if ($page->{Target}->{jumpIndex}) { $? = $? >> 8; find_page($o, $page->{Target}->{jumpIndex}->{$?}), $page; } else { $page->{next_page}, $page; } } elsif ($page->{subWizard}) { my $sub_o = []; my $sub_xmltree = XML::Parser->new(Style => 'Tree')->parsefile($page->{subWizard}); get_parameter($sub_o, $sub_xmltree); eval {navigation($sub_o, $sub_o->[0]); }; $in->exit if $@ =~ /^wizcancel/; $page->{old_page}->{no_prev} = 1; $o->[0], $page, undef; } elsif ($page->{jumpPage}) { find_page($o, $page->{jumpPage}), $page; } else { $page->{next_page}, $page; } }; navigation($o, $next, $prev); }