#!/bin/bash
#
# get_uname topdir kernel_tree
#
# This simple script makes sure the kernel headers are correctly
# and prints the version blurb for the kernel.

KHEADERS=$2/include
[ -d "$2" ] || {
    echo "=================================================================="
    echo "  The directory '$2' is missing. Please specify"
    echo "  the correct path of the kernel source tree in Rules.make"
    echo "=================================================================="
    exit 1
} 1>&2

[ ! -f $KHEADERS/linux/version.h -o ! -f $KHEADERS/linux/autoconf.h ] && {
    echo "==================================================================="
    echo "  One of the files"
    echo "       '$KHEADERS/linux/version.h'"
    echo "       '$KHEADERS/linux/autoconf.h'"
    echo "  is missing. Make sure your kernel headers are installed properly."
    echo 
    echo "  Note that it is necessary to both configure the kernel"
    echo "  and generate the dependencies. That is, do something like:"
    echo "       'cd $2 ; make oldconfig ; make dep'"
    echo "==================================================================="
    exit 1
} 1>&2
cd $1/scripts

gcc -c -o kuname.o -I$KHEADERS kuname.c > /dev/null || exit $?
gcc -o kuname kuname.o > /dev/null || exit $?
./kuname
RESULT=$?
rm -f kuname kuname.o > /dev/null

exit $RESULT
