#! /usr/bin/perl -w

use Getopt::Std;

# This program checks that all the version numbers scattered
# throughout LVM match.  The data section at the end of this file
# contains lines which specify the locations of these version numbers.
# These specifications are of the form:
# <file>:<line>:<pattern>

# Where: 

# 'file' is the name of the file relative to the top of the LVM tree

# 'line' is the line number (numeric) or a regular expression that
#        uniquely selects the line.

# 'pattern' is a regular expression used to extract the version number
#        from the line.  After this pattern has run $1 (ie, the expression
#        contained within the first set of brackets) should contain the
#        version number.

# Run this program from the root of the LVM tree, an optional argument
# tells it what version number should be present.  If this argument
# isn't present it lists all version numbers;

# cd ~/develop/LVM
# scripts/check_version_numbers

# scripts/check_version_numbers 0.9.1_beta2

$err = getopts('t:d:hs');
if($err == 0) {
  $opt_h = 1;
}

$name = $0;
$name =~ s/(.*\/)(.*)/$2/;

if(defined($opt_h)) {
  die "usage: $name [-t Target -d Destination -h -s]\n\t-t\tVersion string we're looking for\n\t-d\tChange target to destination if -s flag is given\n\t-h\tthis help message\n\t-s\tSubstitute Destination for Target\n";
}

if(defined($opt_t)) {
  $target = $opt_t;
  if(defined($opt_s)) {
    if(defined($opt_d)) {
      $destination = $opt_d;
    }
    else {
      $opt_h = 1;
    }
  }
}
$ret = 0;
while(<DATA>) {
    chomp;
    next if(m/^\s*$/);

    ($file, $line, $pattern) = split(':', $_);

    if(!defined($file) || !defined($line) || !defined($pattern)) {
	print "Malformed line: '$_'\n";
	next;
    }

    open(IN, $file) or die("couldn't open '$file'\n");
    $match = 0;
    $count = 0;
    $number = ($line =~ /^[0-9]+$/) ? 1 : 0;


    while(!$match && ($in = <IN>)) {
	chomp $in;
	$count++;
	if($number) {
	    if($count == $line) {$match = 1;}
	} else {
	    if($in =~ m/$line/) {$match = 1;}
	}
    }
    if(!$match) {
	print "Couldn't find line matching $file:$line\n";
    } else {
        if(defined($opt_s)) {
           print "Not yet implemented\n";
	}
	else {
	if($in =~ m/$pattern/) {
	    if(defined($target)) {
		if(!($1 eq $target)) {
		    print "$file:$count:$1\n";
		    $ret = 1;
		}
	    } else {
		print "$file:$count:$1\n";
	    }
	} else {
	    print "couldn't match pattern on $file:$line:$pattern\n";
	}
      }
    }
    close(IN);
}

exit($ret);

__DATA__
README:1:LVM\s+(\S+)
README:This directory contains:version\s+(\S+)\s+release
README:\.tar\.gz:lvm_(.+)\.tar\.gz

INSTALL:1:LVM\s+(\S+)
INSTALL:1. Change to:\/([^\/]+)
INSTALL:6. Change back to :LVM\/([^\/]+)
INSTALL:1. change directory:\/([^\/]+)

FAQ:1:LVM\s+(\S+)

KNOWN_BUGS:1:LVM\s+(\S+)
KNOWN_BUGS:Known bugs in LVM:LVM\s+(.+).\s*

LVM-HOWTO:1:LVM\s+(\S+)

TODO:1:LVM\s+(\S+)

WHATSNEW:1:LVM\s+(\S+)
WHATSNEW:What is new in LVM:LVM\s+(\S+)

kernel/lvm.h:LVM_KERNEL_H_VERSION:LVM\s+(\S+)

kernel/lvm-snap.c:lvm_snap_version __:LVM\s+(\S+)

kernel/lvm-internal.h:LVM_INTERNAL_H_VERSION:LVM\s+(\S+)

kernel/lvm.c:char \*lvm_version:LVM\s+version\s+(\S+)
kernel/lvm.c:char \*lvm_short_version:\"version\s+(\S+)

tools/lvm_user.h:lvm_version = :\s+(\S+)\\nHeinz
tools/lvmcreate_initrd:Logical Volume Manager:er (.*) by Heinz

