#! /bin/sh
#
# Well, we need this to generate a kernel version number,
# that can be easily used whith #if.
#
# We do *not* use /proc/version, because we don't rely on the version of the
# loaded kernel, but on the /usr/src/linux/include..., which is used
# for compilation of dosemu. Ok?
#
# Don't laugh on the 3 digits for each version level,
# we'll never know how long Linus is staying on each level.
#

VERSION=""

# "set -P" does not work on bash 1.14.2, and 1.14.3 is buggy
nolinks=1
export nolinks

function zeropad() {
  if [ $1 -lt 10 ]; then VERSION="${VERSION}00$1"
  else
    if [ $1 -lt 100 ]; then VERSION="${VERSION}0$1"
    else VERSION="${VERSION}$1"; fi
  fi
}

if [ -z "$1" ]; then
  KERNELSRC="-find"
else
  KERNELSRC=$1
fi

if [ "$KERNELSRC" = "-find" ]; then
  KERNELSRC=""
  if [ -d /usr/include/linux ]; then
    xxxx=`(cd /usr/include/linux; pwd)`
    KERNELSRC=`(cd $xxxx/../..; pwd)`
  else
    if [ -d /usr/src/linux ]; then
      KERNELSRC=`(cd /usr/src/linux; pwd)`
    else
      if [ -d /linux ]; then
        KERNELSRC=`(cd /linux; pwd)`
      else
        echo "kversion.sh: cannot find any of the standard linux trees, giving up"
        echo "You have to edit LINUX_KERNEL in the main Makefile so that it"
        echo "points to your Linux source tree, which at least must contain ./include/*"
        echo 'In addition (if your ./linux/include lacks the version.h file):'
        echo "You may edit and uncomment KERNEL_VERSION in the main Makefile,"
        echo "but this is not recommended !"
        echo "The format is:"
        echo "  KERNEL_VERSION=x0yy0zz meaning Linux version x.yy.zz"
        echo "Example:"
        echo "  KERNEL_VERSION=1002002 meaning Linux version 1.2.2"
        exit 1
      fi
    fi
  fi
fi

if [ "$2" = "-print" ]; then
  echo "$KERNELSRC"
  exit 0
fi

if [ -z "$2" ]; then
  DOSEMUSRC="../"
else
  DOSEMUSRC=$2
fi

if [ -f ${KERNELSRC}/include/linux/version.h ]; then
  VERSIONFILE="${KERNELSRC}/include/linux/version.h"
else
  VERSIONFILE="${KERNELSRC}/tools/version.h"
  if [ ! -f ${VERSIONFILE} ]; then
    echo "missing ${VERSIONFILE}, giving up!"
    exit 1
  fi
fi


ASCII_VERSION=`grep UTS_RELEASE ${VERSIONFILE} |cut '-d"' -f2`
VERSION=`echo "${ASCII_VERSION}" |cut '-d.' -f1` 
zeropad `echo "${ASCII_VERSION}" |cut '-d.' -f2` 
zeropad `echo "${ASCII_VERSION}" |cut '-d.' -f3` 
echo "#ifndef KERNEL_VERSION"
echo "#define KERNEL_VERSION ${VERSION}"
echo "#endif "
