#!/usr/bin/perl
# standards-version -- lintian check script

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

%valid_standard = (
		   '2.4.1', 1,
		   '2.4.0', 1,
		   '2.3.0', 1,
		   '2.2.0', 2,
		   '2.1.3', 2,
		   '2.1.2', 2,
		   '2.1.1', 2,
		   '2.1.0', 2,
		   '2.0.1', 2,
		   '2.0.0', 2,
		   '0.2.1', 2,
		   '0.2.0', 2,
		  );

# version lintian is programmed for
$MAJOR = 2;
$MINOR = 4;
$PATCH = 1;

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

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

$std = "fields/standards-version";
# Standards-Version?
if (not -f $std) {
    print "E: $pkg $type: no-standards-version-field\n";
    exit 0;
}

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

unless (/^\s*(\d+\.\d+\.\d+)\s*$/o or /^\s*(\d+\.\d+\.\d+)\.\d+\s*$/o) {
    # invalid standard
    print "E: $pkg $type: invalid-standards-version $_\n";
    exit 0;
}
$stdver = $1;

if ($valid_standard{$stdver} == 1) {
    # ok.
} elsif ($valid_standard{$stdver} == 2) {
    # old standard
    print "W: $pkg $type: ancient-standards-version $_\n";
} else {
    # unknown standard.  perhaps newer?
    /^\s*(\d+)\.(\d+)\.(\d+)/;
    ($major, $minor, $patch) = ($1, $2, $3);
    if (($major > $MAJOR) or
	($major == $MAJOR and $minor > $MINOR) or
	($major == $MAJOR and $minor == $MINOR and $patch > $PATCH)) {
	print "E: $pkg $type: newer-standards-version $_\n";
    } else {
	# invalid standard
	print "E: $pkg $type: invalid-standards-version $_\n";
    }
}

exit 0;

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

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