#!/usr/bin/perl

# Drak Bug Report 
# Copyright (C) 2002 MandrakeSoft (daouda@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 strict;
use diagnostics;
use lib qw(/usr/lib/libDrakX);

use standalone;
use interactive;
use common;
use ugtk2 qw(:all);
use Config;


my $in = 'interactive'->vnew;

my $bugzilla_url = "http://drakbug.mandrakesoft.com";
my $version = "0.9.0";
my $prog;
my $incident = 0;

foreach (@ARGV) {
    next unless defined $_;
    /^--report$/ and $prog = shift @ARGV;
    /^--incident$/ and do { $incident = 1; $prog = shift @ARGV };
}

my $window = ugtk2->new('drakbug', center => 1);
my $window_g = $window->{window};
#$window->{rwindow}->set_policy($false,$false,$true);
$window->{rwindow}->set_border_width(5);
$window->{rwindow}->set_title(N("Mandrake Bug Report Tool"));
$window->{window}->signal_connect("delete_event", \&quit_global);

my $mdk_app = { 
	       N("Mandrake Control Center") => 'drakconf',
	       N("First Time Wizard") => 'drakfw',
	       N("Synchronization tool") => 'draksync',  
	       N("Standalone Tools") => ['adduserdrake', 'diskdrake', 'drakautoinst', 'drakbackup', 'drakboot', 'drakbug', 'drakfloppy', 'drakfont', 'drakgw', 'drakconnect', 'drakxservices', 'drakxtv', 'keyboardrake', 'logdrake', 'mousedrake', 'net_monitor', 'printerdrake', 'scannerdrake', 'drakfirewall', 'XFdrake'],
	       N("HardDrake") => 'harddrake2',
	       N("Mandrake Online") => 'mdkonline',
	       N("Menudrake") => 'menudrake',
	       N("Msec") => 'msec',
	       N("Remote Control") => 'rfbdrake',
	       N("Software Manager") => 'rpmdrake',
	       N("Urpmi") => 'urpmi',
	       N("Windows Migration tool") => 'transfugdrake',
	       N("Userdrake") => 'userdrake',
	       N("Configuration Wizards") => 'wizdrake',
	      };

my @generic_tool = keys %$mdk_app; 
my @all_drakxtools = @{ $mdk_app->{N("Standalone Tools")} };
push(@generic_tool,@all_drakxtools);

my $kernel_release = chomp_(`uname -r`) . "";

my $table = new Gtk2::Table(4,2, 'TRUE');
#$table->set_border_width(5);
$table->set_row_spacings(10);
$table->set_col_spacings(5);
$table->attach(new Gtk2::Label(N("Application:")), 0, 1, 0, 1, 'fill', 'fill',20,0);
$table->attach(new Gtk2::Label(N("Package: ")), 0, 1, 1, 2, 'fill', 'fill',0,0);
$table->attach(new Gtk2::Label(N("Kernel:")), 0, 1, 2, 3, 'fill', 'fill',0,0);
$table->attach(new Gtk2::Label(N("Release: ")), 0, 1, 3, 4, 'fill', 'fill',0,0);
$table->attach(my $comb_app = new Gtk2::Combo(), 1, 2, 0, 1, 'fill', 'fill',0,0);
$comb_app->set_size_request(270, undef);
$comb_app->set_popdown_strings("", sort(@generic_tool));
$table->attach(my $package = new Gtk2::Entry(), 1, 2, 1, 2, 'fill', 'fill',0,0);
$package->set_text("...");
$table->attach(my $kernel_rel = new Gtk2::Entry(), 1, 2, 2, 3, 'fill', 'fill',0,0);
$kernel_rel->set_text($kernel_release);
$table->attach(my $mdk_rel = new Gtk2::Entry(), 1, 2, 3, 4, 'fill', 'fill',0,0);
$mdk_rel->set_text(mandrake_release());

gtkpack2__(
	   gtkpack2__(my $vbx = new Gtk2::VBox(0,5),
		      gtkadd($table),
		      gtkpack(new Gtk2::HBox(0,0),
			      gtkpack(gtkset_justify(new Gtk2::Label(N("\n\nTo submit a bug report, click on the button report.\nThis will open  a web browser window  on https://drakbug.mandrakesoft.com\n where you'll find a form to fill in.The information displayed above will be \ntransferred to that server\n\n")), "left")),
			     ),
		      gtkpack(new Gtk2::HSeparator),
		      
		     ),
	  );

if (defined $prog) {
    update_app($prog); 
    $comb_app->entry->set_text($prog);
};

$comb_app->entry->signal_connect('changed', sub { update_app($comb_app->entry->get_text()) });
my $kernel = $kernel_rel->get_chars(0, -1);
my $hbx = new Gtk2::HBox(0,0);
my $Close_Button = new Gtk2::Button(N("Close"));
$Close_Button->signal_connect(clicked => sub { ugtk2->exit(0) });
$hbx->pack_start($Close_Button,0,0,0);

my $Report_Button = new Gtk2::Button(N("Report"));
$Report_Button->signal_connect(clicked => sub { my $options = "mdkbugreport=1";
						$options .= "&incident=1" if $incident;
						my $p = $package->get_text(); my $k = $kernel_rel->get_text(); (my $r = parse_release()) =~ s/\s//;
						$options .= "&package=$p" if $p =~ /mdk/;
						$options .= "&kernel=$k";
						$options .= "&version=$r";
						print($bugzilla_url . "?" . $options . "\n");
						connect_bugzilla($bugzilla_url."?".$options) });
my $help_button = new Gtk2::Button(" ".N("Help")." ");
$help_button->signal_connect(clicked => sub { system("drakhelp https://qa.mandrakesoft.com &") });
$hbx->pack_end($Report_Button,0,0,0);
$hbx->pack_end($help_button,0,0,5);
$vbx->pack_start($hbx,0,0,0);
$window->{window}->add($vbx);

$window->{window}->show_all();
$window->main;
ugtk2->exit(0);

sub update_app {
    my ($text) = @_;
    my $app_choice;
    $ENV{PATH} = "/sbin:/usr/sbin:$ENV{PATH}";
    if (member($text,@all_drakxtools) || $text eq N("Standalone Tools")) {
	$app_choice = chomp_(`rpm -q drakxtools`);
    } elsif (member($text, keys %$mdk_app) && $text ne N("Standalone Tools")) {	
	$app_choice = get_package($mdk_app->{$text});
    } else {
	LOOP: while (my ($key, $value) = each %$mdk_app) {
	    next if $key eq N("Standalone Tools");
	    if ($value eq $text) { 
		$app_choice = get_package($text);
		$prog = $key; 
		last LOOP;
	    }
	}
	
    }
    $app_choice ? $package->set_text($app_choice) : $package->set_text(N("Not installed"));
}

my %packages;

sub get_package {
    my ($executable) = @_;
    my ($rpm_package, $which_app);
    $rpm_package = $packages{$executable};
    if (!defined $rpm_package) {
        $which_app = chomp_(`which '$executable' 2> /dev/null`);
        # deush, rpm can takes some time aka it'll sleeps if something has opened rpm db !
        $rpm_package = $which_app eq "" ? N("Package not installed") : chomp_(`rpm -qf '$which_app' 2>&1`);
        $packages{$executable} = $rpm_package;
    }
    $rpm_package;
}

sub parse_release() {
    my ($rel) = mandrake_release() =~ /release\s(\S+\s\(.*\))/;
    $rel;
}

sub connect_bugzilla {
    my ($url) = @_;
    my $_w = $in->wait_message('', N("connecting to Bugzilla wizard ...")); 
    sleep(3);
    exec $ENV{BROWSER},$url if exists $ENV{BROWSER};
    my @browser = qw(mozilla konqueror galeon);
    foreach (@browser) {
	if (-e "/usr/bin/$_") { log::explanations("Contacting $url with $_\n "); exec $_,$url } 
    }
    $in->ask_warn('', N("No browser available! Please install one"));
}

sub quit_global() {
    ugtk2->exit(0);
}