#!/usr/bin/perl

# DrakNet

# Copyright (C) 1999 MandrakeSoft (damien@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 Gtk;
init Gtk;
use Data::Dumper;
use lib qw(/usr/lib/libDrakX);

use interactive;
use standalone;
use netconnect;
use c;

$::isEmbedded = ($::XID, $::CCPID) = "@ARGV" =~ /--embedded (\w+) (\w+)/;
$::isWizard = "@ARGV" =~ /--wizard/;
print "EMBED\n" if $::isEmbedded;
print "XID : $::XID\n";
print "CCPID :  $::CCPID\n";
print "wiz : $::isWizard\n";

local $_ = join '', @ARGV;

#/-h/ and die "usage: draknet[--xf3] [--beginner] [--expert] [--auto] [--noauto] [--skiptest] [--testing]\n";

my $netcnx = {};
my $netc = {};
my $intf = {};
my @conx_type = ('modem', 'isdn_internal', 'isdn_external', 'adsl', 'cable', 'lan' );

$::wizard_xpm = "/usr/share/pixmaps/internet.xpm";
#$::force_xf3 = /-xf3/;
#$::beginner = /-beginner/;
#$::expert = /-expert/;
#$::auto = /-auto/;
#$::noauto = /-noauto/;
#$::testing = /-testing/;
#$i->{skiptest} = /-skiptest/;

#my $f = "/usr/X11R6/lib/X11/Cards";
#-e $f or system("urpmi --auto XFree86 XFree86-75dpi-fonts");
#-e $f or die "install XFree86 first!\n";

my $in = vnew interactive('su');

#$::isEmbedded or goto dd;
my $window1 = $::isEmbedded ? new Gtk::Plug ($::XID) : new Gtk::Window -toplevel;
$window1->signal_connect ( delete_event => sub { Gtk->exit(0); });
$window1->set_position(1);
$window1->set_title(_("Network configuration"));
$window1->border_width(10);
my $vbox1 = new Gtk::VBox(0,0);
$window1->add($vbox1);
my $hbox1 = new Gtk::HBox(0,0);
$vbox1->pack_start($hbox1,1,1,0);
$hbox1->pack_start(new Gtk::Label(_("Profile: ")),0,0,0);
my $combo1 = new Gtk::Combo;
$combo1->set_popdown_strings (get_profiles() );
$combo1->entry->set_editable(0);
$combo1->entry->set_text("default");
$hbox1->pack_start($combo1,0,0,0);
my $hbox2 = new Gtk::HBox(0,0);
$vbox1->pack_start($hbox2,1,1,0);
$hbox2->pack_start(new Gtk::Label(_("Hostname: ")),0,0,0);
chop (my $hostname = `hostname`);
my $label_host = new Gtk::Label($hostname);
$hbox2->pack_start($label_host,0,0,0);

$vbox1->pack_start(new Gtk::HSeparator,1,1,5);

netconnect::load_conf('', $netcnx, $netc, $intf);

my $frame1 = new Gtk::Frame (_("Internet access"));
$vbox1->pack_start($frame1,1,1,0);
my $table1 = new Gtk::Table (3,3, 0);
$table1->set_row_spacings(5);
$table1->set_col_spacings(5);
#$table1->border_width(10);
$frame1->add($table1);
#attach (table, child, left_attach, right_attach, top_attach, bottom_attach, xoptions, yoptions, xpadding, ypadding)
#$table->attach($button[0], 0, 1, 0, 1, {expand=>1,fill=>1}, {expand=>1,fill=>1},0,0);
$table1->attach(new Gtk::Label(_("Type:")), 0, 1, 0, 1, 'fill', 'fill',0,0);
print "type : $netcnx->{type}\n";
my $label4 = new Gtk::Label($netcnx->{type});
$table1->attach($label4, 1, 2, 0, 1, 'fill', 'fill',0,0);
my $label5 = new Gtk::Label($netcnx->{type} eq 'lan' ? _("Gateway:") : _("Interface:"));
$table1->attach($label5, 0, 1, 1, 2, 'fill', 'fill',0,0);
my $label6 = new Gtk::Label($netcnx->{type} eq 'lan' ? $netc->{GATEWAY} : $netc->{NET_INTERFACE});
$table1->attach($label6, 1, 2, 1, 2, 'fill', 'fill',0,0);
my $label7 = new Gtk::Label(_("Status:"));
$table1->attach($label7, 0, 1, 2, 3, 'fill', 'fill',0,0);
my $label8 = new Gtk::Label(_("not connected"));
$table1->attach($label8, 1, 2, 2, 3, 'fill', 'fill',0,0);

my $button1 = new Gtk::Button(_("Configure..."));
$table1->attach($button1, 2, 3, 0, 2, 'fill', 'fill',0,0);
$button1->signal_connect( clicked => [ \&configure_net, '', $netcnx, $netc, $intf]);

my $button2 = new Gtk::Button(_("Connect..."));
$table1->attach($button2, 2, 3, 2, 3, 'fill', 'fill',0,0);

my $separator2 = new Gtk::HSeparator;
$vbox1->pack_start($separator2,1,1,5);

my $frame2 = new Gtk::Frame (_("LAN configuration"));
$vbox1->pack_start($frame2,1,1,0);
my$table2 = new Gtk::Table (3,2, 0);
$table2->set_row_spacings(5);
$table2->set_col_spacings(5);
$frame2->add($table2);
my $label9 = new Gtk::Label(_("Network adaptater 1 (eth0):"));
$table2->attach($label9, 0, 1, 0, 1, 'fill', 'fill',0,0);
my $label10 = new Gtk::Label(_("isdn_internal"));
$table2->attach($label10, 1, 2, 0, 1, 'fill', 'fill',0,0);
my $label11 = new Gtk::Label(_("Interface:"));
$table2->attach($label11, 0, 1, 1, 2, 'fill', 'fill',0,0);
my $label12 = new Gtk::Label(_("ippp0"));
$table2->attach($label12, 1, 2, 1, 2, 'fill', 'fill',0,0);
my $label13 = new Gtk::Label(_("Status:"));
$table2->attach($label13, 0, 1, 2, 3, 'fill', 'fill',0,0);
my $label14 = new Gtk::Label(_("not connected"));
$table2->attach($label14, 1, 2, 2, 3, 'fill', 'fill',0,0);

my $bbox1 = new Gtk::HButtonBox;
$vbox1->pack_start($bbox1,0,0,0);
$bbox1->set_layout(-end);
my $button_ok = new Gtk::Button "OK";
$button_ok->signal_connect ( clicked => sub { quit_global(); });
$bbox1->add($button_ok);
my $button_cancel = new Gtk::Button "Cancel";
$button_cancel->signal_connect ( clicked => sub {
				     netconnect::load_conf('', $netcnx, $netc, $intf);
				     $combo1->entry->set_text("default");
				     chop (my $h = `hostname`);
				     $label_host->set ($h);
				     $label4->set($netcnx->{type});
				     $label6->set($netcnx->{type} eq 'lan' ? $netc->{GATEWAY} : $netc->{NET_INTERFACE});
				     quit_global();
				 });
$bbox1->add($button_cancel);
$combo1->entry->signal_connect( 'changed', sub {
				    print "************ " . $combo1->entry->get_text() . " *********\n";
				    netconnect::set_profile('', $combo1->entry->get_text());
				    netconnect::load_conf('', $netcnx, $netc, $intf);
				    chop (my $h = `hostname`);
				    $label_host->set ($h);
				    $label4->set($netcnx->{type});
				    print "************ $netc->{GATEWAY} *********\n";
				    $label6->set($netcnx->{type} eq 'lan' ? $netc->{GATEWAY} : $netc->{NET_INTERFACE});
				});

$window1->show_all();
Gtk->main;

dd:
netconnect::intro('', $netcnx, $in, sub { $in->standalone::pkgs_install(@_) });
$in->exit(0);

sub get_profiles {
    my @a;
    my $i=0;
    foreach (glob("/etc/sysconfig/network-scripts/draknet_conf.*")) {
	print "[$_]\n";
	s/.*\.//;
	print "$_\n";
	$a[$i] = $_;
	$i++;
    }
    @a;
    #grep { s/.*?_//;} 
}

sub quit_global {
    $::isEmbedded ? kill(USR1, $::CCPID) : Gtk->exit(0);
}

sub configure_net {
    my (undef, $prefix, $netcnx, $netc, $intf) = @_;
    my $cnx={};
    $cnx=$netcnx->{$netcnx->{type}};
    my $auto_detect={};
    my $window = new Gtk::Window -toplevel;
    $window->set_policy (1, 1, 1);
    $window->signal_connect ( delete_event => sub { Gtk->main_quit; });
    $window->set_position(1);
    $window->set_title(_("Internet connection configuration"));
    $window->border_width(10);
    my $vbox1 = new Gtk::VBox(0,0);
    $window->add($vbox1);
    $vbox1->pack_start(new Gtk::Label(_("Internet Connection Configuration")),0,1,0);

    $vbox1->pack_start(new Gtk::HSeparator,0,0,5);
    my $table1 = new Gtk::Table (2, 4, 0);
    $table1->set_row_spacings(5);
    $table1->set_col_spacings(5);
    $vbox1->pack_start($table1,0,0,0);
    $table1->attach(new Gtk::Label(_("Profile: ")), 0, 1, 0, 1, 'fill', 'fill',0,0);
    $table1->attach(new Gtk::Label(_('default')), 1, 2, 0, 1, 'fill', 'fill',0,0);
    $table1->attach(new Gtk::Label(_("Connection type: ")), 0, 1, 1, 2, 'fill', 'fill',0,0);
    $table1->attach(new Gtk::Label(_('modem')), 1, 2, 1, 2, 'fill', 'fill',0,0);
    my $button1 = new Gtk::Button(_("Reconfigure using wizard..."));
    $table1->attach($button1, 2, 4, 0, 2, 'fill', 'fill',0,0);
    $vbox1->pack_start(new Gtk::HSeparator,0,0,5);

    my $frame1 = new Gtk::Frame (_("Parameters"));
    $vbox1->pack_start($frame1,1,1,0);
    my $vbox2 = new Gtk::VBox(0,0);
    $frame1->add($vbox2);
    my $i=0;
    foreach ([_("Card IRQ"), \$cnx->{irq}],
	     [_("Card mem (DMA)"), \$cnx->{mem}],
	     [_("Card IO"), \$cnx->{io}],
	     [_("Card IO_0"), \$cnx->{io0} ],
	     [_("Card IO_1"), \$cnx->{io1}],
	     [_("Your personal phone number"), \$cnx->{phone_in}],
	     [_("Provider name (ex provider.net)"), \$netc->{DOMAINNAME2}],
	     [_("Provider phone number"), \$cnx->{phone_out}],
	     [_("Provider dns 1 (optional)"), \$netc->{dnsServer2}],
	     [_("Provider dns 2 (optional)"), \$netc->{dnsServer3}],
	     [_("Account Login (user name)"), \$cnx->{login}],
	     [_("Account Password"), \$cnx->{passwd}],
	     [_("Dialing mode"), $cnx->{dialing_mode}, [ "auto", "manual"] ],
	     [_("Gateway"), \$netc->{GATEWAY}],
	     [_("Connection name"), \$modem->{connection} ],
	     [_("Phone number"), \$modem->{phone} ],
	     [_("Login ID"), \$modem->{login} ],
	     [_("Password"), \$modem->{passwd} ],
	     [_("Authentication"), \$modem->{auth}, [ _("PAP"), _("Terminal-based"), _("Script-based") ] ],
	     [_("Domain name"), \$modem->{domain} ],
	     [_("First DNS Server"), \$modem->{dns1} ],
	     [_("Second DNS Server"), \$modem->{dns2} ]) {
	$infos[2*$i]=new Gtk::HBox(0,0);
	my $l=new Gtk::Label($_->[0]);
	$l->set_justify('left');
	$infos[2*$i]->pack_start($l,1,1,0);
	$vbox2->pack_start($infos[2*$i],0,0,0);
  	if (defined $_->[2]) {
	    my $c=new Gtk::Combo();
	    $c->set_popdown_strings(@{$_->[2]});
	    $infos[2*$i+1]=$c->entry;
	    $infos[2*$i+1]->set_editable(0);
	    $infos[2*$i]->pack_start($c,0,0,0);
	} else {
	    $infos[2*$i+1]=new Gtk::Entry();
	    $infos[2*$i]->pack_start($infos[2*$i+1],0,0,0);
	}
	$infos[2*$i+1]->set_text(${$_->[1]});
  	$i++;
    }
    my @mask;
    $netcnx->{type}='isdn_internal';
    @mask=(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0) if $netcnx->{type} eq 'lan';
    @mask=(0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0) if $netcnx->{type} eq 'isdn_internal' && $cnx->{card_type} ne 'pci';
    @mask=(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0) if $netcnx->{type} eq 'isdn_internal' && $cnx->{card_type} eq 'isa';
    $vbox1->pack_start(new Gtk::HSeparator,0,0,5);


    my $bbox1 = new Gtk::HButtonBox;
    $vbox1->pack_start($bbox1,0,0,0);
    $bbox1->set_layout(-end);
    my $button_ok = new Gtk::Button "OK";
    $button_ok->signal_connect ( clicked => sub { Gtk->main_quit });
    $bbox1->add($button_ok);
    my $button_cancel = new Gtk::Button "Cancel";
    $bbox1->add($button_cancel);

    $window->set_modal(1);
    $window->show_all();
    $i=0;
    foreach (@mask) {
	print "$_\n";
	if ($_) { $infos[2*$i]->show }
	else { $infos[2*$i]->hide; }
	$i++;
    }
    $window->set_position('center_always');
    Gtk->main;

}

sub detect {
    my ($auto_detect) = @_;
    my $isdn={};
    netconnect::isdn_detect_backend($isdn);
    $auto_detect->{isdn}{$_}=$isdn->{$_} foreach qw(description vendor id driver card_type type);

    my $netc={};
    my $intf={};
    any::setup_thiskind_backend('net', undef);
    my @all_cards = netconnect::conf_network_card_backend ('', undef, undef, undef, undef, undef, undef);
    map { netconnect::adsl_detect("", $_->[0]) ? $auto_detect->{adsl}=$_->[0] : $auto_detect->{lan}{$_->[0]}=$_->[1]; } @all_cards;
    my $modem={};
    netconnect::modem_detect_backend($modem);#, $mouse);
    $modem->{device} and $auto_detect->{modem}=$modem->{device};
}