#!/usr/bin/perl
################################################################################
##
##    Copyright 2001 Sistina Software, Inc.
##
##    This is free software released under the GNU General Public License.
##    There is no warranty for this software.  See the file COPYING for
##    details.
##
##    See the file CONTRIBUTORS for a list of contributors.
##
##    This file is maintained by:
##      AJ Lewis <lewis@sistina.com>
## 
##    File name: iopver
##
##    Description: outputs the IOP version of lvm in the src directory
##                 specified to stdout
################################################################################

use Getopt::Std;

getopt('d');

$lvm_src = $opt_d;

# help message
if(defined($opt_h) || !defined($opt_d)) {
  $msg = "usage: $0 [OPTIONS]\n";
  $msg = $msg . "\t-d\tLVM src directory\n";
  $msg = $msg . "\t-h\tDisplay this help message\n";
  die $msg;
}

    if ($lvm_src) {
      $file1 = $lvm_src . '/kernel/lvm.h';
      $file2 = $lvm_src . '/tools/lib/liblvm.h';
    }

    open(DATAFILE, "< $file1") || die "platform: can't open $file1\n";
    while ($line = <DATAFILE>)
    {
	if ($line =~ /LVM_DRIVER_IOP_VERSION\s*([\w\d.]+).*/)
        {	
	    	$version1 = $1;
  	}
    }
    close(DATAFILE);

    open(DATAFILE, "< $file2") || die "platform: can't open $file2\n";
    while ($line = <DATAFILE>)
    {
        if ($line =~ /LVM_LIB_IOP_VERSION\s*([\w\d.]+).*/)
        { 
                $version2 = $1;
        }
    }
    close(DATAFILE);

    
    die "Error: iopver can't determine the IOP version from $file1\n" unless (defined($version1));
    die "Error: iopver can't determine the IOP version from $file2\n" unless (defined($version2));
    die "Error: iopver reading inconsistent IOP versions\nIOP version $version1 reported in $file1\nIOP version $version2 reported in $file2\n" unless ($version1 eq $version2);

print "$version1\n";

