#! /usr/bin/perl -w
#                              -*- Mode: Perl -*- 
# kpkg-vercheck --- 
# Author           : Manoj Srivastava ( srivasta@tiamat.datasync.com ) 
# Created On       : Fri Nov  7 13:14:25 1997
# Created On Node  : tiamat.datasync.com
# Last Modified By : Manoj Srivastava
# Last Modified On : Sun Aug 20 20:27:50 2006
# Last Machine Used: glaurung.internal.golden-gryphon.com
# Update Count     : 24
# Status           : Unknown, Use with caution!
# HISTORY          : 
# Description      : 
# 
# 
require 5.002;

use strict;
use diagnostics;
use vars qw($MYNAME $Author $AuthorMail $Version);

=head1 NAME

kpkg-vercheck - Test if a package version is valid

=cut

($MYNAME     = $main::0) =~ s|.*/||;
$Author      = "Manoj Srivastava";
$AuthorMail  = "srivasta\@debian.org";
$Version     = '$Revision: 1.3 $';

=head1 SYNOPSIS

 usage: kpkg-vercheck version-number 

=cut

=head1 DESCRIPTION

This manual page explains the  Debian  B<kpkg-vercheck>  utility,
which  is  used  to check whether a package version follows the
directives in chapter 5 of the Debian packaging manual

=cut

=head1 Version numbering

Every package has a version number, in its Version control file field.
dpkg imposes an ordering on version numbers, so that it can tell
whether packages are being up- or downgraded and so that dselect can
tell whether a package it finds available is newer than the one
installed on the system. The version number format has the most
significant parts (as far as comparison is concerned) at the
beginning.

The version number format is: [epoch:]upstream-version[-debian-revision].

=cut
  
sub main {
  my $version = $ARGV[0];
  my $have_epochs = 0;
  my $have_debrev = 0;
  my $upstream_pat = "";
  
=head2 epoch

This is a single unsigned integer, which should usually be small. It
may be omitted, in which case zero is assumed. If it is omitted then
the upstream-version may not contain any colons.

=cut

  warn "nothing to work on --- version is empty [$version]" unless $version;

  # Test if we have epochs
  if ($version =~ m/\:/og) {
    $have_epochs = 1;
    # The epoch is a single unsigned integer
    if ($version =~ m/\s*\d+:(\S+)/o) {
      $version = $1;		# remove the epoch
    } 
    else {
      #This is an error.
      print "The epoch should be a simple integer in $version.\n";
      exit 1;
    }
  }

=head2 debian-revision

This part of the version represents the version of the modifications
that were made to the package to make it a Debian binary package. It
is optional; if it is not present then the upstream-version may not
contain a hyphen. This format represents the case where a piece of
software was written specifically to be turned into a Debian binary
package, and so there is only one "debianization" of it and therefore
no revision indication is required.

dpkg will break the upstream-version and debian-revision apart
at the last hyphen in the string. 

The debian-revision may contain only alphanumerics and the
characters + and . (plus and full stop).

=cut

  # Test and remove the debian revision
  if ($version =~ m/\-/o) {
    $have_debrev = 1;
    if ($version =~ m/(.*)-[A-Za-z0-9\~\+\.]+$/o) {
      $version = $1;		# remove the debian version
    }
    else {
      #This is an error.
      print "The Debian revision fails to match (.*)-[A-Za-z0-9\\~\\+\\.]+\$ in $version.\n";
      exit 1;
    }
  }

=head2 upstream-version

This is the main part of the version. It is usually version number of
the original (``upstream'') package of which the .deb file has been
made, if this is applicable. The upstream-version portion of the
version number is mandatory.

The upstream-version may contain only alphanumerics and the characters
+ . - : (full stop, plus, hyphen, colon) and should start with a
digit. If there is no debian-revision then hyphens are not allowed; if
there is no epoch then colons are not allowed.

=cut

  #Check out the main version
  if ($have_epochs) {
    if ($have_debrev) {
      $upstream_pat = '^[A-Za-z0-9\~\.\+\:\-]+$'; # Note the : and the -
    }
    else {
      $upstream_pat = '^[A-Za-z0-9\~\.\+\:]+$'; # Note the : 
    }
  }
  else {
    if ($have_debrev) {
      $upstream_pat = '^[A-Za-z0-9\~\.\+\-]+$'; # Note the -
    }
    else {
      $upstream_pat = '^[A-Za-z0-9\~\.\+\-]+$';
    }
  }

  if ($version =~ m/$upstream_pat/o) {
    if ($version =~ m/[0-9]+/o) {
      print "YES\n";
      exit 0;
    }
    else {
      print "The upstream version $version does not contain a digit\n";
      exit 1;
    }
  }
  else {
    #This is an error.
    print "The upstream version fails to match $upstream_pat in $version\n";
    exit 1;
  }
  # Not-reached
}


## Now just call main
&main();


=head1 B<SEE ALSO>

B<dpkg>(5), B<dpkg-deb>(1), B<dpkg-source>(1),  B<dpkg-parsechangelogs>(1),
B<The Debian Packaging manual>.

=cut


=head1 BUGS

None Known so far.

=cut

=head1 AUTHOR

This  was  written by Manoj Srivastava <srivasta@debian.org>, for the
Debian GNU/Linux system. 

=cut


exit 0;
__END__
