#!/usr/bin/perl -w
#
# lintian-info -- transform lintian tags into descriptive text
#
# Copyright (C) 1998 Christian Schwarz and Richard Braakman
#
# This program is free software.  It is distributed 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., 51 Franklin St, Fifth Floor, Boston,
# MA 02110-1301, USA.

use strict;

use Getopt::Long;

# turn file buffering off:
$| = 1;

BEGIN {
  # determine LINTIAN_ROOT
  my $LINTIAN_ROOT = $ENV{'LINTIAN_ROOT'} || '/usr/share/lintian';
  $ENV{'LINTIAN_ROOT'} = $LINTIAN_ROOT
    unless exists $ENV{'LINTIAN_ROOT'};
}

# import perl libraries
use lib "$ENV{'LINTIAN_ROOT'}/lib";
use Read_taginfo;

my %already_displayed = ();

my %tag_info = %{read_tag_info()};

my ($annotate, $tags);
Getopt::Long::config('bundling', 'no_getopt_compat', 'no_auto_abbrev');
GetOptions('annotate|a' => \$annotate, 'tags|t' => \$tags)
    or die("error parsing options\n");

# If tag mode was specified, read the arguments as tags and display the
# descriptions for each one.  (We don't currently display the severity,
# although that would be nice.)
my $unknown;
if ($tags) {
    for my $tag (@ARGV) {
        print "N: $tag\n";
        print "N:\n";
        if (exists $tag_info{$tag}) {
            print wrap_paragraphs('N:   ', $tag_info{$tag});
        } else {
            print "N:   Unknown tag.\n";
            $unknown = 1;
        }
        print "N:\n";
    }
    exit ($unknown ? 1 : 0);
}

# Otherwise, read input files or STDIN, watch for tags, and add descriptions
# whenever we see one, can, and haven't already explained that tag.  Strip off
# color and HTML sequences.
while (<>) {
    print;
    chomp;
    next if /^\s*$/;
    s/\e[\[\d;]*m//g;
    s/<span style=\"[^\"]+\">//g;
    s,</span>,,g;

    my ($type, $pkg);
    my @pieces = split(/:\s+/);
    if ($annotate) {
        $type = shift @pieces if ($pieces[0] =~ /^\w$/);
        $pkg = shift @pieces if ($pieces[0] =~ /^\S+( (binary|udeb))?$/);
    } else {
	$type = shift @pieces;
	$pkg = shift @pieces;
    }
    if ($annotate or (defined $type and $type =~ m/^[OEWIX]$/)) {
	my $tag = shift @pieces;
	next if not defined $tag;
	($tag) = split(/\s+/, $tag, 2);

	next if not exists $tag_info{$tag} or $already_displayed{$tag}++;
	print "N:\n";
	print wrap_paragraphs('N:   ',$tag_info{$tag});
	print "N:\n";
    }
}

exit 0;

# Local Variables:
# indent-tabs-mode: t
# cperl-indent-level: 4
# End:
# vim: syntax=perl sw=4 ts=8
