#!/usr/bin/perl

###############################################################################
###############################################################################
##
##  Copyright (C) Sistina Software, Inc.  1997-2003  All rights reserved.
##  Copyright (C) 2004 Red Hat, Inc.  All rights reserved.
##
##  This copyrighted material is made available to anyone wishing to use,
##  modify, copy, or redistribute it subject to the terms and conditions
##  of the GNU General Public License v.2.
##
###############################################################################
###############################################################################

##    Description: outputs the version of linux in the src directory specified 
##                 to stdout

use Getopt::Std;

getopt('d');

$linux_src = $opt_d;
$majorver = $opt_b;
$noextra = $opt_s;

# help message
if(defined($opt_h)) {
  $msg = "usage: $0 [OPTIONS]\n";
  $msg = $msg . "\t-b\tBrief format (MAJOR_MINOR)\n";
  $msg = $msg . "\t-s\tsimple format (MAJOR.MINOR.RELEASE)\n";
  $msg = $msg . "\t-d\tLinux src directory   (Default: /usr/src/linux)\n";
  $msg = $msg . "\t-h\tDisplay this help message\n";
  $msg = $msg . "\t-v\tVerbose format (${MAJOR}.${MINOR}.${RELEASE}${EXTRAVERSION}) - Default\n";
  die $msg;
}

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

    open(DATAFILE, "< $file") || die "$0: can't open $file\n";
    while ($line = <DATAFILE>)
    {
    	if ($majorver) {
		if ($line =~ /UTS_RELEASE.+"(\d+\.\d+)\.\d+/)
	  	{
	    	$version = $1;
		$version =~ tr/\./_/;
	  	}
	}
	else {
		if ($noextra) {
		  if ($line =~ /UTS_RELEASE.+"(\d+\.\d+\.\d+)/)
		  {
		    $version = $1;
		  }
		}
		else {
  		  if ($line =~ /UTS_RELEASE.+"(\d+\.\d+\.[\+\_\-\.\d\w]+)/)
  		  {
    		    $version = $1;
  		  }
		}
	}
    }

    close(DATAFILE);

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

print "$version\n";

