#!/bin/sh
#$Id: kconf,v 1.1 2002/09/11 14:25:17 chris Exp $
#
# configure aksparlnx for correct kernel version
# creates /opt/aksparlnx/etc/kversion

outfile=/opt/aksparlnx/etc/kversion
MODDIR=/opt/aksparlnx/drv

if [ "$1" != "-reconf" ]; then
    echo "usage: $0 -reconf"
    exit 1
fi

#rm -f $outfile
#echo "KVERSION=2.4.18" > $outfile
#exit 0

VERSION=$(uname -r)
ISSMP="$(uname -a | grep SMP)"

if [ ! "$ISSMP" = "" ]; then
    if [ "$(uname -r | grep -i smp)" = "" ]; then
	SMP=-smp
    fi
fi

if [ -f $MODDIR/$VERSION$SMP/aksparlnx.o ]; then
    # found=yes
    # best possible match
    rm -f $outfile
    echo "KVERSION=$VERSION$SMP" > $outfile
else   # exact modules not there
    TWODOT="$(echo $VERSION | grep ^2.0 > /dev/null 2>&1)"
    if [ "$TWODOT" = "" ]; then # Kernel-Version is either 2.2.x or 2.4.x or ?

	# cut everything after 3rd number:  2.4.18-3 will be 2.4.18
	VERSION2=$(echo $VERSION | sed 's/\([0-9]*.[0-9]*.[0-9]*\).*/\1/')

	if [ -f $MODDIR/$VERSION2$SMP/aksparlnx.o ]; then

	    rm -f $outfile
	    echo "KVERSION=$VERSION2$SMP" > $outfile
	    echo "USEFORCE=-f" >> $outfile
	    exit 0

	fi

	# search next "lower" version that we have

	VHIGH=$(echo $VERSION2 | sed 's/\([0-9]*.[0-9]*\).*/\1/')
	VLOW=$(echo $VERSION2 | sed 's/[0-9]*.[0-9]*.\([0-9]*\)/\1/')

	while [ $VLOW -ge 0 ]; do

	    if [ -f $MODDIR/$VHIGH.$VLOW$SMP/aksparlnx.o ]; then

		rm -f $outfile
		echo "KVERSION=$VHIGH.$VLOW$SMP" > $outfile
		echo "USEFORCE=-f" >> $outfile
		exit 0

	    fi

	    VLOW=$(expr $VLOW - 1)

	done

	# nothing found!

	echo "Fatal: no matching module found for kernel version $VERSION"
	echo "Parallel key access not possible"
	exit 1

    else # Kernel-Version is 2.0.x -> supported by module 2.0.38
	rm -f $outfile
	echo "KVERSION=2.0.38" > $outfile
	echo "USEFORCE=-f" >> $outfile
    fi
fi
