# standards-version -- lintian check script -*- perl -*-

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

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

sub run {

# 1 means current
# 2 means old
# 3 means ancient
my %valid_standard = (
		   '3.7.3', 1,
		   '3.7.2', 2,
		   '3.7.1', 2,
		   '3.7.0', 2,
		   '3.6.2', 2,
		   '3.6.1', 3,
		   '3.6.0', 3,
		   '3.5.10', 3,
		   '3.5.9', 3,
		   '3.5.8', 3,
		   '3.5.7', 3,
		   '3.5.6', 3,
		   '3.5.5', 3,
		   '3.5.4', 3,
		   '3.5.3', 3,
		   '3.5.2', 3,
		   '3.5.1', 3,
		   '3.5.0', 3,
                   '3.2.1', 3,
                   '3.2.0', 3,
		   '3.1.1', 3,
		   '3.1.0', 3,
		   '3.0.1', 3,
		   '3.0.0', 3,
		   '2.5.1', 3,
		   '2.5.0', 3,
		   '2.4.1', 3,
		   '2.4.0', 3,
		   '2.3.0', 3,
		   '2.2.0', 3,
		   '2.1.3', 3,
		   '2.1.2', 3,
		   '2.1.1', 3,
		   '2.1.0', 3,
		   '2.0.1', 3,
		   '2.0.0', 3,
		   '0.2.1', 3,
		   '0.2.0', 3,
		  );

# version lintian is programmed for. ($valid_standard[0]?)
my $MAJOR = 3;
my $MINOR = 7;
my $PATCH = 3;

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

unless ( -d "fields" ) {
  fail("fields/ directory missing in lintian laboratory");
};

my $std = "fields/standards-version";
# Standards-Version?
if (not -f $std) {
    # udebs aren't required to conform to policy, so they don't need
    # Standards-Version. (If they have it, though, it should be valid.)
    tag "no-standards-version-field", "" unless $type eq 'udeb';
    return 0;
}

open(IN, '<', $std) or fail("cannot open $std for reading: $!");
chop($_ = <IN>);
close(IN);

unless (m/^\s*(\d+\.\d+\.\d+)(?:\.\d+)?\s*$/) {
    # invalid standard
    tag "invalid-standards-version", "$_";
    return 0;
}

my $stdver = $1;
my ($major, $minor, $patch) = $stdver =~ m/^(\d+)\.(\d+)\.(\d+)/;

if (not exists $valid_standard{$stdver}) {
    # unknown standard.  perhaps newer?
    if (($major > $MAJOR) or
	($major == $MAJOR and $minor > $MINOR) or
	($major == $MAJOR and $minor == $MINOR and $patch > $PATCH)) {
	tag "newer-standards-version", "$_ (current is $MAJOR.$MINOR.$PATCH)";
    } else {
	# invalid standard
	tag "invalid-standards-version", "$_";
    }
} elsif ($valid_standard{$stdver} == 2) {
    # old standard
    tag "out-of-date-standards-version", "$_ (current is $MAJOR.$MINOR.$PATCH)";
} elsif ($valid_standard{$stdver} == 3) {
    # OK, now this is ancient.
    tag "ancient-standards-version", "$_ (current is $MAJOR.$MINOR.$PATCH)";
} else { # looks valid ......
    if (($major == 3 and $minor == 0) or $major < 3) {
	if (-f "fields/build-depends" or
	    -f "fields/build-depends-indep" or
	    -f "fields/build-conflicts" or
	    -f "fields/build-conflicts-indep") {
	    tag "package-declares-source-relation-but-has-older-standards-version", "$_ < 3.1.0";
	}
    }
}

}

1;

# vim: syntax=perl
