#!/bin/sh
################################################################################
##
##    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
################################################################################

# help message
usage()
{
  echo "usage: $0 [OPTIONS]";
  echo -e "\t-d\tLVM src directory";
  echo -e "\t-h\tDisplay this help message";
  exit 0;
}

while getopts "d:h" option ;
do
	case $option in
		d) linux_src=${OPTARG};;
		*) usage; exit;;
	esac
done

if [ "x${linux_src}" = "x" ]; then
	linux_src="/usr/src/linux";
fi

file="${linux_src}/Makefile";

if [ ! -e $file ]; then
	echo "linuxver - $file does not exist" > /dev/stderr;
	exit -1;
fi

major=`cat $file | grep "^VERSION[[:blank:]]*=" | \
	sed -e 's/^VERSION[[:blank:]]*=[[:blank:]]*\([[:digit:]]*\)\(.*\)/\1/'` ;
minor=`cat $file | grep "^PATCHLEVEL[[:blank:]]*=" | \
	sed -e 's/^PATCHLEVEL[[:blank:]]*=[[:blank:]]*\([[:digit:]]*\).*/\1/'`;
sublvl=`cat $file | grep "^SUBLEVEL[[:blank:]]*=" | \
	sed -e 's/^SUBLEVEL[[:blank:]]*=[[:blank:]]*\([[:digit:]]*\).*/\1/'`;
extraver=`cat $file | grep "^EXTRAVERSION[[:blank:]]*=" | \
	sed -e 's/^EXTRAVERSION[[:blank:]]*=[[:blank:]]*\([-+_.[:alnum:]]*\).*/\1/'`;

if [ "x$DEBUG" != "x" ]; then
	echo "major = $major";
	echo "minor = $minor";
	echo "sublvl = $sublvl";
	echo "extraver = $extraver";
fi

if [ -n "$major" -a -n "$minor" -a -n "$sublvl" ]; then
	if [ -n "$extraver" ]; then
		echo "${major}.${minor}.${sublvl}${extraver}";
	else
		echo "${major}.${minor}.${sublvl}";
	fi;
else
	echo "linuxver: Unable to determine version of linux kernel in ${linux_src}" > /dev/stderr;
	exit -2;
fi

