#!/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: linuxver
##
##    Description: outputs the version of linux in the src directory specified 
##                 to stdout
################################################################################

use Getopt::Std;

getopt('d');

$linux_src = $opt_d;

# help message
if(defined($opt_h)) {
  $msg = "usage: $0 [OPTIONS]\n";
  $msg = $msg . "\t-d\tLinux src directory   (Default: /usr/src/linux)\n";
  $msg = $msg . "\t-h\tDisplay this help message\n";
  die $msg;
}

    # if there was an argument to linuxver, use it as the linux source directory
    if ($linux_src) {
      $file = $linux_src . '/Makefile';
    }
    # otherwise, use the default 
    else {
      $file = '/usr/src/linux/Makefile';
    }
      

    open(DATAFILE, "< $file") || die "platform: can't open $file\n";
    while ($line = <DATAFILE>)
    {
	if ($line =~ /VERSION\s*=\s*(\d+)/)
  	{
	    	$major = $1;
		#print "major = $major\n";
		#$major = ($version ~ s/(VERSION)(\s)*(=)(\s)*(\d)/$5/);
  	}
	if ($line =~ /PATCHLEVEL\s*=\s*(\d+)/)
	{
		$minor = $1;
		#print "minor = $minor\n";
	}
	if ($line =~ /SUBLEVEL\s*=\s(\d+)/)
	{
		$sublevel = $1;
		#print "sublevel = $sublevel\n";
	}
	if ($line =~ /EXTRAVERSION\s*=\s([-\d\w_]+)/)
	{
		$extraver = $1;
		#print "extraver = $extraver\n";
	}
    }

    if(defined($major) && defined($minor) && defined($sublevel))
    {
    	if(defined($extraver))
	{
		$version = $major . "." . $minor . "." . $sublevel . $extraver;
	}
	else
	{
    		$version = $major . "." . $minor . "." . $sublevel
	}
     }

    close(DATAFILE);

    die "linuxver can't determine Linux version\n" unless (defined($version));

print "$version\n";

