#!/usr/bin/perl -w
#
# tagpending: Parse a Debian changelog for a list of bugs closed
# and tag any that are not already pending as such.
#
# The original shell version of tagpending was written by Joshua Kwan
# and is Copyright 2004 Joshua Kwan <joshk@triplehelix.org>
# with changes copyright 2004-07 by their respective authors.
#
# This version is
#   Copyright 2008 Adam D. Barratt
#
# 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 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, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

use strict;
use warnings;
use Getopt::Long qw(:config gnu_getopt);
use File::Basename;
use lib '/usr/share/devscripts';
use Devscripts::Debbugs;

sub bugs_info;

my $progname = basename($0);

my ($opt_help, $opt_version, $opt_verbose, $opt_noact, $opt_silent);
my ($opt_online, $opt_confirm, $opt_to, $opt_wnpp, $opt_comments);
my $opt_interactive;

# Default options
$opt_silent = 0;
$opt_verbose = 0;
$opt_online = 1;
$opt_noact = 0;
$opt_confirm = 0;
$opt_wnpp = 0;
$opt_to = '';
$opt_comments = 1;
$opt_interactive = 0;

GetOptions("help|h" => \$opt_help,
	   "version" => \$opt_version,
	   "verbose|v!" => \$opt_verbose,
	   "noact|n" => \$opt_noact,
	   "comments!" => \$opt_comments,
	   "silent|s" => \$opt_silent,
	   "force|f" => sub { $opt_online = 0; },
	   "confirm|c" => \$opt_confirm,
	   "to|t=s" => \$opt_to,
	   "wnpp|w" => \$opt_wnpp,
	   "interactive|i" => \$opt_interactive,
           )
    or die "Usage: $progname [options]\nRun $progname --help for more details\n";

$opt_to = "-v$opt_to" if $opt_to;

if ($opt_help) {
    help(); exit 0;
} elsif ($opt_version) {
    version(); exit 0;
}

if ($opt_verbose and $opt_silent) {
    die "$progname error: --silent and --verbose contradict each other\n";
}

=head1 NAME

tagpending - tags bugs that are to be closed in the latest changelog as pending

=head1 SYNOPSIS

B<tagpending> [I<options>]

=head1 DESCRIPTION

B<tagpending> parses debian/changelog to determine
which bugs would be closed if the package were uploaded. Each bug is
then marked as pending, using B<bts>(1) if it is not already so.

=head1 OPTIONS

=over 4

=item B<-n>, B<--noact>

Check whether any bugs require tagging, but do not actually do so.

=item B<-s>, B<--silent>

Do not output any messages.

=item B<-v>, B<--verbose>

List each bug checked and tagged in turn.

=item B<-f>, B<--force>

Do not query the BTS, but (re)tag all bugs closed in the changelog.

=item B<--comments>

Include the changelog header line and the entries relating to the tagged
bugs as comments in the generated mail.  This is the default.

Note that when used in combination with B<--to>, the header line output
will always be that of the most recent version.

=item B<--no-comments>

Do not include changelog entries in the generated mail.

=item B<-c>, B<--confirm>

Tag bugs as both confirmed and pending.

=item B<-t>, B<--to> I<version>

Parse changelogs for all versions strictly greater than I<version>.

Equivalent to B<dpkg-parsechangelog>'s B<-v> option.

=item B<-i>, B<--interactive>

Display the message which would be sent to the BTS and, except when
B<--noact> was used, prompt for confirmation before sending it.

=item B<-w>, B<--wnpp>

For each bug that does not appear to belong to the current package,
check whether it is filed against wnpp. If so, tag it. This allows e.g.
ITAs and ITPs closed in an upload to be tagged.

=back

=head1 SEE ALSO

B<bts>(1) and B<dpkg-parsechangelog>(1)

=cut

my $source;
my @closes;
my $in_changes=0;
my $changes='';
my $header='';

foreach my $file ("debian/changelog") {
    if (! -f $file) {
	die "$progname error: $file does not exist!\n";
    }
}

open PARSED, "dpkg-parsechangelog $opt_to |";

while (<PARSED>) {
    if (/^Source: (.*)/) {
	$source = $1;
    } elsif (/^Closes: (.*)$/) {
	@closes = split ' ', $1;
    } elsif (/^Changes: /) {
	$in_changes = 1;
    } elsif ($in_changes) {
	if ($header) {
	    next unless /^ {3}[^[]/;
	    $changes .= "\n" if $changes;
	    $changes .= $_;
	} else {
	    $header = $_;
	}
    }
}

close PARSED;

# Add a fake entry to the end of the recorded changes
# This makes the parsing of the changes simpler
$changes .= "   *";

my $pending;
my $open;

if ($opt_online) {
    if (!Devscripts::Debbugs::have_soap()) {
	die "$progname: The libsoap-lite-perl package is required for online operation; aborting.\n";
    }

    eval {
	$pending = Devscripts::Debbugs::select( "src:$source", "status:open", "status:forwarded", "tag:pending" );
	$open = Devscripts::Debbugs::select( "src:$source", "sta                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 