package netconnect; # $Id: netconnect.pm,v 1.133 2000/10/17 11:18:22 damien Exp $





use common qw(:common :file :functional :system);
use log;
use detect_devices;
use run_program;
use netconnect_consts;
use modules;
use any;
use mouse;
use network;



sub isdn_write_config {
    my ($prefix, $isdn, $in, $isLaptop) = @_;
    
    
    my $f = "$prefix/etc/isdn/profile/link/myisp";
    local *F;
    open F, ">$f" or die "can't write $f : $!";
    chmod 0600, "$prefix/etc/isdn/profile/link/myisp";
    print F qq(
I4L_USERNAME="$isdn->{login}"
I4L_SYSNAME=""
I4L_LOCALMSN="$isdn->{phone_in}"
I4L_REMOTE_OUT="$isdn->{phone_out}"
I4L_DIALMODE="$isdn->{dialing_mode}"
);
#I4L_IDLETIME="$isdn->{idl}"
    close F;

    $f = "$prefix/etc/isdn/profile/card/mycard";
    open F, ">$f" or die "can't write $f : $!";
    print F qq(
I4L_MODULE="$isdn->{driver}"
I4L_TYPE="$isdn->{type}"
I4L_IRQ="$isdn->{irq}"
I4L_MEMBASE="$isdn->{mem}"
I4L_PORT="$isdn->{io}"
I4L_IO0="$isdn->{io0}"
I4L_IO1="$isdn->{io1}"
);
    close F;
    write_secret($isdn->{login}, $isdn->{passwd}, $prefix);

    $f = "$prefix/etc/sysconfig/network-scripts/net_cnx_up";
    open F, ">$f" or die "can't write network script in $f .";
    print F "#!/bin/bash
/sbin/route del default
/sbin/ifup ippp0
/sbin/isdnctrl dial ippp0
/bin/sleep 5
/sbin/route add default ippp0
";
    close F;

    $f = "$prefix/etc/sysconfig/network-scripts/net_cnx_down";
    open F, ">$f" or die "can't write network script in $f .";
    print F "#!/bin/bash
/sbin/isdnctrl hangup ippp0
/sbin/ifdown ippp0
/bin/sleep 5
" if eof;
    close F;

    chmod 0755, "$prefix/etc/sysconfig/network-scripts/net_cnx_down";
    chmod 0755, "$prefix/etc/sysconfig/network-scripts/net_cnx_up";
    system "$prefix/etc/rc.d/init.d/isdn4linux restart";
    $::isStandalone and ask_connect_now($prefix, $isdn, 'ippp0', $in, $isLaptop);
    1;
}

sub write_secret {
    my ($a,$b, $prefix) = @_;
    foreach ("pap-secrets", "chap-secrets") {
	substInFile { s/^$a.*\n//; $_ .= "\n'$a' * '$b' * \n" if eof  } "$prefix/etc/ppp/$_";
    }
}

sub ask_connect_now {
    my ($prefix, $cnx, $inter, $in, $isLaptop) = @_;
    my @l=(
	   { description => _("Yes"),
	     connect => 1},
	   { description => _("No"),
	     connect => 2},
	  );
    my $e = $in->ask_from_listf(_("Internet configuration"),
				_("Do you want to try to connect to the Internet now?"),
				sub { $_->{description} },
				\@l );

    if ($e->{connect} == 1) {
	my $up;
	{
	    my $w = $in->wait_message('', _("Testing your connection..."), 1);
	    system "$prefix/etc/sysconfig/network-scripts/net_cnx_up";
	    sleep 5;
	    $up=`ifconfig | grep "^$inter" 2> /dev/null`;
	}
	$in->ask_warn('', $up ? _("The system is now connected to Internet.") :
		      _("The system doesn't seem to be connected to internet.
Try to reconfigure your connection.") );
    }
}

sub read_isdn_providers {
    my $prefix = $_[0];
    my $file = "$prefix/usr/share/isdn_db.txt";
    my $file2 = "/usr/share/isdn_db.txt";
    local *F; open F, $file or open F, $file2 or die "can't find $file\n";
    map { (split '=>')[0] } <F>;
}

sub get_info_isdn_providers {
    my ($prefix, $isdn, $netc, $name) = @_;
    my $file = "$prefix/usr/share/isdn_db.txt";
    my $file2 = "/usr/share/isdn_db.txt";
    $name eq 'Unlisted - edit manually' and return;
    local *F; open F, $file or open F, $file2 or die "can't find $file\n";
    foreach (<F>) {
	chop;
	my ($name_, $phone, $real, $dns1, $dns2) = split '=>';
	if ($name eq $name_) {
	    $isdn->{user_name}=(split(/\|/, $name_))[2];
	    $isdn->{phone_out}=$phone;
	    $netc->{DOMAINNAME2} = $real;
	    $netc->{dnsServer2} = $dns1;
	    $netc->{dnsServer3} = $dns2;
	}
    }
}

sub isdn_ask_info {
    my ($prefix, $isdn, $netc, $in, $isLaptop) = @_;
    my $str= $in->ask_from_treelist( _("ISDN Configuration"), _("Select your provider.\n If it's not in the list, choose Unlisted"),
				     '|', ['Unlisted - edit manually', read_isdn_providers($prefix)]) or return 0;
    get_info_isdn_providers($prefix, $isdn, $netc, $str || 'Unlisted - edit manually' );
    foreach (qw(phone_in phone_out dialing_mode login passwd passwd2 idl)) {
	$isdn->{$_} ||= '';
    }
    $netc->{dnsServer2} ||= '';
    $netc->{dnsServer3} ||= '';
    $netc->{DOMAINNAME2} ||= '';
    ask_info2($prefix, $isdn, $netc, $in, $isLaptop);
}

sub ask_info2 {
    my ($prefix, $cnx, $netc, $in, $isLaptop) = @_;
    my @dial = ("auto", "manual");
    $::isInstall and $in->set_help('configureNetworkDNS');
  bcl:
    $in->ask_from_entries_ref(_("Connection Configuration"),
			      _("Please fill or check the field below"),
			      [
			       exists $cnx->{irq} ? _("Card IRQ") : (),
  			       exists $cnx->{mem} ? _("Card mem (DMA)") : (),
  			       exists $cnx->{io} ? _("Card IO") : (),
  			       exists $cnx->{io0} ? _("Card IO_0") : (),
  			       exists $cnx->{io1} ? _("Card IO_1") : (),
  			       exists $cnx->{phone_in} ? _("Your personal phone number") : (),
#  			       exists $cnx->{provider_name} ? _("Provider name (ex provider.net)") : (),
			       exists $netc->{DOMAINNAME2} ? _("Provider name (ex provider.net)") : (),
  			       exists $cnx->{phone_out} ? _("Provider phone number") : (),
  			       exists $netc->{dnsServer2} ? _("Provider dns 1") : (),
  			       exists $netc->{dnsServer3} ? _("Provider dns 2") : (),
  			       exists $cnx->{dialing_mode} ? _("Dialing mode") : (),
#			       exists $cnx->{idl} ? _("Time (secs) of inactivity after which\nit hangs up. (leave blank to disable it)") : (),
  			       exists $cnx->{login} ? _("Account Login (user name)") : (),
  			       exists $cnx->{passwd} ? _("Account Password") : (),
  			       exists $cnx->{passwd2} ? _("Confirm Password") : ()
			      ],
			      [
			       exists $cnx->{irq} ? ({ val => \$cnx->{irq} , list => '' }) : (),
  			       exists $cnx->{mem} ? ({ val => \$cnx->{mem} || '0x', list => '' }) : (),
  			       exists $cnx->{io} ? ({ val => \$cnx->{io} || '0x', list => '' }) : (),
  			       exists $cnx->{io0} ? ({ val => \$cnx->{io0} || '0x', list => '' }) : (),
  			       exists $cnx->{io1} ? ({ val => \$cnx->{io1} || '0x', list => '' }) : (),
  			       exists $cnx->{phone_in} ? ({ val => \$cnx->{phone_in}, list => '' }) : (),
#  			       exists $cnx->{provider_name} ? ({ val => \$cnx->{provider_name}, list => '' }) : (),
  			       exists $netc->{DOMAINNAME2} ? ({ val => \$netc->{DOMAINNAME2}, list => '' }) : (),
  			       exists $cnx->{phone_out} ? ({ val => \$cnx->{phone_out}, list => '' }) : (),
  			       exists $netc->{dnsServer2} ? ({ val => \$netc->{dnsServer2}, list => '' }) : (),
  			       exists $netc->{dnsServer3} ? ({ val => \$netc->{dnsServer3}, list => '' }) : (),
  			       exists $cnx->{dialing_mode} ? ({ val => \$cnx->{dialing_mode}, list => \@dial }) : (),
#			       exists $cnx->{idl} ?  ({ val => \$cnx->{idl}, list => '' }) : (),
  			       exists $cnx->{login} ? ({ val => \$cnx->{login}, list => ''}) : (),
  			       exists $cnx->{passwd} ? ({ val => \$cnx->{passwd}, list => '', hidden => 1}) : (),
  			       exists $cnx->{passwd2} ? ({ val => \$cnx->{passwd2}, list => '', hidden => 1}) : (),
			      ]
			     ) or return 0;
    $cnx->{passwd} ne $cnx->{passwd2} && do { $in->ask_warn('', "The passwords don't match. Try again!"); goto bcl };
1;
}

sub isdn_ask_protocol {
    my ($isdn, $in) = @_;
    #    if (my $e = $in->ask_from_listf(_("ISDN Configuration"),
    #  				   _("Which is your ISDN card ?"),
    #  				   sub { $_->{description} },
    #  				   [ @isdndata ])) {
    my @toto=(
	      { description => $::expert ? _("Europe (EDSS1)") : _("Europe"),
		protokol => 2},
	      { description => $::expert ? _("Rest of the world - no D-Channel (leased lines)") : _("Rest of the world"),
		protokol => 3}
	     );
    my $e = $in->ask_from_listf(_("ISDN Configuration"),
				_("Which protocol do you want to use ?"),
				sub { $_->{description} },
				\@toto ) or return 0;
    $e->{protokol};
}

sub isdn_ask {
    my ($prefix, $isdn, $netc, $in, $isLaptop) = @_;
    my $e;
    my @toto=(
	      { description => _("ISA / PCMCIA"),
		f => 1},
	      { description => _("PCI"),
		f => 2},
	      { description => _("I don't know"),
		f => 3}
	     );
    #    if ($isdn->{card_type} eq '') {
    $e = $in->ask_from_listf(_("ISDN Configuration"),
			     _("What kind of card do you have?"),
			     sub { $_->{description} },
			     \@toto ) or return 0;
    #    }
    if ($e->{f}==1 || $e->{f}==3) {
	my @tutu=(
		  { description => _("Continue"),
		    f => 1},
		  { description => _("Abort"),
		    f => 2},
		 );
	$a = $in->ask_from_listf(_("ISDN Configuration"),
				 _("
If you have an ISA card, the values on the next screen should be right.\n
If you have a PCMCIA card, you have to know the irq and io of your card.
"),
				 sub { $_->{description} },
				 \@tutu );
	$a->{f}==2 && return 0;
	$isdn->{card_type}='isa';
    }
    $isdn->{card_type}='pci' if ($e->{f}==2);

    $e = $in->ask_from_listf(_("ISDN Configuration"),
				    _("Which is your ISDN card ?"),
				    sub { $_->{description} },
				    [ grep {$_->{card} eq $isdn->{card_type}; } @isdndata ] ) or return 0;
    $isdn->{type}=$e->{type};
    $isdn->{driver}='hisax';
    $isdn->{protocol}=isdn_ask_protocol($isdn, $in) or return 0;
    $e->{mem} and $isdn->{mem}=$e->{mem};
    $e->{io} and $isdn->{io}=$e->{io};
    $e->{io0} and $isdn->{io0}=$e->{io0};
    $e->{io1} and $isdn->{io1}=$e->{io1};
    $e->{irq} and $isdn->{irq}=$e->{irq};
    isdn_ask_info($prefix, $isdn, $netc, $in, $isLaptop) or return 0;
    isdn_write_config($prefix, $isdn, $in, $isLaptop);
}

sub isdn_detect {
    my ($prefix, $isdn, $netc, $in, $isLaptop) = @_;
    if (my ($c) = (modules::get_that_type('isdn'))) {
  	$isdn->{description}=$c->{description}; 
  	$isdn->{vendor}=$c->{vendor};
  	$isdn->{id}=$c->{id};
  	$isdn->{driver}=$c->{driver};
  	log::l("found isdn card : $c->{description}; vendor : $c->{vendor};id : $c->{id}; driver : $c->{driver}\n");
	local $_=$c->{description};
	s/\|/ -- /;
	my $e=_("I have found an ISDN Card:\n") . $_;
	$in->ask_warn('', $e );
	my $l="$c->{vendor}$c->{id}";
	$isdn->{type}=%isdnid2type->{$l};
	if ($isdn->{type} eq '') {
	    $isdn->{card_type}='pci';
	    $in->ask_warn('', _("I have detected an ISDN PCI Card, but I don't know the type. Please select one PCI card on the next screen.")) or return 0;
	    isdn_ask($prefix, $isdn, $netc, $in, $isLaptop) or return 0;
	    $netc->{NET_DEVICE}='ippp0';
	    $netc->{NET_INTERFACE}='ippp0';
	    return 1;
	}
	$isdn->{protocol}=isdn_ask_protocol($isdn, $in) or return 0;
	isdn_ask_info($prefix, $isdn, $netc, $in, $isLaptop) or return 0;
	isdn_write_config($prefix, $isdn, $in, $isLaptop) or return 0;
	$netc->{NET_DEVICE}='ippp0';
	$netc->{NET_INTERFACE}='ippp0';
    } else {
	$in->ask_warn('', _("No ISDN PCI card found. Please select one on the next screen.")) or return 0;
	isdn_ask($prefix, $isdn, $netc, $in, $isLaptop) or return 0;
	$netc->{NET_DEVICE}='ippp0';
	$netc->{NET_INTERFACE}='ippp0';
    }
}

sub adsl_ask_info {
    my ($prefix, $adsl, $netc, $intf, $in, $isLaptop) = @_;
#    my $str= $in->ask_from_treelist( _("ADSL Configuration"), _("Select your provider.\n If it's not in the list, choose Unlisted"),
#				     '|', ['Unlisted - edit manually', read_adsl_providers($prefix)]) or return 0;
#    get_info_adsl_providers($prefix, $adsl, $netc, $str || 'Unlisted - edit manually' );
    $netc->{dnsServer2} ||= '';
    $netc->{dnsServer3} ||= '';
    $netc->{DOMAINNAME2} ||= '';
    $adsl->{login} ||= '';
    $adsl->{passwd} ||= '';
    $adsl->{passwd2} ||= '';
    ask_info2($prefix, $adsl, $netc, $in, $isLaptop);
}



sub conf_network_card {
    my ($prefix, $in, $netc, $intf, $isLaptop, $type, $ipadr, $netadr) = @_;
    
    any::setup_thiskind($in, 'net', !$::expert, 1);
    my @all_cards=detect_devices::getNet();
    $::isStandalone and modules::mergein_conf("$prefix/etc/modules.conf");
    @all_cards = map { $_ . (modules::get_alias($_) ? " (using module " . modules::get_alias($_) . ")" : "") } (@all_cards);
    my $interface;
    $netc->{nb_cards}=$#all_cards + 1;
    ($#all_cards == -1) and $in->ask_warn('', _("No ethernet network adapter has been detected on your system.
I cannot set up this connection type.")) and return 0;
    ($#all_cards == 0) and $interface = $all_cards[0] and goto l1;
    again :
	$interface = $in->ask_from_list(_("Choose the network interface"),
					_("Please choose which network adapter you want to use to connect to Internet"),
					\@all_cards,
				       ) or return 0;
    defined $interface or goto again;
  l1:
    $interface =~ /(eth[0-9]+)/ or die("Internal error");
    my $device = $1;
    $netc->{NET_DEVICE}=$device; 
    $intf->{$device}->{DEVICE}=$device;
    $intf->{$device}->{BOOTPROTO}=$type;
    $ipadr and $intf->{$device}->{IPADDR}=$ipadr;
    $intf->{$device}->{NETMASK}='255.255.255.0';
    $intf->{$device}->{NETWORK}=$netadr;
    $intf->{$device}->{ONBOOT}="yes";
    if ( $::isStandalone and !($type eq "dhcp")) {
	$in->ask_yesorno(_("Network interface"),
			  _("I'm about to restart the network device:\n") . $device . _("\nDo you agree?"), 1) and network::configureNetwork2($prefix, $netc, $intf) and system("$prefix/sbin/ifdown $device;$prefix/sbin/ifup $device");
    }
    1;
}










sub get_info_adsl_providers {
    my ($prefix, $isdn, $netc, $name) = @_;
    my $file = "$prefix/usr/share/adsl_db.txt";
    my $file2 = "/usr/share/adsl_db.txt";
    $name eq 'Unlisted - edit manually' and return;
    local *F; open F, $file or open F, $file2 or die "can't find $file\n";
    foreach (<F>) {
	chop;
	my ($name_, $real, $dns1, $dns2) = split '=>';
	if ($name eq $name_) {
	    $isdn->{user_name}=(split(/\|/, $name_))[2];
	    $netc->{DOMAINNAME2} = $real;
	    $netc->{dnsServer2} = $dns1;
	    $netc->{dnsServer3} = $dns2;
	}
    }
}

sub adsl_conf {
    my ($prefix, $adsl, $netc, $intf, $in, $isLaptop, $adsl_type) = @_;

    adsl_ask_info ($prefix, $adsl, $netc, $intf, $in, $isLaptop) or return 0;
#      my $f = "$prefix/etc/hosts";
#      if ($adsl_type eq 'pptp') {
#    	substInFile {
#    	    s/^(.*\n)/\#$1/;
#  	    $_ .= "10.0.0.10 my_computer\n10.0.0.138 adsl\n127.0.0.1 localhost localhost.localdomain\n" if eof;
#    	} $f;
#      }
    my $f="$prefix/etc/ppp/options";
    if ($adsl_type eq 'pptp') {
	substInFile {
	    s/.*\n//;
	    $_ .= "lock
noipdefault
noauth
usepeerdns
defaultroute
" if eof
	} $f;
    }
    if ($adsl_type eq 'pppoe') {
	substInFile {
	    s/.*\n//;

	    $_ .= "noipdefault
usepeerdns
hide-password
defaultroute
persist
lock
" if eof
	} $f;
    }

    write_secret($adsl->{login}, $adsl->{passwd}, $prefix);
    conf_network_card ($prefix, $in, $netc, $intf, $isLaptop, 'static' , $adsl_type eq 'pptp' ? '10.0.0.10' : "" ) or return 0;

    if ($adsl_type eq 'pppoe') {
	substInFile {
	    s/ETH=.*\n/ETH=$netc->{NET_DEVICE}\n/;
	    s/USER=.*\n/USER=$adsl->{login}\n/;
	} "$prefix/etc/ppp/pppoe.conf";
    }

    open F, ">$prefix/etc/sysconfig/network-scripts/net_cnx_up" or die '';
    if ($adsl_type eq 'pptp') {
	print F "#!/bin/bash
/sbin/route del default
/usr/bin/pptp 10.0.0.138 name $adsl->{login}
";
    }
    if ($adsl_type eq 'pppoe') {
	print F "#!/bin/bash
/sbin/route del default
ifconfig $netc->{NET_DEVICE} 0.0.0.0 up
LC_ALL=C LANG=C LANGUAGE=C LC_MESSAGES=C /usr/sbin/adsl-start $netc->{NET_DEVICE} $adsl->{login}
";
    }
    close F;
    open F, ">$prefix/etc/sysconfig/network-scripts/net_cnx_down" or die '';
    if ($adsl_type eq 'pptp') {
	print F q(#!/bin/bash
/usr/bin/killall pptp pppd
);
    }
    if ($adsl_type eq 'pppoe') {
	print F "#!/bin/bash
/usr/sbin/adsl-stop
/usr/bin/killall pppoe pppd
";
    }
    close F;
    chmod 0755, "$prefix/etc/sysconfig/network-scripts/net_cnx_down";
    chmod 0755, "$prefix/etc/sysconfig/network-scripts/net_cnx_up";

    my @l=(
	   { description => _("Yes"),
	     connect => 1},
	   { description => _("No"),
	     connect => 2},
	  );
    my $e = $in->ask_from_listf(_("ADSL configuration"),
				_("Do you want to start your connection at boot?"),
				sub { $_->{description} },
				\@l );
    if ($e->{connect} == 1) {
	open F, ">$prefix/etc/rc.d/init.d/adsl" or die '';
	print F q{
#!/bin/bash
#
# adsl       Bring up/down adsl connection
#
# chkconfig: 2345 11 89
# description: Activates/Deactivates the adsl interfaces
	case "$1" in
		start)
		echo -n "Starting adsl connection: "
		/etc/sysconfig/network-scripts/net_cnx_up
		touch /var/lock/subsys/adsl
		echo -n adsl
		echo
		;;
	stop)
		echo -n "Stopping adsl connection: "
		/etc/sysconfig/network-scripts/net_cnx_down
		echo -n adsl
		echo
		rm -f /var/lock/subsys/adsl
		;;
	restart)
		$0 stop
		echo -n "Waiting 10 sec before restarting adsl."
		sleep 10
		$0 start
		;;
	status)
		;;
	*)
	echo "Usage: adsl {start|stop|status|restart}"
	exit 1
esac
exit 0
 };
	close F;
	chmod 0755, "$prefix/etc/rc.d/init.d/adsl";
	$::isStandalone ? system("/sbin/chkconfig --add adsl") : do {
	    symlinkf "../init.d/adsl", "$prefix/etc/rc.d/rc0.d/K11adsl";
	    symlinkf "../init.d/adsl", "$prefix/etc/rc.d/rc1.d/K11adsl";
	    symlinkf "../init.d/adsl", "$prefix/etc/rc.d/rc2.d/K11adsl";
	    symlinkf "../init.d/adsl", "$prefix/etc/rc.d/rc3.d/S89adsl";
	    symlinkf "../init.d/adsl", "$prefix/etc/rc.d/rc5.d/S89adsl";
	    symlinkf "../init.d/adsl", "$prefix/etc/rc.d/rc6.d/K11adsl";
	};
    }
    else {
	-e "$prefix/etc/rc.d/init.d/adsl" and do{
	    system("/sbin/chkconfig --del adsl");
	    unlink "$prefix/etc/rc.d/init.d/adsl";
	};
    }
    $netc->{NET_INTERFACE}="ppp0";
    $::isStandalone && ask_connect_now($prefix, $adsl, 'ppp0', $in, $isLaptop);
}



sub pppConfig {
    my ($prefix, $modem, $mouse, $in, $isLaptop, $install, $netc) = @_;

    unless ($modem->{device} || $::expert && !$in->ask_yesorno('', _("Try to find a modem?"), 1)) {
	eval { modules::load("serial"); };
	detect_devices::probeSerialDevices();
	foreach (0..3) {
	    next if $mouse->{device} =~ /ttyS$_/;
	    detect_devices::hasModem("/dev/ttyS$_") and $modem->{device} = "ttyS$_", last;
	}
    }

    $::isInstall and $in->set_help('selectSerialPort');
    $modem->{device} ||= $in->ask_from_listf('', _("Please choose which serial port your modem is connected to."),
					     \&mouse::serial_port2text,
					     [ grep { $_ ne $mouse->{device} } mouse::serial_ports ]);

    $::isStandalone || $in->set_help('configureNetworkISP');
    any::pppConfig($in, $modem, $prefix, $install) if $in->ask_from_entries_refH('', _("Dialup options"), [
_("Connection name") => \$modem->{connection},
_("Phone number") => \$modem->{phone},
_("Login ID") => \$modem->{login},
_("Password") => { val => \$modem->{passwd}, hidden => 1 },
_("Authentication") => { val => \$modem->{auth}, list => [ __("PAP"), __("Terminal-based"), __("Script-based") ] },
_("Domain name") => \$modem->{domain},

_("First DNS Server") => \$modem->{dns1},
_("Second DNS Server") => \$modem->{dns2},

    ]);
    $netc->{NET_DEVICE}='ppp0';
    $netc->{NET_INTERFACE}='ppp0';
}





sub connected {


}

sub disconnected {

}

sub intro {
    my ($prefix, $netcnx, $in, $isLaptop, $install) = @_;
    my ($netc, $mouse, $intf) = ({}, {}, {});
    my $text;
    my $connected;
    read_net_conf($prefix, $netcnx, $netc);
    if ( $netc->{NET_DEVICE} ) {
	my $a=`ifconfig | grep "^$netcnx->{NET_INTERFACE}" 2> /dev/null`;
	if (!$a) {
	    $text=_("You are not currently connected to Internet.") . (-e "/etc/sysconfig/network-scripts/net_cnx_up" ? _("\nYou can connect to Internet or reconfigure your connection.") : _("\nYou can reconfigure your connection."));
	    $connected=0;
	}
	else {
	    $text=_("You are currently connected to internet.") . (-e "/etc/sysconfig/network-scripts/net_cnx_down" ? _("\nYou can disconnect or reconfigure your connection.") : _("\nYou can reconfigure your connection."));
	    $connected=1;
	}
	my @l=(
	       !$connected && -e "/etc/sysconfig/network-scripts/net_cnx_up" ? { description => _("Connect to Internet"),
										 c => 1} : (),
	       $connected && -e "/etc/sysconfig/network-scripts/net_cnx_down" ? { description => _("Disconnect from Internet"),
										  c => 2} : (),
	       { description => _("Configure network connection (LAN or Internet)"),
		 c => 3},
	      );
	my $e = $in->ask_from_listf(_("Internet connection & configuration"),
				    _($text),
				    sub { $_->{description} },
				    \@l );
	run_program::rooted($prefix, "/etc/sysconfig/network-scripts/net_cnx_up") if ($e->{c}==1);
	run_program::rooted($prefix, "/etc/sysconfig/network-scripts/net_cnx_down") if ($e->{c}==2);
	main($prefix, $netcnx, $netc, $mouse, $in, $isLaptop, $intf, $install, 0, 0) if ($e->{c}==3);
    } else {
	main($prefix, $netcnx, $netc, $mouse, $in, $isLaptop, $intf, $install, 0, 0);
    }
}

sub go_ethernet {
    my ($prefix, $in, $netc, $intf, $isLaptop, $type, $ipadr, $netadr, $first_time) = @_;
    conf_network_card($prefix, $in, $netc, $intf, $isLaptop, $type, $ipadr, $netadr) or return;
    $netc->{NET_INTERFACE}=$netc->{NET_DEVICE};
    network::configureNetwork($prefix, $netc, $in, $intf, $first_time);
    open F, ">$prefix/etc/sysconfig/network-scripts/net_cnx_up" or die '';
    print F "#!/bin/bash
ifup $netc->{NET_DEVICE}
";
    open F, ">$prefix/etc/sysconfig/network-scripts/net_cnx_down" or die '';
    print F "#!/bin/bash
ifdown $netc->{NET_DEVICE}
";
    if ( $::isStandalone and $netc->{NET_DEVICE}) {
	$in->ask_okcancel(_("Network interface"),
			  _("I'm about to restart the network device $netc->{NET_DEVICE}. Do you agree?"), 1) and system("$prefix/sbin/ifdown $netc->{NET_DEVICE}; $prefix/sbin/ifup $netc->{NET_DEVICE}");
    }
}

sub main {
    my ($prefix, $netcnx, $netc, $mouse, $in, $isLaptop, $intf, $install, $first_time, $direct_fr) = @_;
    $netc->{minus_one}=0;
    $::isInstall and $in->set_help('configureNetwork');
    my $continue = !($::beginner && values %$intf > 0 && $first_time);
    $::isStandalone and read_net_conf($prefix, $netcnx, $netc);
    $netc->{NET_DEVICE}=$netcnx->{NET_DEVICE} if $netcnx->{NET_DEVICE};
    $netc->{NET_INTERFACE}=$netcnx->{NET_INTERFACE} if $netcnx->{NET_INTERFACE};
    network::read_all_conf($prefix, $netc ||= {}, $intf ||= {});
#    $in->set_help('') unless $::isStandalone;
    $install->(qw(chkconfig net-tools));
    while ($continue) {
	my @l=(
	       { description => _("Configure a normal modem connection"),
		 connect => sub {
		     $netcnx->{type}='modem';
		     pppConfig($prefix, $netcnx->{$netcnx->{type}}, $mouse, $in, $isLaptop, $install, $netc);
		     open F, ">$prefix/etc/sysconfig/network-scripts/net_cnx_up" or die '';
		     print F q(#!/bin/bash
if [ -n "$DISPLAY" -a -e /usr/bin/kppp ]; then
/usr/bin/kppp &
else
ifup ppp0
fi
);
		     open F, ">$prefix/etc/sysconfig/network-scripts/net_cnx_down" or die '';
		     print F "#!/bin/bash
ifdown ppp0
killall pppd
";
		     ask_connect_now($prefix, $netcnx->{$netcnx->{type}}, 'ppp0', $in, $isLaptop);
		 }
	       },
	       { description => _("Configure an ISDN connection"),
		 connect => sub {
		     if (modules::get_that_type('isdn')) { goto intern_pci }
		     $::isInstall and $in->set_help('configureNetworkISDN');
		     my @m=(
			    { description => _("Internal ISDN card"),
			      c => 1},
			    { description => _("External ISDN modem"),
			      c => 2},
			   );
		     my $f = $in->ask_from_listf(_("Connect to the Internet"),
						 _("What kind is your ISDN connection?"),
						 sub { $_->{description} },
						 \@m );
		     if ($f->{c}==1) {
		       intern_pci:
			 $install->(qw(isdn4net isdn4k-utils dev));
			 $::Install and $in->pkg_install_if_requires_satisfied('kisdn');
			 $netcnx->{type}='isdn_internal';
			 $netcnx->{$netcnx->{type}}={};
			 isdn_detect($prefix, $netcnx->{$netcnx->{type}}, $netc, $in, $isLaptop);
		     }
		     if ($f->{c}==2) {
			 $netcnx->{type}='isdn_external';
			 $netcnx->{isdn_external}={};
			 $netcnx->{$netcnx->{type}}{special_command}='AT&F&O2B40';
			 pppConfig($prefix, $netcnx->{$netcnx->{type}}, $mouse, $in, $isLaptop, $install, $netc);
		     }
		 }
	       },
	       { description => _("Configure a DSL (or ADSL) connection"),
		 connect => sub {
		     my (@m, $f);
		     $::isInstall and $in->set_help('configureNetworkADSL');
		     if ($direct_fr) {
			 $f->{c}=1;
		     }
		     else {
			 @m=(
			     { description => _("France"),
			       c => 1},
			     { description => _("Other countries"),
			       c => 2},
			    );
			 $f = $in->ask_from_listf(_("Connect to the Internet"),
						  _("In which country are you located ?"),
						  sub { $_->{description} },
						  \@m );
		     }
		     if ($f->{c}==1) {
			 @m=(
			     { description => _("Alcatel modem"),
			       c => 1},
			     { description => _("ECI modem"),
			       c => 2},
			    );
			 $f = $in->ask_from_listf(_("Connect to the Internet"),
						  _("If your adsl modem is an Alcatel one, choose Alcatel. Otherwise, ECI."),
						  sub { $_->{description} },
						  \@m );
			 if ($f->{c}==1) {
			     $install->(qw(pptp-adsl-fr));
			     $netcnx->{type}='adsl_pptp';
			     $netcnx->{$netcnx->{type}}={};
			     adsl_conf($prefix, $netcnx->{$netcnx->{type}}, $netc, $intf, $in, $isLaptop, 'pptp');
			 }
			 if ($f->{c}==2) {
			     $install->(qw(rp-pppoe));
			     $netcnx->{type}='adsl_pppoe';
			     $netcnx->{$netcnx->{type}}={};
			     adsl_conf($prefix, $netcnx->{$netcnx->{type}}, $netc, $intf, $in, $isLaptop, 'pppoe');
			 }
		     }
		     elsif ($f->{c}==2) {
			 @m=(
			     { description => _("use pppoe"),
			       c => 1},
			     { description => _("don't use pppoe"),
			       c => 2},
			    );
			 $f = $in->ask_from_listf(_("Connect to the Internet"),
_("The most common way to connect with adsl is dhcp + pppoe.
However, some connections only use dhcp.
If you don't know, choose 'use pppoe'"),
						  sub { $_->{description} },
						  \@m );
			 if ($f->{c}==1) {
			     $install->(qw(rp-pppoe));
			     $netcnx->{type}='adsl_pppoe';
			     $netcnx->{$netcnx->{type}}={};
			     adsl_conf($prefix, $netcnx->{$netcnx->{type}}, $netc, $intf, $in, $isLaptop, 'pppoe');
			     
			     if ( $::isStandalone and $netc->{NET_DEVICE}) {
				 $in->ask_okcancel(_("Network interface"),
						   _("I'm about to restart the network device $netc->{NET_DEVICE}. Do you agree?"), 1) and system("$prefix/sbin/ifdown $netc->{NET_DEVICE}; $prefix/sbin/ifup $netc->{NET_DEVICE}");
			     }
			 }
			 if ($f->{c}==2) {
			     $install->(qw(dhcpcd));
			     go_ethernet($prefix, $in, $netc, $intf, $isLaptop, 'dhcp', '', '', $first_time);
			 }
		     }
		 }
	       },
	       { description => _("Configure a cable connection"),
		 connect => sub {
		     $::isInstall and $in->set_help('configureNetworkCable');
		     $netcnx->{type}='cable';
#  		     $netcnx->{cable}={};
#  		     $in->ask_from_entries_ref(_("Cable connection"),
#  _("Please enter your host name if you know it.
#  Some DHCP servers require the hostname to work.
#  Your host name should be a fully-qualified host name,
#  such as ``mybox.mylab.myco.com''."),
#  					       [_("Host name:")], [ \$netcnx->{cable}{hostname} ]);
		     if ($::expert) {

			 my @m=(
			     { description => "dhcpcd",
			       c => 1},
			     { description => "dhcpxd",
			       c => 3},
			     { description => "dhcp-client",
			       c => 4},
			    );
			 if (my $f = $in->ask_from_listf(_("Connect to the Internet"),
							 _("Which dhcp client do you want to use?
Default is dhcpd"),
							 sub { $_->{description} },
							 \@m )) {
			     $f->{c}==1 and $install->(qw(dhcpcd));
			     $f->{c}==3 and $install->(qw(dhcpxd));
			     $f->{c}==4 and $install->(qw(dhcp-client));
			 }
		     } else { $install->(qw(dhcpcd)) }
		     go_ethernet($prefix, $in, $netc, $intf, $isLaptop, 'dhcp', '', '', $first_time);
		 }
	       },
	       $::isStandalone && $netc->{NET_DEVICE} ? (
					 { description => _("Disable Internet Connection"),
					   connect => sub {
					       if ( -e "$prefix/etc/sysconfig/network-scripts/net_cnx_down") {
						   system ("$prefix/etc/sysconfig/network-scripts/net_cnx_down");
					       }
					       unlink "$prefix/etc/sysconfig/network-scripts/net_cnx_down";
					       unlink "$prefix/etc/sysconfig/network-scripts/net_cnx_up";
					       undef $netc->{NET_DEVICE};
					   },
					 },
					) : (),
	       { description => _("Configure local network"),
		 connect => sub {
		     $::isInstall and $in->set_help('configureNetworkIP');
		     network::configureNetwork($prefix, $netc, $in, $intf, $first_time);
		     $::isStandalone and $in->ask_yesorno(_("Network configuration"),
		     _("Do you want to restart the network"), 1) and do {
			 system("$prefix/etc/rc.d/init.d/network stop");
			 system("$prefix/etc/rc.d/init.d/network start");
		     };
		     $netc->{NETWORKING} = "yes";
		 },
	       },
	       $netc->{NETWORKING} ne 'no' ? (
					      { description => _("Disable networking"),
						connect => sub {
						    $netc->{NETWORKING} = 'no';
						},
					      },
					     ) : (),
	       { description => _("Done"),
		 connect => sub { $continue = 0 },
	       }
	      );
	my $e = $in->ask_from_listf([_("Configure the Internet connection / Configure local Network")],
				    values %$intf > 0 ? _("Local networking has already been configured.\nDo you want to:") :
				    _("How do you want to connect to the Internet?"),
				    sub { $_->{description} }, \@l);
	$e->{connect} and $e->{connect}();
	$::beginner and last;
    }
    
    
    
    
    
    
    network::configureNetwork2($prefix, $netc, $intf, $install);
    $netcnx->{NET_DEVICE}=$netc->{NET_DEVICE};
    $netcnx->{NET_INTERFACE}=$netc->{NET_INTERFACE};
    $netcnx->{NET_INTERFACE} and set_net_conf($prefix, $netcnx, $netc);
    $netcnx->{type} =~ /adsl/ or system("/sbin/chkconfig --del adsl 2> /dev/null");
    if ($netc->{NET_DEVICE} and $netc->{NETWORKING} ne 'no' and $::isStandalone and !$::beginner) {
	exists $netc->{nb_cards} or do {
	    any::setup_thiskind($in, 'net', !$::expert, 1);
	    my @all_cards=detect_devices::getNet();
	    $netc->{nb_cards}=$#all_cards + 1;
	};
	($netc->{nb_cards} - $netc->{minus_one} - (get_net_device($prefix) =~ /eth.+/ ? 1 : 0) > 0) and $in->ask_okcancel(_("Network Configuration"),
_("Now that your Internet connection is configured,
your computer can be configured to share its Internet connection.
Note: you need a dedicated Network Adapter to set up a Local Area Network (LAN).

Would you like to setup the Internet Connection Sharing?
"), 1) and system("/usr/sbin/drakgw --direct");
    }
}

sub get_net_device {
    my %h=getVarsFromSh("/etc/sysconfig/draknet");
    return %h->{NET_DEVICE};
}

sub read_net_conf {
    my ($prefix, $netcnx, $netc)=@_;
    add2hash($netcnx, { getVarsFromSh("$prefix/etc/sysconfig/draknet") });
    $netc->{NET_DEVICE}=$netcnx->{NET_DEVICE};
    $netc->{NET_INTERFACE}=$netcnx->{NET_INTERFACE};
    add2hash($netcnx->{$netcnx->{type}}, { getVarsFromSh("$prefix/etc/sysconfig/draknet." . $netcnx->{type}) });
}

sub set_net_conf {
    my ($prefix, $netcnx, $netc)=@_;
    setVarsInShMode("$prefix/etc/sysconfig/draknet", 0600, $netcnx, "NET_DEVICE", "NET_INTERFACE", "type" );
    setVarsInShMode("$prefix/etc/sysconfig/draknet." . $netcnx->{type}, 0600, $netcnx->{$netcnx->{type}});
}



#                WONDERFULL pad

1;
