#!/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" >&2;
	exit -1;
fi

major=`cat $file | grep "^VERSION[[:space:]]*=" | \
	sed -e 's/^VERSION[[:space:]]*=[[:space:]]*\([[:digit:]]*\)\(.*\)/\1/'` ;
minor=`cat $file | grep "^PATCHLEVEL[[:space:]]*=" | \
	sed -e 's/^PATCHLEVEL[[:space:]]*=[[:space:]]*\([[:digit:]]*\).*/\1/'`;
sublvl=`cat $file | grep "^SUBLEVEL[[:space:]]*=" | \
	sed -e 's/^SUBLEVEL[[:space:]]*=[[:space:]]*\([[:digit:]]*\).*/\1/'`;
extraver=`cat $file | grep "^EXTRAVERSION[[:space:]]*=" | \
	sed -e 's/^EXTRAVERSION[[:space:]]*=[[:space:]]*\([-+_.[: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}" >&2;
	exit -2;
fi

