#!/usr/bin/perl

=pod

=head1 Name

rc-zenity.pl - use SOAP to get info on a bug

=cut

=head1 Copyright and Licence

 Copyright (C) 2008  Neil Williams <codehelp@debian.org>

 This package 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 3 of the License, 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, see <http://www.gnu.org/licenses/>.

=cut

=head1 Dependencies

RC-Zenity depends on libsoap-lite-perl and zenity.
It could use whiptail with some modifications, see emrecent in
emdebian-tools for an example of dual whiptail | zenity support.

=cut

use File::Basename;
use SOAP::Lite;
use strict;
use warnings;
use vars qw( $our_version $verbose $progname $soap $fixed_ver %buglist
 $bugs $package $date $number $stats $str $subject $severity $modified
 $originator $pending $fixed_date $cache $upload $policy $mode
 $height $width $text);

$progname = basename($0);
$our_version = "0.0.1";

=head1 Description

Simple script to collate some data about an RC bug.

Initially loads a zenity text entry window asking for a bug number. The
bug is queried, data about the package is collated and displayed in a
new zenity question window. Click Cancel to review a different bug,
click OK to load the PTS and the bug report in 'sensible-browser' and
continue reviewing bugs.

Click Cancel in the bug number entry dialog to end the program.

(Although designed for RC bugs, any bug can actually be reviewed.)

It may be helpful to ensure that the BROWSER environment variable is
set to support opening new URLs in new tabs if your chosen browser supports
tabs. e.g. in your ~/.bashrc
 export BROWSER="epiphany-browser -n"

=cut

sub usageversion {
    print(STDERR <<END)
$progname version $our_version

Simple script to collate some data about an RC bug.

Initially loads a zenity text entry window asking for a bug number. The
bug is queried, data about the package is collated and displayed in a
new zenity question window. Click Cancel to review a different bug,
click OK to load the PTS and the bug report in 'sensible-browser' and
continue reviewing bugs.

Click Cancel in the bug number entry dialog to end the program.

END
        || die "$0: failed to write usage: $!\n";
exit 0;
}

=head1 Options

RC-Zenity only takes one option - a bug number.

=cut

while( @ARGV ) {
	$_= shift( @ARGV );
	last if m/^--$/;
	if (!/^-/) {
		unshift(@ARGV,$_);
		last;
	}
	elsif (/^(-\?|-h|--help|--version)$/) {
		&usageversion();
		exit( 0 );
	}
}

if (! $ARGV[0])
{
	$number = `zenity --entry --title "RC-Zenity" --text "Enter the next bug number"`;
}
else
{
	$number = $ARGV[0];
}

if ($number !~ /^[0-9]+$/)
{
	&usageversion;
	exit 1;
}
chomp $number;
$soap = SOAP::Lite->uri('Debbugs/SOAP')->proxy('http://bugs.debian.org/cgi-bin/soap.cgi');
$stats = $soap->get_status($number)->result->{$number};
$package = $stats->{'package'};
$date = $stats->{'date'};
$date = `date -d\@$date`;
$subject = $stats->{'subject'};
my $str = `apt-cache show $package`;
$cache = join ('', grep /Description/, split('\n',$str)) . "\n";
$cache .= join ('', grep /Maintainer/, split('\n',$str)) . "\n";
$cache .= join ('', grep /Tag: /, split('\n',$str)) . "\n";
$policy = `apt-cache policy $package`;
$upload = `who-uploads --date -M 1 $package`;
$pending = $stats->{'pending'};
$originator = $stats->{'originator'};
$modified = $stats->{'modified'};
$modified = ((defined $modified) and $modified =~ /^[0-9]+$/) ? `date -d\@$modified` : "";
$severity = $stats->{'severity'};
$fixed_date = $stats->{'fixed_date'};
$fixed_date = ($fixed_date =~ /^[0-9]+$/) ? `date -d\@$fixed_date` : undef;

$mode = "--question";
# make these configurable
$height = "400";
$width = "300";

$text = "Bug Number: $number\nPackage: $package\n";
$text .= "$subject\nFiled: $date\n";
$text .= "FIXED on $fixed_date\n" if (defined $fixed_date);
if ($pending !~ /^pending-fixed$/)
{
	$text .= $cache;
	$text .= $policy;
	$text .= $upload;
}
else
{
	$text .= "$number is tagged as pending.\n";
}
$text =~ s/\Q@\E/\@/g;
$text =~ s/\Q<\E/&lt;/g;
$text =~ s/\Q>\E/&gt;/g;
$text .= "Click OK to load the bug and PTS in 'sensible-browser'";
$text .= " or Cancel to see the next bug.";
my $dgv = system (qq/zenity $mode --title $number --text "$text" $height $width /);
if ($dgv == 0)
{
	system ("sensible-browser http://bugs.debian.org/$number");
	system ("sensible-browser http://packages.qa.debian.org/$package");
}
exec ($0);
