#! /bin/sh
#
# Configuration script for ftape.
#
# This script will create the kernel-version.h file that contains
# kernel version dependent settings.
#
# Configure,v 1.1.1.1 1995/11/15 16:55:46 root Exp
#
# Make sure we're running bash
[ -z "$BASH" ] && { echo "Configure requires bash !" 1>&2; exit 1; }
#
#
if [ "$#" = "0" ] 
then
	LINUX_ROOT=/usr/src/linux
else
	LINUX_ROOT=$1
	if [ ! -d $LINUX_ROOT ]
	then
		echo -e "\nusage:"
		echo "$0 <root directory of linux source tree>"
		echo "or just"
		echo -e "$0\n"
	fi
fi
VERSION="$LINUX_ROOT/include/linux/version.h"
CONFIG="$LINUX_ROOT/include/linux/autoconf.h"
TARGET="kernel-version.h"

cat >$TARGET <<HERE
#ifndef _KERNEL_VERSION_H
#define _KERNEL_VERSION_H
/*  
 *  Kernel options for ftape device driver
 *
 *  created by: `pwd`/$0
 *          on: `date`
 *
 * Don't change this file, instead change Configure.skel, which contains the
 * conditionals for the different kernel versions.
 * 
HERE

if cat $VERSION 2>/dev/null >/dev/null ;
then

#
# Use the kernel source code for version information and configuration options.
#
	echo " *  Used "$VERSION >>$TARGET
        echo " */" >>$TARGET
	
	grep UTS_RELEASE $VERSION >>$TARGET

	if grep NR_FTAPE_BUFFERS $CONFIG > /dev/null 2>&1 
	then	
		grep NR_FTAPE_BUFFERS $CONFIG >>$TARGET
	else
		echo "#define NR_FTAPE_BUFFERS 3" >>$TARGET
	fi

	grep CONFIG_MODVERSIONS $CONFIG >>$TARGET

	HOST=`grep LINUX_COMPILE_HOST $VERSION | cut -d"\"" -f2`

# Use for testing:
#	echo " *  Used dummy" >>$TARGET
#	echo " */" >>$TARGET
#	echo "#define UTS_RELEASE \"1.1.90\"" >>$TARGET
#	echo "#define MODULE 1" >>$TARGET

else

#
# Use the running kernel for version and configuration information.
#
	echo "$VERSION seems not to exist"
	echo "Using running kernel  for configuration"

	echo " *  Used /proc/version" >>$TARGET
	echo " */" >>$TARGET

	echo "#define UTS_RELEASE \"`cat /proc/version | cut -d" " -f3`\"" \
		>>$TARGET
	
	echo "#define NR_FTAPE_BUFFERS 3" >>$TARGET

        if grep Using_Versions /proc/ksyms >/dev/null 2>/dev/null ;
        then
		echo "#define CONFIG_MODVERSIONS 1" >>$TARGET
        else
		echo "#undef CONFIG_MODVERSIONS" >>$TARGET
	fi

	HOST=`cat /proc/version | cut -d"@" -f2 | cut -d")" -f1`

fi

#
# Special test to set a flag on Bas Larhovens ftape development system
#       
if [ "z$HOST" = "zdodo" ] ;
then
	echo "#define DODO 1" >>$TARGET
else
	echo "#define DODO 0" >>$TARGET
fi

#
# Now extract the version numbers for comparisons
#
declare -i MAJOR=`grep UTS_RELEASE $TARGET | \
	cut -d" " -f3 | cut -d\" -f2 | cut -d\. -f1`
declare -i MINOR=`grep UTS_RELEASE $TARGET | \
	cut -d" " -f3 | cut -d\" -f2 | cut -d\. -f2`
declare -i REVISION=`grep UTS_RELEASE $TARGET | \
	cut -d" " -f3 | cut -d\" -f2 | cut -d\. -f3`
declare -i VERSION=$REVISION+1000*$MINOR+1000000*$MAJOR

echo Building ftape for kernel version $MAJOR.$MINOR.$REVISION \($VERSION\)

#
# Add integer coded kernel version number for conditional compilation
#
echo "#define KERNEL_VERSION $VERSION" >>$TARGET

#
# now add the known incompatibilty test for the different kernel versions
#
cat Configure.skel >>$TARGET 

#
# Done
#
echo "#endif /* _KERNEL_VERSION_H */" >> $TARGET
exit
#








