#!/usr/bin/perl
# debdiff -- Lintian check script

# Copyright (C) 1998 by Richard Braakman
# 
# 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, you can find it on the World Wide
# Web at http://www.gnu.org/copyleft/gpl.html, or write to the Free
# Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
# MA 02111-1307, USA.

($#ARGV == 1) or fail("syntax: field <pkg> <type>");
$pkg = shift;
$type = shift;

open(STAT, "diffstat") or fail("cannot open diffstat file: $!");
     
while (<STAT>) {
    chop;
    
    my ($file) = /^\s+(.*?)\s+\|/
	or fail("syntax error in diffstat file: $_");
    
    tag_warn("patch-failure-file-in-diff", $file)
	if ($file =~ /\.(orig|rej)$/);
}
close(STAT) or fail("error reading diffstat file: $!");

exit 0;

# ---------------------------------

sub tag_warn {
    my $tag = shift;
    if ($#_ >= 0) {
        # We can't have newlines in a tag message, so turn them into \n
        map { s,\n,\\n, } @_;
        my $args = join(' ', @_);
        print "W: $pkg $type: $tag $args\n";
    } else {
        print "W: $pkg $type: $tag\n";
    }
}

sub fail {
    if ($_[0]) {
	print STDERR "error: $_[0]\n";
    } elsif ($!) {
	print STDERR "error: $!\n";
    } else {
	print STDERR "error.\n";
    }
    exit 1;
}
