#!/usr/bin/perl
# AQ
# 20121121

BEGIN { $ENV{LC_ALL} = "en_GB"; }

use utf8;
use locale ':not_characters';
use Glib qw/TRUE FALSE/;
use Gtk2 '-init';

my $seconds = 15;

Gtk2::Rc->parse_string(<<__);
style "superbig" {
    font_name ="Droid Sans 14"
}

style "normal" {
    font_name = "Droid Sans 9"
}

style "error" {
    font_name ="Droid Sans 12"
    text[NORMAL] = "#FF0000"
    fg[NORMAL] = "#FF0000"
}

widget "*" style "normal"
widget "*error*" style "error"
widget "*title*" style "superbig"

__

sub callback {
	my $widget = shift;
	my $data   = shift;
	if ($data =~ m/setup/) {
	$seconds = 10000;
	$window->resize(800,200);
	$window->set_position(GTK_WIN_POS_CENTER);
	$box1->hide;
	
	$button_exit = Gtk2::Button->new("Exit");
	$image_exit  = Gtk2::Image->new_from_file('/usr/share/icons/Faenza/actions/48/application-exit.png');
	$button_exit->set_image ($image_exit);
	$button_exit->set_image_position ('top');
	$button_exit->signal_connect("clicked" => \&on_stop);
	$table->attach_defaults($button_exit, 0, 6, 3, 4);
	$image_exit->show;
	$button_exit->show;

	$box2 = Gtk2::HBox->new (TRUE, 2);
	$table->attach_defaults($box2, 0, 6, 0, 3);

	$button_nm = Gtk2::Button->new("Network connections");
	$image_nm  = Gtk2::Image->new_from_file('/usr/share/icons/Faenza/apps/48/preferences-system-network.png');
	$button_nm->set_image ($image_nm);
	$button_nm->set_image_position ('top');
	$button_nm->signal_connect("clicked" => \&callback, "networkmanager");
	$box2->pack_start($button_nm, TRUE, TRUE, 0);
	$image_nm->show;
	$button_nm->show;
	
	$button_modem1 = Gtk2::Button->new("GPRS connection setup");
	$image_modem1  = Gtk2::Image->new_from_file('/usr/share/icons/Faenza/devices/48/network-wireless.png');
	$button_modem1->set_image ($image_modem1);
	$button_modem1->set_image_position ('top');
	$button_modem1->signal_connect("clicked" => \&callback, "gprsmanual");
	$box2->pack_start($button_modem1, TRUE, TRUE, 0);
	$image_modem1->show;
	$button_modem1->show;

	$button_modem2 = Gtk2::Button->new("GPRS connection search");
	$image_modem2  = Gtk2::Image->new_from_file('/usr/share/icons/Faenza/devices/48/network-wireless.png');
	$button_modem2->set_image ($image_modem2);
	$button_modem2->set_image_position ('top');
	$button_modem2->signal_connect("clicked" => \&callback, "gprsauto");
	$box2->pack_start($button_modem2, TRUE, TRUE, 0);
	$image_modem2->show;
	$button_modem2->show;

	$button_conf = Gtk2::Button->new("Configure payment software");
	$image_conf  = Gtk2::Image->new_from_file('/usr/share/icons/Faenza/categories/48/package_system.png');
	$button_conf->set_image ($image_conf);
	$button_conf->set_image_position ('top');
	$button_conf->signal_connect("clicked" => \&callback, "configurator");
	$box2->pack_start($button_conf, TRUE, TRUE, 0);
	$image_conf->show;
	$button_conf->show;

	$box2->show;
	}
	
	if ($data =~ m/networkmanager/) {
		system("/bin/nm-connection-editor");
	}
	if ($data =~ m/gprsmanual/) {
		system("sudo /home/terminal/atm/bin/g-ppp");
	}
	if ($data =~ m/gprsauto/) {
		system("LC_ALL=en_GB.UTF-8 xfce4-terminal -e \"sudo /home/terminal/atm/bin/gprsconfig_alternate\" -T \"GPRS connection setup\" --hide-toolbar --hide-menubar");
	}
	if ($data =~ m/configurator/) {
		system("LC_ALL=en_GB.UTF-8 ~/atm/configure.sh");
	}

}

sub progress_timeout {
	$seconds--;
	if ($seconds eq 0) {Gtk2->main_quit;}
        return TRUE;
}

sub on_stop {
	Gtk2->main_quit;
}

Gtk2->init;

$window = Gtk2::Window->new;
$window->signal_connect("destroy" => \&on_stop);
$window->signal_connect("delete-event" => \&on_stop);
$window->set_default_size(800,200);
$window->set_position (GTK_WIN_POS_CENTER);
$window->set_title("Terminal setup wizard");
$window->activate_focus;
$window->set_border_width(3);

$table = Gtk2::Table->new(4, 6, TRUE);
$table->set_col_spacings (5);
$table->set_row_spacings (5);
$window->add($table);

$box1 = Gtk2::HBox->new (TRUE, 2);
$table->attach_defaults($box1, 0, 6, 0, 4);

$button = Gtk2::Button->new("Exit");
$image_exit  = Gtk2::Image->new_from_file('/usr/share/icons/Faenza/actions/48/application-exit.png');
$button->set_image ($image_exit);
$button->set_image_position ('top');
$button->signal_connect("clicked" => \&on_stop);
$box1->pack_start($button, TRUE, TRUE, 0);
$button->set_name("title1");
$image_exit->show;
$button->show;

$button2 = Gtk2::Button->new("Terminal wizard");
$image_conf  = Gtk2::Image->new_from_file('/usr/share/icons/Faenza/categories/48/applications-system.png');
$button2->set_image ($image_conf);
$button2->set_image_position ('top');
$button2->signal_connect("clicked" => \&callback, "setup");
$box1->pack_start($button2, TRUE, TRUE, 0);
$button2->set_name("title2");
$image_conf->show;
$button2->show;

$box1->show;
$table->show;
$window->show;

my $timer = Glib::Timeout->add(1000, \&progress_timeout);

Gtk2->main;
