#!/bin/sh
# Copyright 2000, International Business Machines Corporation and others.
# All Rights Reserved.
# 
# This software has been released under the terms of the IBM Public
# License.  For details, see the LICENSE file in the top-level source
# directory or online at http://www.openafs.org/dl/license10.html

# Verify that this build machine has the required header files for the
# kernel versions specified in LINUX_VERS.
#
# The following critical variables are passed in from ./Makefile:
#
#   LINUX_SRCDIR="/usr/src/linux-"
#   LINUX_VERS="2.2.5-15 2.2.10 2.2.12 2.2.12-20 2.2.13 2.2.14"
#
# To add support for a new Linux kernel rev, msut therefore add it to 
# $LINUX_VERS in ./Makefile and create $LINUX_SRCDIR$VERS, populating
# the latter with header files including a version of the include file
# $LINUX_SRCDIR$VERS/include/linux/version.h  containing the appropriate
# UTS_RELEASE definition.
#

# The following two tests are done so that "make install" can be executed
# directly from the config directory.
if [ -z "$LINUX_VERS" ]; then
	echo LINUX_VERS is not set. Not testing header links for kernel build.
	exit 0
fi

if [ -z "$LINUX_SRCDIR" ]; then
	echo "LINUX_SRCDIR is not set. Not testing header links for kernel build."
	exit 0
fi

errors="false"
found_one="false"
CAN_BUILD=""

for VERS in $LINUX_VERS ; do
	dir=$LINUX_SRCDIR$VERS
	if [ ! -d $dir ] ; then
	    dir=$LINUX_SRCDIR
	    if [ ! -d $dir ] ; then
		echo "ERROR: Cannot build for Linux kernel $VERS: $dir does not exist."
		errors=true
		continue
      	    fi
	fi
	header=$LINUX_SRCDIR$VERS/include/linux/version.h
	if [ ! -f $header ] ; then
	    header=$LINUX_SRCDIR/include/linux/version.h
	    if [ ! -f $header ] ; then
	        echo "ERROR: Cannot build for Linux kernel $VERS: $header does not exist."
	        errors=true
	        continue
	    fi
	fi

	vers=`fgrep UTS_RELEASE "$header" |
		awk 'BEGIN { FS="\"" } { print $2 }'`
	if [ "x$vers" = "x" ] ; then
	    echo "ERROR: Cannot build for Linux kernel $VERS:"
	    echo "       No UTS_RELEASE string found in $header."
	    continue
	elif [ "$VERS" != "$vers" ] ; then
# Redhat kernel source has the problem that they create one version.h for
# all their builds; So, we have to be creative here.
#	    echo "ERROR: Cannot build $VERS. Wrong version '('$vers')' in $header."
#	    errors=true
#	    continue
	    errors=true
	    for subvers in $vers ; do
		if [ "x$subvers" = "x$VERS" ] ; then
		    errors=false
		    continue
		fi
	    done
	fi
	CAN_BUILD="$CAN_BUILD $VERS"
        found_one="true"
done

if [ "x$errors" = "xtrue" ] ; then
    echo "ERROR: Should be able to build at least one of $LINUX_VERS."
    if [ "x$found_one" = "xtrue" ] ; then
	echo "       Cannot build all kernels. Only set up for $CAN_BUILD."
    else
        echo "       Valid headers not present for any Linux kernel."
        exit -1
    fi
fi

exit 0
