#!/usr/bin/perl
# description -- lintian check script

# Copyright (C) 1998 by 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., 59 Temple Place - Suite 330, Boston,
# MA 02111-1307, USA.

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

$ppkg = quotemeta($pkg);

$cf = "fields/description";
# description?
unless (-f $cf) {
  print "E: $pkg $type: package-has-no-description\n";
  exit 0;
}

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

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

if ($synopsis =~ /^\s*$/) {
  print "E: $pkg $type: description-synopsis-is-empty\n";
} else {
  if ($synopsis =~ /^\s/) {
    print "E: $pkg $type: description-synopsis-has-leading-spaces\n";
  }
  if ($synopsis =~ /^\s*$ppkg\b/i) {
    print "E: $pkg $type: description-starts-with-package-name\n";
  }
  if ($synopsis =~ /\t/) {
    print "E: $pkg $type: description-contains-tabs\n" unless $tabs++;
  }
  if (length($synopsis) >= 80) {
    print "E: $pkg $type: description-too-long\n";
  }
  if ($synopsis =~ /^\s*missing\s*$/i) {
    print "E: $pkg $type: description-is-debmake-template\n" unless $template++;
  }
}

$lines = 0;

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

  $lines++;

  if (/^\.\s*\S/o) {
    print "E: $pkg $type: description-contains-invalid-control-statement\n";
  }
  elsif (/^[\-\*]/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.
    print "W: $pkg $type: possible-unindented-list-in-extended-description\n" if $unindented_list++ == 2;
  }

  if (/\t/o) {
    print "E: $pkg $type: description-contains-tabs\n" unless $tabs++;
  }

  if ($lines == 1) {
    # checks for the first line of the extended description:
    if (/^\s/o) {
      print "W: $pkg $type: description-starts-with-leading-spaces\n";
    }
    if (/^\s*missing\s*$/oi) {
      print "E: $pkg $type: description-is-debmake-template\n" unless $template++;
    }
  }
}
close(IN);

if ($lines == 0) {
  print "E: $pkg $type: extended-description-is-empty\n";
}

exit 0;

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

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