# description -- lintian check script -*- perl -*-

# Copyright (C) 1998 Christian Schwarz
#
# 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., 51 Franklin St, Fifth Floor, Boston,
# MA 02110-1301, USA.

package Lintian::description;
use strict;
use Tags;
use Util;

sub run {

my $pkg = shift;
my $type = shift;

my $ppkg = quotemeta($pkg);
my $cf = "fields/description";
my $tabs = 0;
my $lines = 0;
my $template = 0;
my $unindented_list = 0;
my $synopsis;

# description?
unless (-f $cf) {
    tag "package-has-no-description", "";
    return 0;
}

open(IN,$cf) or fail("cannot open $cf for reading: $!");

# 1st line contains synopsis
chop($synopsis = <IN>);

if ($synopsis =~ m/^\s*$/) {
    tag "description-synopsis-is-empty", "";
} else {
    if ($synopsis =~ m/^\s/) {
	tag "description-synopsis-has-leading-spaces", "";
    }
    if ($synopsis =~ m/^\s*$ppkg\b/i) {
	tag "description-starts-with-package-name", "";
    }
    if ($synopsis =~ m/\.\s*$/) {
	tag "description-synopsis-might-not-be-phrased-properly", "";
    }
    if ($synopsis =~ m/\t/) {
	tag "description-contains-tabs", "" unless $tabs++;
    }
    if (length($synopsis) >= 80) {
	tag "description-too-long", "";
    }
    if ($synopsis =~ m/^\s*missing\s*$/i) {
	tag "description-is-debmake-template", "" unless $template++;
    } elsif ($synopsis =~ m/<insert up to 60 chars description>/) {
	tag "description-is-dh_make-template", "" unless $template++;
    }
}

while (<IN>) {
    chop;
    next if m/^\s*$/o;
    next if m/^\.\s*$/o;

    if ($lines == 0) {
	my $firstline = lc $_;
	my $lsyn = lc $synopsis;
	if ($firstline =~ /^\Q$lsyn\E$/) {
	    tag "description-synopsis-is-duplicated", "";
	} else {
	    $firstline =~ s/[^a-zA-Z0-9]+//g;
	    $lsyn =~ s/[^a-zA-Z0-9]+//g;
	    if ($firstline eq $lsyn) {
		tag "description-synopsis-is-duplicated", "";
	    }
	}
    }

    $lines++;

    if (m/^\.\s*\S/o) {
	tag "description-contains-invalid-control-statement", "";
    } elsif (m/^[\-\*]/o) {
	# Print it only the second time.  Just one is not enough to be sure that
	# it's a list, and after the second there's no need to repeat it.
	tag "possible-unindented-list-in-extended-description", "" if $unindented_list++ == 2;
    }

    if (m/\t/o) {
	tag "description-contains-tabs", "" unless $tabs++;
    }

    if ($lines == 1) {
	# checks for the first line of the extended description:
	if (m/^\s/o) {
	    tag "description-starts-with-leading-spaces", "";
	}
	if (m/^\s*missing\s*$/oi) {
	    tag "description-is-debmake-template", "" unless $template++;
	} elsif (m/<insert long description, indented with spaces>/) {
	    tag "description-is-dh_make-template", "" unless $template++;
	}
    }

    if (length($_) >= 80) {
	tag "extended-description-line-too-long", "";
    }
}
close(IN);

if ($lines == 0) {
    tag "extended-description-is-empty", "" unless $type eq 'udeb';
}

}

1;
